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.
//with multiple inheritance
ReplyDelete#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();
}
////Hirerichical inheritance
ReplyDelete#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;
}
//Polymorphism
ReplyDelete#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;
}
//polymorphism part 1
ReplyDelete#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;
}
//part 2
ReplyDeleteint 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();
}
iD:201820244
ReplyDelete#include
using namespace std;
class mullclass
{
public:
void mullclass_value();
protected:
int width,hight;
};
class Rectangle:public mullclass
{
public:
area();
};
class Triangle:public mullclass
{
public:
area();
};
void mullclass::mullclass_value()
{
int x;
int y;
cin>>x;//input for rectangle area value
cin>>y;
width=x;
hight=y;
}
Rectangle::area()
{
return width*hight;
}
Triangle::area()
{
return (width*hight)/2;
}
int main()
{
Rectangle obj1;
Triangle obj2 ;
mullclass*pointer1 = &obj1;
mullclass *pointer2 = &obj2;
cout<<"Enter the values for Rectangle: ";
pointer1 ->mullclass_value();
cout<<"Enter the values for Triangle: ";
pointer2 ->mullclass_value();
cout<<obj1.area()<<endl;
cout<<obj2.area()<<endl;
}