I have three classes: Shape
, Rectangle
and Circle
. Shape
is parent of two other class. definition of this classes is in following code:
#include <iostream>
using namespace std;
class Shape {
public:
Shape() {
}
~Shape() {
}
void set(float BORDER, string COLOR) {
border = BORDER;
color = COLOR;
}
double computeArea() {
return 0;
}
private:
float border;
string color;
};
class Circle : public Shape {
public:
Circle() {
}
~Circle() {
}
void setRadius(float RADIUS) {
radius = RADIUS;
}
double computeArea() {
return 3.14 * radius * radius;
}
private:
float radius;
};
class Rectangle : public Shape {
public:
Rectangle() {
}
~Rectangle() {
}
void setWidth(float w) {
width = w;
}
void setLength(float l) {
length = l;
}
double computeArea() {
return width * length;
}
private:
float width;
float length;
};
I build two object from Circle
and Rectangle
classes. Then I copied this two object into Shape
class. When I run computeArea()
function in following order I get 0
result.
#include <iostream>
using namespace std;
int main() {
Circle c;
c.setRadius(3);
Rectangle r;
r.setWidth(4);
r.setLength(5);
Shape sh[2];
sh[0] = c;
sh[1] = r;
cout << sh[0].computeArea() << endl;
cout << sh[1].computeArea();
return 0;
}
I want compute area of all shapes with correct function. How can I do that?
Thanks in Advance