## Write a Program to find the area of Rectangle and Triangle using Inheritance and Polymorphism. Submit two different program in Comments Section.

Rectangle: A rectangle is a four-sided flat shape where every angle is a right angle (90°).

The Area is the width times the height: Area = w × h

Triangle: The area of a polygon is the number of square units inside that polygon. Area is 2-dimensional like a carpet or an area rug. A triangle is a three-sided polygon. We will look at several types of triangles in this lesson.

1. This comment has been removed by the author.

2. //with multiple inheritance
#include
using namespace std;
class value
{
protected:
int h,w;
public:
void get_v();
};
class rect:public value
{
public:
void a_rect();
};
class tri:public value
{
public:
void a_tri();
};
void value::get_v()
{
cin>>h>>w;
}
void rect::a_rect()
{
cout<<h*w<<endl;
}
void tri::a_tri()
{
cout<<(h*w)/2<<endl;
}
int main()
{
rect r;
r.get_v();
r.a_rect();
tri t;
t.get_v();
t.a_tri();
}

3. ////Hirerichical inheritance
#include
using namespace std;
class Polygon
{
public:
void get_values();
protected:
int width,hight;
};
class Rectangle:public Polygon
{
public:
double area();
};
class Triangle:public Polygon
{
public:
double area();
};
void Polygon::get_values()
{
int a;
int b;
cin>>a;
cin>>b;
width=a;
hight=b;
}
double Rectangle::area()
{
return width*hight;
}
double Triangle::area()
{
return ((double)width*(double)hight)/2;
}
int main()
{
Rectangle r;
Triangle t;
cout<<"Enter the values for Rectangle: ";
r.get_values();
cout<<"Enter the values for Triangle: ";
t.get_values();
cout<<r.area()<<endl;
cout<<t.area()<<endl;
}

4. This comment has been removed by the author.

5. This comment has been removed by the author.

6. //Polymorphism
#include
using namespace std;
class Polygon
{
public:
void get_values();
protected:
int width,hight;
};
class Rectangle:public Polygon
{
public:
double area();
};
class Triangle:public Polygon
{
public:
double area();
};
void Polygon::get_values()
{
int a;
int b;
cin>>a;
cin>>b;
width=a;
hight=b;
}
double Rectangle::area()
{
return width*hight;
}
double Triangle::area()
{
return ((double)width*(double)hight)/2;
}
int main()
{
Rectangle r;
Triangle t;
Polygon *ppoly1 = &r;
Polygon *ppoly2 = &t;

cout<<"Enter the values for Rectangle: ";
ppoly1 ->get_values();
cout<<"Enter the values for Triangle: ";
ppoly2 ->get_values();
cout<<r.area()<<endl;
cout<<t.area()<<endl;
}

7. //polymorphism part 1
#include
using namespace std;
class value
{
protected:
int height,width;
public:
void get_v(int h,int w);
};
class rect:public value
{
public:
void a_rect();
};
class tri:public value
{
public:
void a_tri();
};
void value::get_v(int h,int w)
{
height=h;
width=w;
}
void rect::a_rect()
{
cout<<height*width<<endl;
}
void tri::a_tri()
{
cout<<(height*width)/2<<endl;
}

8. //part 2
int main()
{
rect r;
value * rect1 = &r;
rect1->get_v(10,5);
r.a_rect();
tri t;
value * rect2 = &t;
rect2->get_v(9,4);
t.a_tri();
}

Comment Here