17
17
#include <iostream>
using namespace std;
class Polygon
{
protected:
double width, height;
public:
void setDimensions(double w, double h)
{
width = w;
height = h;
}
};
int main()
{
Polygon *polyPtr;
Rectangle rect;
Triangle tri;
polyPtr = ▭
polyPtr->setDimensions (5.0, 3.0);
cout << "Area of Rectangle: " << rect.area() << endl;
polyPtr = &tri;
polyPtr->setDimensions (4.0, 6.0);
cout << "Area of Triangle: " << tri.area() << endl;
return 0;
}
/*Output:
Area of Rectangle: 15
Area of Triangle: 12*/
/* Name: Daphal Atul Sanjay
Enrollment No.: 23210270283
2. Write a program which show the use of Pointer to derived class in multilevel
inheritance.*/
#include <iostream>
using namespace std;
class Base
{
public:
void display()
{
cout << "Base class display function." << endl;
}
};
int main()
{
Derived2 d2;
Derived2 *ptrDerived2 = &d2;
ptrDerived2->display();
Derived1 *ptrDerived1 = &d2;
ptrDerived1->display();
Base *ptrBase = &d2;
ptrBase->display();
return 0;
}
/*Output:
Derived2 class display function.
Derived1 class display function.
Base class display function.*/