You are on page 1of 4

#include <iostream>

#include <cmath>

using namespace std;

class Shape
{
protected:
double area;

public:
Shape(double ar=0) : area(ar){}
virtual void display()
{
cout << "Area: " << area << " sq units." << endl;
}
virtual void readData(){}
virtual double findarea(){
return area;
}
};

class Circle : public Shape


{
double radius;

public:
Circle(double r=0) : Shape(), radius(r) {}
double findarea()
{
Shape::area = M_PI * radius * radius;
return Shape::area;
}
void readData()
{
cout << "Enter radius: ";
cin >> radius;
}
void display()
{
cout << "Circle -->\n";
cout << "Radius: " << radius << " units." << endl;
if (Shape::area == 0) findarea();
Shape::display();
}
};

class Rectangle : public Shape


{
double length;
double width;

public:
Rectangle(double l=0, double w=0) : Shape(), length(l), width(w) {}
double findarea()
{
Shape::area = length * width;
return Shape::area;
}
void readData()
{
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
}
void display()
{
cout << "Rectangle -->\n";
cout << "Length: " << length << " units." << endl;
cout << "Width: " << width << " units." << endl;
if (Shape::area == 0) findarea();
Shape::display();
}
};

class Triangle : public Shape


{
double side1;
double side2;
double side3;

public:
Triangle(double s1=0, double s2=0, double s3=0) : Shape(), side1(s1),
side2(s2), side3(s3) {}
double findarea()
{
double s = (side1 + side2 + side3)/2.0;
Shape::area = sqrt(s * (s-side1) * (s-side2) * (s-side3));
return Shape::area;
}
void readData()
{
cout << "Enter side 1: ";
cin >> side1;
cout << "Enter side 2: ";
cin >> side2;
cout << "Enter side 3: ";
cin >> side3;
}
void display()
{
cout << "Triangle -->\n";
cout << "Side 1: " << side1 << " units." << endl;
cout << "Side 2: " << side2 << " units." << endl;
cout << "Side 3: " << side3 << " units." << endl;
if (Shape::area == 0) findarea();
Shape::display();
}
};

class ShapeNode
{
Shape* s;
ShapeNode* next;

public:
ShapeNode()
{
s = nullptr;
next = nullptr;
}
friend class ShapeStack;
};

class ShapeStack
{
ShapeNode* top;

public:
ShapeStack()
{
top = nullptr;
}
void push(Shape* shape)
{
ShapeNode* node = new ShapeNode;
node->s = shape;
node->next = top;
top = node;
}
Shape* pop()
{
ShapeNode* temp = top;
Shape* pop = temp->s;
top = top->next;
delete temp;
return pop;
}
void display()
{
ShapeNode* n = top;
cout << "Stack shapes from top to bottom -->\n";
while(n != nullptr)
{
n->s->display();
cout << endl;
n = n->next;
}
}
double totalAreaCovered()
{
ShapeNode* n = top;
double sum = 0.0;
while(n != nullptr)
{
sum += n->s->findarea();
n = n->next;
}
return sum;
}
};

int main()
{
ShapeStack stack;
int option;
cout << "Choose a stack operation:\n1.Push\n2.Pop\n3.Display\n4.Total
Area\n5.Exit\n";
cin >> option;
while (option != 5)
{
if (option == 1) {
int o;
cout << "Choose a Shape type to
push:\n1.Circle\n2.Rectangle\n3.Triangle\n";
cin >> o;
if (o == 1) {
cout << "\nCircle -->\n";
Shape *p = new Circle;
p->readData();
stack.push(p);
}
else if (o == 2) {
cout << "\nRectangle -->\n";
Shape *p = new Rectangle;
p->readData();
stack.push(p);
}
else if (o == 3) {
cout << "\nTriangle -->\n";
Shape *p = new Triangle;
p->readData();
stack.push(p);
}
else
cout << "Wrong option. No shape pushed to stack.\n";
}
else if (option == 2) {
Shape *p = stack.pop();
cout << "Details of shape popped -->\n";
p->display();
}
else if (option == 3) {
stack.display();
}
else if (option == 4) {
cout << "Total area: " << stack.totalAreaCovered() << " sq units\n";
}
else
cout << "Wrong option.\n";
cout << "\nChoose a stack operation:\n1.Push\n2.Pop\n3.Display\n4.Total
Area\n5.Exit\n";
cin >> option;
}
return 0;
}

You might also like