c++ - setting a later value after constructing an object in object inheritance -


shape *shape[100]; square sqr;  void inputdata() { int len,width; cout << "enter length"; cin >> len; cout << "enter width"; cin >> width;  sqr = square(len,width,0); //---> have not compute area this, put 0 first      shape[0] = &sqr; }  void computearea() { int area; area = shape[0]->computearea(); //----> need set area here after getting } 

shape parent class , square sub-class

after creating square objects , insert shape array. not reach setarea() method in square class set area.

i have found 2 solution this, feel doesnt suit object inheritance polymorphism.

one way implement setarea() in shape class(i have setarea() on square class already) , call setarea method through polymorphism , set square area attributes.

another way create object method in shape class getsquare() can reach getarea() method through shape array

is 2 method valid? or there better way doing it?

class square: public shape{  private: int len; int width; int area;  public: square(string,int,int,int); int getarea(); void setarea(int); };  int square::computearea() { int sqrarea = len*width; area = setarea(sqrarea); return sqrarea; }  int square::setarea(int _area) { area = _area; } 

computing area should common shapes, hoisting computearea base class (and making abstract) seems valid solution.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -