// pointers to base class
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};
class Rectangle: public Polygon {
public:
int area()
{ return width*height; }
};
class Triangle: public Polygon {
public:
int area()
{ return width*height/2; }
};
int main () {
Rectangle rect;
Triangle trgl;
//Polygon * ppoly1 = ▭
//Polygon * ppoly2 = &trgl;
//ppoly1->set_values (4,5);
//ppoly2->set_values (4,5);
Polygon ppoly1 = rect;
Polygon ppoly2 = trgl;
ppoly1.set_values (4,5);
ppoly2.set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}
I know that commented lines( when pointer of Polygon is used to call function, its fine). Why do we need to take a pointer , why cannot we just use normal variable of type Polygon. I tried compiling, it compiles fine, but does not give the correct result. Why is it so ?.