You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use this class to store two double type values that could be used to compute the area of figures.
7
+
8
+
Add to the base class, a member function:
9
+
```
10
+
get_data(): to initialize base class data members.
11
+
display_area(): to compute and display area of figures.
12
+
```
13
+
Make display area as a virtual function and override this function into the derived classes to suit their requirements.
14
+
15
+
Derive two classes called triangle and rectangle from the base shape.
16
+
17
+
Using these three classes, design a program that will accept dimension of a triangle or a rectangle interactively, and display the area.
18
+
19
+
(Remember the two values given as input will be treated as length of two sides in case of rectangles, and as base and heights in the case of triangles.)
20
+
21
+
### Program 1a
22
+
Extend the above program to display area of circles.
23
+
24
+
This requires addition of a new derived class ‘circle’ that computes the area of a circle.
25
+
26
+
Remember, for a circle we need only one value its radius, but the get_data function in the base class requires two values to be passed.
double b ; double h ; // global variables for base and height
8
+
double b;
9
+
double h; // global variables for base and height
9
10
10
11
// Functions for Main Menus
11
12
voidF_First();
12
13
voidF_Second();
13
14
14
15
// Base Class
15
-
classShape { // Abstract class
16
+
classShape
17
+
{ // Abstract class
16
18
protected:
17
19
double base;
18
20
double height;
21
+
19
22
public:
20
-
voidget_data(double base, double height) {
21
-
this -> base = base;
22
-
this -> height = height;
23
+
voidget_data(double base, double height)
24
+
{
25
+
this->base = base;
26
+
this->height = height;
23
27
}
24
28
voidvirtualdisplay_area() = 0; // pure virtual function
25
29
};
26
30
27
31
// Triangle class to calculate the area of triangle
28
-
classTriangle : publicShape {
32
+
classTriangle : publicShape
33
+
{
29
34
public:
30
-
voiddisplay_area() {
35
+
voiddisplay_area()
36
+
{
31
37
cout << "Area of triangle: " << 0.5 * base * height << endl;
32
38
}
33
39
};
34
40
35
41
// Rectangle class to calculate the area of rectangle
36
-
classRectanglee : publicShape {
42
+
classRectanglee : publicShape
43
+
{
37
44
public:
38
-
voiddisplay_area() {
45
+
voiddisplay_area()
46
+
{
39
47
cout << "Area of Rectangle: " << base * height << endl;
40
48
}
41
49
};
42
50
43
-
44
-
classCircle : publicShape{
51
+
classCircle : publicShape
52
+
{
45
53
public:
46
-
voiddisplay_area() {
54
+
voiddisplay_area()
55
+
{
47
56
cout << "Area of Circle: " << base * base * 3.1415 << endl;
48
57
}
49
58
};
50
59
51
-
52
-
intmain() {
53
-
for (int k = 0; k < 1000; k++) {
60
+
intmain()
61
+
{
62
+
for (int k = 0; k < 1000; k++)
63
+
{
54
64
system("cls");
55
-
cout << " M A I N M E N U\n" << "-------------------------------\n" << "1. First program\n" << "2. Second program\n" << "0. Exit\n" << "-------------------------------\n" << "Your choice: \n";
65
+
cout << " M A I N M E N U\n"
66
+
<< "-------------------------------\n"
67
+
<< "1. First program\n"
68
+
<< "2. Second program\n"
69
+
<< "0. Exit\n"
70
+
<< "-------------------------------\n"
71
+
<< "Your choice: \n";
56
72
switch (_getch())
57
73
{
58
74
// First program
59
-
case49:
75
+
case49:
60
76
F_First();
61
-
break;
77
+
break;
62
78
// second program
63
-
case50:
79
+
case50:
64
80
F_Second();
65
-
break;
66
-
default:
81
+
break;
82
+
default:
67
83
cout << " Your choice is not available in Menu.\n Please, enter one more time.\n";
68
-
Sleep(0700); Sleep(0700);
69
-
break;
84
+
Sleep(0700);
85
+
Sleep(0700);
86
+
break;
70
87
case48:
71
88
return0;
72
89
break;
73
90
} // Switch
74
-
} // For loop
91
+
}// For loop
75
92
system("pause");
76
93
}
77
94
78
-
79
-
voidF_First(){
80
-
for (int k = 0; k < 1000; k++) {
95
+
voidF_First()
96
+
{
97
+
for (int k = 0; k < 1000; k++)
98
+
{
81
99
system("cls");
82
-
cout << " C A L C U L A T I N G A R E A S\n" << "-------------------------------------------\n" << "1. Triangle\n" << "2. Rectangle\n" << "0. Back\n" << "Your choice: \n";
cout << "Your choice is not available in Menu.\nPlease, enter one more time.\n";
125
-
Sleep(0700); Sleep(0700);
159
+
Sleep(0700);
160
+
Sleep(0700);
126
161
}
127
-
break;
162
+
break;
128
163
} // Switch
129
-
} // For loop
164
+
}// For loop
130
165
system("pause");
131
166
}
132
167
133
-
134
-
voidF_Second() {
135
-
for (int k = 0; k < 1000; k++) {
168
+
voidF_Second()
169
+
{
170
+
for (int k = 0; k < 1000; k++)
171
+
{
136
172
system("cls");
137
-
cout << " S E C O N D P R O G R A M\n" << "-------------------------------------\n" << "1. Triangle\n" << "2. Rectangle\n" << "3. Circle\n"<< "0. Back\n" << "Your choice: \n";
173
+
cout << " S E C O N D P R O G R A M\n"
174
+
<< "-------------------------------------\n"
175
+
<< "1. Triangle\n"
176
+
<< "2. Rectangle\n"
177
+
<< "3. Circle\n"
178
+
<< "0. Back\n"
179
+
<< "Your choice: \n";
138
180
switch (_getch())
139
181
{
140
182
// Case to Exit from the program
141
-
case48: {
183
+
case48:
184
+
{
142
185
system("cls");
143
186
main();
144
-
}break;
187
+
}
188
+
break;
145
189
146
190
// First program
147
-
case49: {
191
+
case49:
192
+
{
148
193
system("cls");
149
194
cout << " T R I A N G L E \n";
150
195
cout << "----------------------------------\n";
151
-
Shape* shape; //
196
+
Shape *shape; //
152
197
Triangle triangle;
153
198
shape = ▵ // overriding functions for triangle
0 commit comments