Using Base class pointer create a derived class dynamic object. Set values of dynamically created object using base class pointer (use arrow operator with pointer of base class). Call Area function of derived class using base class pointer. Delete pointer of base class as it has fulfilled its duty.
CODE GIVEN BELOW
#include using namespace std;class CPolygon {protected:int width, height;public:void set_values (int a, int b){ width=a; height=b; }virtual int area (){ return (0); }};class CRectangle: public CPolygon {public:int area (){ return (width * height); }};class CTriangle: public CPolygon {public:int area (){ return (width * height / 2); }};int main () {CRectangle rect;CTriangle trgl;CPolygon poly;CPolygon * ppoly1 = ▭CPolygon * ppoly2 = &trgl;CPolygon * ppoly3 = &poly;ppoly1->set_values (4,5);ppoly2->set_values (4,5);ppoly3->set_values (4,5);cout < ppoly1-="">area() <>cout < ppoly2-="">area() <>cout < ppoly3-="">area() <>return 0;}
using namespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area ()
{ return (0); }
};
class CRectangle: public CPolygon {
int area ()
{ return (width * height); }
class CTriangle: public CPolygon {
{ return (width * height / 2); }
int main () {
CRectangle rect;
CTriangle trgl;
CPolygon poly;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
CPolygon * ppoly3 = &poly;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly3->set_values (4,5);
cout < ppoly1-="">area() <>
cout < ppoly2-="">area() <>
cout < ppoly3-="">area() <>
return 0;
}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here