-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_5.cpp
More file actions
85 lines (68 loc) · 1.98 KB
/
5_5.cpp
File metadata and controls
85 lines (68 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
#include <memory>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double Area() const = 0;
virtual void display() const = 0;
virtual ~Shape() {}
};
class Rectangle : public Shape {
private:
double length, width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double Area() const override {
return length * width;
}
void display() const override {
cout << "Rectangle (Length: " << length << ", Width: " << width << ")";
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double Area() const override {
return M_PI * radius * radius;
}
void display() const override {
cout << "Circle (Radius: " << radius << ")";
}
};
int main() {
vector<shared_ptr<Shape>> shapeCollection;
int choice, n;
cout << "How many shapes do you want to enter? ";
cin >> n;
for (int i = 0; i < n; ++i) {
cout << "\nEnter shape " << i + 1 << " type (1 - Rectangle, 2 - Circle): ";
cin >> choice;
if (choice == 1) {
double l, w;
cout << "Enter length: ";
cin >> l;
cout << "Enter width: ";
cin >> w;
shapeCollection.push_back(make_shared<Rectangle>(l, w));
} else if (choice == 2) {
double r;
cout << "Enter radius: ";
cin >> r;
shapeCollection.push_back(make_shared<Circle>(r));
} else {
cout << "Invalid choice. Skipping.\n";
}
}
cout << "\n--- Area Results ---\n";
for (size_t i = 0; i < shapeCollection.size(); ++i) {
cout << i + 1 << ". ";
shapeCollection[i]->display();
cout << " => Area = " << shapeCollection[i]->Area() << endl;
}
cout<<endl<<"24CE052_pushti kansara"<<endl;
return 0;
}