C++ Pure virtual method override -
i'm facing little problem , don't know in situation : have first class (object3d) parent class
object3d{ public: object3d(){}; virtual ~object3d(){}; ///random methods virtual void printobj() = 0; virtual void printobj(double) = 0; //etc.. private: //.. };
as can see , have printobj() method overridden. objects take no arguments , in child class
class cube : public object3d { public: cube(unsigned char c):object3d(c){}; ~cube(){}; void printobj(); private: //.. };
and other classes require argument call printobj method
class teapot : public object3d { public: teapot(unsigned char c):object3d(c){}; ~teapot(){}; void printobj(double s){//code}; private: //.. };
the problem these 2 classes automatically abstract inherit pure virtual methods parent class.
i've thought redefining printobj(double) in cube class , printobj() class in teapot class , allow user call 'wrong' printobj() method.
edit : explain little bit more mean "calling wrong" printobj();
in main function have declared cube dynamically :
object3d *c = new cube(250);
and teapot , dynamically :
object3d *t= new teapot(250);
now if change classes way :
class cube : public object3d { public: cube(unsigned char c):object3d(c){}; ~cube(){}; void printobj(); private: void printobj(double){};// add method here in private };
and teapot :
class teapot : public object3d { public: teapot(unsigned char c):object3d(c){}; ~teapot(){}; void printobj(double s){}; private: void printobj(){};//i add method here in private };
following these changes , in main function still can these calls :
c->printobj(0);//a cube should called no arguments t->printobj();//a teapot should called arguments
which want avoid; can access these methods despite them being private because declared object dynamically object3d (i think that's why ?)
the solution quite simple. need made second method private.
class cube : public object3d { public: cube(unsigned char c):object3d(c){}; ~cube(){}; void printobj(); private: void printobj(double d); };
and
class teapot : public object3d { public: teapot(unsigned char c):object3d(c){}; ~teapot(){}; void printobj(double s){//code}; private: void printobj(){//code}; //.. };
Comments
Post a Comment