c++ - For a derived class with a constructor that takes a base class pointer, how do I refer to the base class pointer? -


i'm working on assignment on abstract base classes shapes. last section need write class general 3d version of of prior 2d shapes eg. square.

so can see code below, constructor takes base class pointer shape*. in function "getvolume" need multiply z area of shape pointed shape*, can calculated function getarea() specified in each shapes respective class. refer shape being pointed to?

class prism : public square, public rectangle, public circle { private:     double z; public:     prism(){z=0;}     prism(shape*, double a){z=a;}     ~prism(){cout<<"prism destructor called"<<endl;}      //access functions     void print_(){cout<<"prism length = "<<z;}     double getlength(int n)const{ return z; }     void setlength(double a){z=a;}      //other functions     double getvolume(){ return ??????????;}      }; 

how refer shape being pointed to? hoping this->getarea() or shape*->getarea() errors tell me "shape not refer value" etc.

can offer assistance?

shape class type not object. should keep pointer shape in constructor. should like:

your class member:

shape * theshape; 

and constructor:

prism(shape* sh, double a) {     z=a;     theshape =sh; } 

and getvolume() should like:

double getvolume(){ return theshape->getarea()*z;} 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -