-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.Shape.cpp
70 lines (66 loc) · 1.54 KB
/
06.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
using namespace std;
#include<iostream>
class shape {
public:
double base, height;
shape(int a = 0, int b = 0) {
base = a;
height = b;
}
void getData() {
double a, b;
cout << "Enter Base : ";
cin >> a;
cout << "Enter Height : ";
cin >> b;
base = a, height = b;
}
virtual void displayArea() {
cout << "Shape undefined" << endl;
}
};
class triangle : public shape {
public:
void displayArea() {
double area = 0.5*base*height;
cout << "Area of the triangle : " << area << endl;
}
};
class rectangle : public shape {
public:
void displayArea() {
double area = base*height;
cout << "Area of the rectangle : " << area << endl;
}
};
int main() {
int choice;
shape *ptrA, * ptrB;
triangle t;
rectangle r;
ptrA = &t;
ptrB = &r;
while (1) {
cout << "Press 1 to enter dimensions of triangle.\nPress 2 to enter dimensions of rectangle.\nPress 3 to get area of triangle.\nPress 4 to get area of rectangle.\nPress 5 to exit.\n";
cout << "Enter choice : ";
cin >> choice;
switch(choice) {
case 1:
t.getData();
break;
case 2:
r.getData();
break;
case 3:
t.displayArea();
break;
case 4:
r.displayArea();
break;
default:
exit(0);
break;
}
}
return 0;
}