-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape.cpp
87 lines (74 loc) · 1.58 KB
/
shape.cpp
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
86
87
#include<bits/stdc++.h>
#include <cstdio>
using namespace std;
class Geometry{
public:
string color = "blue";
Geometry(){
}
void printColor(){
cout<<color<<endl;
}
virtual void calArea(){
cout<<"Area: 0 \nshape: unknown\n";
}
};
class Circle : public Geometry{
float radius;
float area = 0;
public:
Circle(){
radius = 5;
}
void calArea(){
area = 3.14 * radius * radius;
cout<<"Area: "<<area<<", Shape: Circle, Radius: "<<radius<<endl;
}
};
class Square : public Geometry{
float side;
float area = 0;
public:
Square(){
side = 5.3;
}
void calArea(){
area = side * side;
cout<<"Area: "<<area<<", Shape: Square, Side: "<<side<<endl;
}
};
class Cube : public Geometry{
float length;
float width;
float height;
float area;
public:
Cube(){
length = 5;
width = 5;
height = 5;
}
void calArea(){
area = length * width * 2 + length * height * 2 + width * height * 2;
cout<<"Area: "<<area<<", Shape: Cube, Length: "<<length<<" Height: "<<height<<" width: "<<width<<endl;
}
};
int main(){
Geometry* ar[10];
for(int i = 0; i < 10; i++){
int choice = 1 + rand()%3;
if(choice == 1){
ar[i] = new Circle();
}
else if(choice == 2){
ar[i] = new Square();
}
else if(choice == 3){
ar[i] = new Cube();
}
}
for(int i = 0; i < 10; i++){
ar[i]->calArea();
}
return 0;
}