Hi, anyone know how come the answer is zero
#include <iostream>
using namespace std;
class Shape {
public:
int findArea() {
return 0;
}
};
class Square : public Shape {
int side;
public:
Square(int s) {
side = s;
}
int findArea() {
return side * side;
}
};
class Rectangle : public Shape {
int width;
int height;
public:
Rectangle(int w, int h) {
width = w;
height = h;
}
int findArea() {
return width * height;
}
};
int main() {
Shape* shape1 = &Square(3);
Shape* shape2 = &Rectangle(2, 5);
cout << "Area of square is "
<< shape1->findArea() << endl;
cout << "Area of rectangle is "
<< shape2->findArea() << endl;
return 0;
}
what is the problem and how to solve?
Your findArea() function in the base class should be virtual.
Also, please add code tags to improve readability.