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

Program

The document is a C++ program that defines an abstract class 'Shape' with a pure virtual method for calculating area. It includes derived classes for 'Rectangle', 'Triangle', and 'Circle', each implementing the area calculation. The main function prompts the user for dimensions, calculates the areas of the shapes, and displays the results.

Uploaded by

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

Program

The document is a C++ program that defines an abstract class 'Shape' with a pure virtual method for calculating area. It includes derived classes for 'Rectangle', 'Triangle', and 'Circle', each implementing the area calculation. The main function prompts the user for dimensions, calculates the areas of the shapes, and displays the results.

Uploaded by

RØyaL LS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream>

#include <cmath>
using namespace std;
class Shape
{
public:
virtual double area() const = 0;
virtual ~Shape() {}
};
class Rectangle : public Shape
{
double length, width;
public: Rectangle(double l, double w) : length(l), width(w)
{}
double area() const override
{ return length * width;
} };
class Triangle : public Shape
{
double base, height;
public:
Triangle(double b, double h) : base(b), height(h)
{}
double area() const override
{ return 0.5 * base * height;
} };
class Circle : public Shape
{
double radius;
public: Circle(double r) : radius(r)
{}
double area() const override
{
return M_PI * radius * radius;
} };
double getPositiveInput(const string &prompt)
{
double value;
while (true)
{
cout << prompt;
cin >> value;
if ([Link]() || value <= 0)
{
cout << "Invalid input. Please enter a positive number.\n"; [Link]();
[Link](numeric_limits<streamsize>::max(), '\n');
}
else
{
return value;
} } }
int main()
{
cout << "=== Area Calculation Program ===\n";
double length = getPositiveInput("Enter rectangle length: ");
double width = getPositiveInput("Enter rectangle width: ");
Shape *rect = new Rectangle(length, width);
cout << "Area of Rectangle: " << rect->area() << "\n";
delete rect;
double base = getPositiveInput("Enter triangle base: ");
double height = getPositiveInput("Enter triangle height: ");
Shape *tri = new Triangle(base, height);
cout << "Area of Triangle: " << tri->area() << "\n";
delete tri;
double radius = getPositiveInput("Enter circle radius: ");
Shape *circ = new Circle(radius);
cout << "Area of Circle: " << circ->area() << "\n";
delete circ;
return 0;
}

You might also like