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); //---> i have not compute area for this, i just put a 0 for it first
shape[0] = &sqr;
}
void computeArea() {
int area;
area = shape[0]->computeArea();
//----> need to set my area here after getting it
}
shape is parent class and square is sub-class
after creating the square objects and insert it into shape array. i could not reach the setArea() method in my square class to set the area.
i have already found two solution for this, but feel that it doesnt suit object inheritance polymorphism.
one way is to implement setArea() in shape class(i have setArea() on square class already) and call the setArea method through polymorphism and set it into my square area attributes.
another way is to create a get object method in shape class which is getSquare() so i can reach the getArea() method through the Shape array
is my two method valid? or is there a 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;
}