Extending parent class method in C ++

0

I have a sphere class:

class CEsfera{ 
    protected: 
       double centro[3]; 
       double raio;

    public: CEsfera();

    CEsfera(double x, double y, double z, double r); 

    mostra() { 
          for (int i=0; i<3; i++) 
                cout << centro[i] << “, “; cout << raio;
    } 
}; 

with a defined radius and a function that shows attributes xyz and radius.

I need to create a derived class that includes the weight attribute and (re) set the show () method to display all attributes, including those inherited from the parent class.

and create a method that returns the value of the density of a ball (= weight / volume, volume = 4/3 π r ^ 3) that should not write anything on the screen.

c) Define a method that returns the value of the density of a ball (= weight / volume, volume = 4/3 π r ^ 3). This method should not write anything on the screen.

Cesfera Class Declaration

class Cesfera{
    protected:
      double centro[3];
      double raio;
        double vol;

    public:
    Cesfera();//const defeito
    Cesfera(double x, double y, double z, double r);// const enum
    Cesfera (const Cesfera&);
    void mostra(double x, double y, double z, double r);
    void calcvol();
    double volume(){return (vol);}
};

Cesfera Class Definition

void Cesfera::calcvol(){
   vol = pi*raio*raio*raio*(4.0/3.0);
}

Cesfera::Cesfera(double x, double y, double z, double r){
   centro[0]=x;
   centro[1]=y;
   centro[2]=z;
   raio=r;
}

void Cesfera::mostra(double x, double y, double z, double r){
     for (int i=0; i<3; i++) cout << centro[i] << ", ";
     cout << raio;
}

Statement of class Cbola (extends Cesfera)

class Cbola: public Cesfera {
  int peso;

  public:
    Cbola():Cesfera(x,y,z,r){}

    // Cbola(double x, double y, double z, double r, int p);
    void calculadens();
    void mostra0(double a, double b, double c, double d, int p);
};

Cbola class definition

void Cbola::calculadens(){
    dens = (peso/vol);
}

void Cbola::mostra0(double a, double b, double c, double d, int p){
     for (int i=0; i<4; i++) 
           cout << centro[i] << ", ";
     cout << raio<<endl;
     cout<<p<<endl;
}

Main

int main(){

    Cesfera esfera1(1,4,5,6);// constructor enumeracao
    esfera1.mostra(1,4,5,6);//chama para escrever

    Cbola bola1(1,4,5,6,8);// tenho o erro aqui no matching construct

    bola1.mostra0(1,4,5,6,8);
    esfera1.calcvol();// calc volume

    return 0;
}

My question is how do I get the mostra0 function in the derived class to use the parameters of the parent class and append the new parameter?

Supposedly with public in the derived class the parameters of the base class are not available for the mostra0 use function?

    
asked by anonymous 30.03.2018 / 20:24

1 answer

1

Object Orientation in C ++ is very different from Java, you need to make the method mostrar0 of the parent class also virtual so that you can set it in the Child class. Public does not mean that you can replace everything. Try to use in the Pai class

virtual void mostra0(double x, double y, double z, double r) = 0;

And in the Son class

void mostra0(double x, double y, double z, double r)

with the same parameter signature.

In addition, since the method is virtual, it is not possible to define your code in the parent class, only in the classes that extend it, using the same parameters.

    
30.03.2018 / 22:40