CP2 Lab Tech 8
CP2 Lab Tech 8
Create a base class Shape .Use this Class to store two double type values that could
be used to compute areas. Add two derived Class Triangle and Rectangle from the
base Class Shape. Add to the base class, a member functions get_data () to initialize
the data members in the base class and add another member function display_area ()
to compute the area.
Write a C++ program to implement the class that accepts dimensions and calculate
area.
Sample output:
Area of triangle is 25
Area of rectangle is 80
SOURCE CODE:
#include <iostream>
using namespace std;
class Shape {
public:
double a,b;
public:
void get_data(double a,double b)
{
this->a=a;
this->b=b;
}
void dispaly_area()
{
} };
class Triangle:public Shape {
public: Triangle()
{
}
public:
void display_area()
{
cout<<"Area of Triangle is "<<(a*b)/2<<endl;
} };
class Rectangle:public Shape {
public:
Rectangle()
{
}
public:
void display_area()
{
cout<<"Area of Rectangle is "<<(a*b)<<endl;
}; };
int main() {
Triangle t;
Rectangle r;
double m,n;
cout << "Enter base and height: ";
cin>>m>>n;
t.get_data(m,n);
t.display_area();
cout << "Enter length and width: ";
cin>>m>>n;
r.get_data(m,n);
r.display_area();
return 0;
OUTPUT: