0% found this document useful (0 votes)
27 views3 pages

Activity 11

This C++ program defines classes for common shapes - triangle, rectangle, square, and parallelogram. Each shape class inherits from a base shape class and contains methods to set the shape's measurements and compute its area. In main(), objects of each shape class are created, user input is used to set the measurements, and the area computation method is called for each shape.

Uploaded by

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

Activity 11

This C++ program defines classes for common shapes - triangle, rectangle, square, and parallelogram. Each shape class inherits from a base shape class and contains methods to set the shape's measurements and compute its area. In main(), objects of each shape class are created, user input is used to set the measurements, and the area computation method is called for each shape.

Uploaded by

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

#include <iostream>

#include <iomanip>
#include <cmath>
using namespace std;

class shape
{
private:
float length;
float width;
float base;
float height;
float side;
public:
shape ()
{
length = 0;
width = 0;
base = 0;
height = 0;
side = 0;
}
setMeasurement (float l, float w, float b, float h, float s)
{
length = l;
width = w;
base = b;
height = h;
side = s;
}
};

class triangle : public shape


{
protected:
float Tbase;
float Theight;
public:
float area, area1;
void computeTriangle (){
area1 = (Tbase * Theight) / 2;
cout << "Area of Triangle: " << area1 << endl;
}
void setTriangle (float h, float b){
Tbase = b;
Theight = h;
}
};

class rectangle : public shape


{
protected:
float Rwidth;
float Rlength;
public:
float area;
void computeRectangle (){
area = Rlength * Rwidth;
cout << "Area of Rectangle: " << area << endl;
}
void setRectangle (float l, float w) {
Rlength = l;
Rwidth = w;
}
};

class square : public shape


{
protected:
float Sside;
public:
float area;
void computeSquare () {
area = Sside * Sside ;
cout << "Area of Square: " << area << endl;
}
void setSquare (float s) {
Sside = s;
}
};

class parallelogram : public shape


{
protected:
float Pbase;
float Pheight;
public:
float area;
void computeParallelogram (){
area = Pbase * Pheight;
cout << "Area of Parallelogram: " << area << endl;
}
void setParallelogram (float b, float h) {
Pbase = b;
Pheight = h;
}
};

int main (){

triangle t;
square square;
rectangle r;
parallelogram p;

float b, h , l, w, s;

cout << "Enter triangle base: ";


cin >> b;
cout << "Enter triangle height: ";
cin >> h;
cout << endl;
cout << "Enter rectangle length: ";
cin >> l;
cout << "Enter rectangle width: ";
cin >> w;
cout << endl;
cout << "Enter square side: ";
cin >> s;
cout << endl;
cout << "Enter parallelogram base: ";
cin >> b;
cout << "Enter parallelogram height: ";
cin >> h;
cout << endl;

t.setTriangle(b, h);
r.setRectangle(l, w);
square.setSquare(s);
p.setParallelogram(b, h);

t.computeTriangle();
r.computeRectangle();
square.computeSquare();
p.computeParallelogram();

You might also like