0
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;
}
user2351750
  • 53
  • 1
  • 7
  • 2
    Show the Square and Shape classes! – Victor Sand May 08 '13 at 14:29
  • You have an error on `inputdata()` method, you should use `sqr = new Square(len, width, 0)`, look at warnings generated by your compiler, they are here not only to annoy you. Returning to topic, explain to us, why you need to keep `Square`s in `Shape` type array. If you want to use `Square`-specific method with this array, you can just use `Square *square[100];` array. If you want to keep another objects there, why do you use methods that usable only for `Square`s there? It looks like design problem, provide us with `Square` and `Shape` classes so we can help you with it. – SpongeBobFan May 08 '13 at 14:53

3 Answers3

1

Computing area should be something common to all shapes, so hoisting computeArea into the base class (and probably making it abstract) seems like a valid solution.

Mark B
  • 95,107
  • 10
  • 109
  • 188
0

If your Square really relies on getting its area set by someone passing it the result of its own computeArea(), your design looks wrong.

Why don't you implement computeArea() so that it sets the area on the object as well as returning it?

EDIT

Based on your updated question, why does SetArea() return anything? Actually, as per declaration, it doesn't, but its definition does. Simply drop SetArea() altogether (what sense does it make to set a square's area from outside, anyway?) and do this:

class Square: public Shape {

private:
  int len;
  int width;
  int area;

public:
  Square(string,int,int,int);
  int getArea();
};

int Square::computeArea() {
  area = len*width;
  return area;
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • lets say i have 3 square objects in my shapeArray, shape[0],shape[1],shape[3] and i compute 3 of them. setting area to my area attributes straight away after calling computeArea() in my square.cpp. will the program able to verify area belongs to which shape array? i though i need something like shape[0].setArea to correctly enter the correct area for each array. – user2351750 May 08 '13 at 14:46
  • @user2351750 I assume the area is stored in a data member of `Square`. This means each instance of `Square` has its own copy of the data member, and it's the one on which its member function `computeArea()` will operate. These are the very basics of OOP in C++; you might want to read a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to get to grips with them. – Angew is no longer proud of SO May 08 '13 at 14:58
  • ok i set it on area attributes in my computeArea() before returning the area. i got an error "void value not ignored as it ought to be. because runtime have not start,so no computation is done yet,so when i set it,i have this error. any ways to ignore or avoid this? – user2351750 May 08 '13 at 15:08
  • @user2351750 You're asking questions about code you haven't shown. Add the definition of `Shape`, `Square`, `computeArea()` and `setArea()` to your question. – Angew is no longer proud of SO May 08 '13 at 15:11
  • @user2351750 Not into comments. Edit your question to put the code in there. – Angew is no longer proud of SO May 08 '13 at 15:21
  • @user2351750 And I the answer. – Angew is no longer proud of SO May 08 '13 at 15:36
0

if you really only want to implement setArea in Square class, you can dynamic_cast

if ( Square *s = dynamic_cast<Square *>shape[0] ) {
  s -> setArea();
}

usually using dynamic_cast is a sign of bad design, in this case why wouldn't Shape implement setArea(), since area is common to all shape.

yngccc
  • 5,594
  • 2
  • 23
  • 33