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?
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 werevirtual
.– Jefferson Quesado
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?
– Nuno Dias
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)
– Sveen
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)
– Sveen
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?
– Nuno Dias
Cbola(double x,double y,double z,double r):Cesfera(x,y,z,r)
– Sveen