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

Temporary

The document is a C++ program that defines a base class 'Shape' and three derived classes: 'Triangle', 'Square', and 'Rectangle'. It allows users to calculate the area and perimeter of these shapes based on user input. The program runs in a loop until the user chooses to quit by entering a specific option.

Uploaded by

acernitro88588
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)
16 views3 pages

Temporary

The document is a C++ program that defines a base class 'Shape' and three derived classes: 'Triangle', 'Square', and 'Rectangle'. It allows users to calculate the area and perimeter of these shapes based on user input. The program runs in a loop until the user chooses to quit by entering a specific option.

Uploaded by

acernitro88588
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>

using namespace std;


class Shape
{
public:
void area(int x,int y, int c);
void perimeter(int x,int y,int z, int c);
};

class square :public Shape


{
public:
int length;
};

class Rectangle :public Shape


{
public:
int length;
int width;
};
class Triangle: public Shape
{
public:
int side;
int height;
int base;
};

int main()
{
int choice;
cout << "\t\t\t\tEnter Choice: " << endl;
cout << "\t\t1 for Triangle\t\t2 for Square" << endl;
cout << "\t\t3 for Rectangle\t\t4 to Quit" << endl;
cin >> choice;
while (choice != 4)
{
//Triangle
if (choice == 1)
{
Triangle obj;
cout << "Enter Height: " << endl;
cin >> obj.height;
cout << "Enter Base: " << endl;
cin >> obj.base;
cout << "Enter Side: " << endl;
cin >> obj.side;
obj.area(obj.base, obj.height, 1);
obj.perimeter(obj.base, obj.height, obj.side, 1);
}
//Square
if (choice == 2)
{
square obj;
cout << "Enter Length: " << endl;
cin >> obj.length;
obj.area(obj.length, 0, 2);
obj.perimeter(obj.length, 0, 0, 2);
}
//Rectangle
if (choice == 3)
{
Rectangle obj;
cout << "Enter Length: ";
cin >> obj.length;
cout << "Enter Width: ";
cin >> obj.width;
obj.area(obj.length, obj.width, 3);
obj.perimeter(obj.length,obj.width, 0, 3);

system("pause");
system("cls");
cout << "\t\t\t\tEnter Choice: " << endl;
cout << "\t\t1 for Triangle\t\t2 for Square" << endl;
cout << "\t\t3 for Rectangle\t\t4 to Quit" << endl;
cin >> choice;
}
system("pause");
return 0;
}
void Shape::area(int x, int y, int c)
{
int area;
//Triangle
if (c == 1)
{
area = (x*y)/2;
}
//Square
if (c == 2)
{
area = x*x;
}
//Rectangle
if (c == 3)
{
area = x*y;
}
cout << "Area is: " << area << endl;
}

void Shape::perimeter(int x, int y, int z, int c)


{
int perimeter;
//Triangle
if (c == 1)
{
perimeter = x + y + z;
}
//Square
if (c == 2)
{
perimeter = 4 * x;
}
//Rectangle
if (c == 3)
{
perimeter = 2*(x*y);
}
cout << "Perimeter is: " << perimeter << endl;
}

You might also like