I have this Parent Class call
Shape (ShapeTwoD.cpp)
He got 2 Child Class
1 is Rectangle (Rectangle.cpp)
1 is Square (Square.cpp)
There this variable call area declare at Rectangle & Square , but not at Shape(the parent)
So i was trying to sort by area ascending, however area can be access by
class->getArea();
vector<ShapeTwoD*> myVector;
//during the period of recording data like width ,height to the shape.
myVector[arrayCount] = new Rectangle();
myVector[arrayCount].setWidth(inWidth);
myVector[arrayCount].setHeight(inHeight);
myVector[arrayCount].computeArea();
arrayCount ++;
//done record width , height and then compute area of the shape
Assuming myVector now Contains about 2 Rectangle and 1 Square
Now I want to sort them by area ascending.
But my getArea() function is only available at child class.
If i try do this
sort(myVector.begin(),myVector.end(),funcAreaSort);
I not sure where to create funcAreaSort , next is how do i send in child area , assuming if they are same shape class(rectangle against rectangle) or different shape class (rectangle against shape)
how i create a boolean operator overload that can compare by getArea() then see which is better return a.getArea() > b.getArea() and sort myVector
Question is Where do i create those function I was thinking of making it a free method at main.cpp but i fail getting the thing sort and compile with error like qualifier error
error: passing 'const shapeTwoD' as 'this' argument of 'virtual double shapeTwoD::getArea()' discards qualifers.
error: passing 'const shapeTwoD' as 'this' argument of 'virtual double shapeTwoD::getArea()' discards qualifers.
1) Make the compare function take
ShapeTwoD*as parameters, and have avirtualgetAreamethod in the base class, which you override in the children.2) Make the method
getAreaconst:because it logically makes sense & so that you can call it on
constobjects.