Extending Parent Class method in C++

Asked

Viewed 413 times

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 showing the Xyz attributes and the radius.

need to create a derivative class that includes the weight attribute and (re)define the display() method so that it displays all attributes on the screen, including those inherited from the parent class.

and create method that returns the density value 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 density value of a ball (=weight/volume, volume=4/3 π r 3). This method should not write anything on the screen.

Declaration of the Cesfera Class

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);}
};

Definition of the Cesfera class

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;
}

Declaration 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);
};

Definition of class Cbola

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 doubt is how do I get the function mostra0 in the derived class use the parent class parameters and add the new parameter?

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

1 answer

1

Object Orientation in C++ is quite different from Java, you need to make the method mostrar0 of the Father class also as virtual to be possible to define it in the Son class. The public does not mean that you can replace everything. Try using in the Parent class

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

And in the class Son

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

with the same parameter signature.

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

  • You don’t have to leave it abstract to make polyformism work. Only the virtual is enough. When you refer to differences in orientation and Java vs C++ objects, it is as if in Java all methods were virtual.

  • already corrected the above points however I get error: public: Cbola():Cesfera(x,y,z,r){}// error use in undeclared Identifier x, y, z, supposedly my constructor by enumeration of Cesfera should not deal with these parameters?

  • It is that you did not declare x,y and z, because the Child Builder did not define the parameters. the right should be Cbola(x,y,z,r):Cesfera(x,y,z,r)

  • O :Cesfera(x,y,z,r) is simply a call to the parent constructor. before executing the child constructor. Just like the JAVA super(x,y,z,r)

  • Ah ok basically is the link between the parent constructor for the child constructor. After changing I have error c++ requires a type specifier for all declarations. Should not pass the type of parameter?? In this case double type?

  • Cbola(double x,double y,double z,double r):Cesfera(x,y,z,r)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.