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

CP2 Lab Tech 8

The document discusses creating a base Shape class to store double values for computing areas. Derived Triangle and Rectangle classes are created from the base class. Member functions are added to initialize data and compute areas. A C++ program is implemented to accept dimensions as input and calculate triangle and rectangle areas.

Uploaded by

Alex Yumul
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)
16 views2 pages

CP2 Lab Tech 8

The document discusses creating a base Shape class to store double values for computing areas. Derived Triangle and Rectangle classes are created from the base class. Member functions are added to initialize data and compute areas. A C++ program is implemented to accept dimensions as input and calculate triangle and rectangle areas.

Uploaded by

Alex Yumul
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

TECHNICAL ASSESSMENT 8 - INTRODUCTION TO OOP

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:

Enter base and height: 10 5

Area of triangle is 25

Enter length and width: 10 8

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:

You might also like