0% found this document useful (0 votes)
5 views

Oops p4

The document defines classes for shapes including Circle, Rectangle and Triangle. It calculates the area of different shapes based on user input for the dimensions.

Uploaded by

Maximus Aranha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Oops p4

The document defines classes for shapes including Circle, Rectangle and Triangle. It calculates the area of different shapes based on user input for the dimensions.

Uploaded by

Maximus Aranha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1: #include <iostream>

2: #include <cmath>
3:
4: class Shape {
5: public:
6: virtual double calculateArea() const = 0;
7: };
8:
9: class Circle : public Shape {
10: private:
11: double radius;
12:
13: public:
14: Circle(double r) : radius(r) {}
15:
16: double calculateArea() const override {
17: return M_PI * radius * radius;
18: }
19: };
20:
21: class Rectangle : public Shape {
22: private:
23: double length;
24: double width;
25:
26: public:
27: Rectangle(double l, double w) : length(l), width(w) {}
28:
29: double calculateArea() const override {
30: return length * width;
31: }
32: };
33:
34: class Triangle : public Shape {
35: private:
36: double base;
37: double height;
38:
39: public:
40: Triangle(double b, double h) : base(b), height(h) {}
41:
42: double calculateArea() const override {
43: return 0.5 * base * height;
44: }
45: };
46:
47: int main() {
48: int choice;
49: double dimensions[2];
50:
51: std::cout << "Shape Area Calculator" << std::endl;
52: std::cout << "1. Circle" << std::endl;
53: std::cout << "2. Rectangle" << std::endl;
54: std::cout << "3. Triangle" << std::endl;
55: std::cout << "Enter your choice (1-3): ";
56: std::cin >> choice;
57:
58: switch (choice) {
59: case 1: {
60: double radius;
61: std::cout << "Enter the radius of the circle: ";
62: std::cin >> radius;
63: Circle circle(radius);
64: std::cout << "Area of the circle: " << circle.calculateArea() << std::endl;
65: break;
66: }
67: case 2: {
68: double length, width;
69: std::cout << "Enter the length and width of the rectangle: ";
70: std::cin >> length >> width;
71: Rectangle rectangle(length, width);
72: std::cout << "Area of the rectangle: " << rectangle.calculateArea() << std::endl;
73: break;
74: }
75: case 3: {
76: double base, height;
77: std::cout << "Enter the base and height of the triangle: ";
78: std::cin >> base >> height;
79: Triangle triangle(base, height);
80: std::cout << "Area of the triangle: " << triangle.calculateArea() << std::endl;
81: break;
82: }
83: default:
84: std::cout << "Invalid choice!" << std::endl;
85: break;
86: }
87:
88: return 0;
89: }
90:

You might also like