0% found this document useful (0 votes)
35 views2 pages

17

Uploaded by

atul10179
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

17

Uploaded by

atul10179
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/* Name: Daphal Atul Sanjay

Enrollment No.: 23210270283


Practical Related Questions:
1. Write a C++ program declare a class "polygon" having data members width
and height. Derive classes "rectangle" and "triangle" from "polygon" having
area() as a member function. Calculate area of triangle and rectangle using
pointer to derived class object.*/

#include <iostream>
using namespace std;

class Polygon
{
protected:
double width, height;
public:
void setDimensions(double w, double h)
{
width = w;
height = h;
}
};

class Rectangle : public Polygon


{
public:
double area()
{
return width * height;
}
};

class Triangle : public Polygon


{
public:
double area()
{
return 0.5* width * height;
}
};

int main()
{
Polygon *polyPtr;
Rectangle rect;
Triangle tri;
polyPtr = &rect;
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;
}
};

class Derived1: public Base


{
public:
void display()
{
cout << "Derived1 class display function." << endl;
}
};

class Derived2: public Derived1


{
public:
void display()
{
cout << "Derived2 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.*/

You might also like