Skip to content

Commit 522ca58

Browse files
committed
OOP2 Lab 8 updated
1 parent c081435 commit 522ca58

File tree

3 files changed

+155
-72
lines changed

3 files changed

+155
-72
lines changed
-15.7 KB
Binary file not shown.

OOP2_Lab8/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Practical Lab Assignment - Virtual function
2+
3+
### Program 1
4+
Create a base class called shape.
5+
6+
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.

OOP2_Lab8/U1910049.cpp renamed to OOP2_Lab8/main.cpp

Lines changed: 129 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,196 +5,253 @@
55
#include <Windows.h>
66
using namespace std;
77

8-
double b ; double h ; // global variables for base and height
8+
double b;
9+
double h; // global variables for base and height
910

1011
// Functions for Main Menus
1112
void F_First();
1213
void F_Second();
1314

1415
// Base Class
15-
class Shape { // Abstract class
16+
class Shape
17+
{ // Abstract class
1618
protected:
1719
double base;
1820
double height;
21+
1922
public:
20-
void get_data(double base, double height) {
21-
this -> base = base;
22-
this -> height = height;
23+
void get_data(double base, double height)
24+
{
25+
this->base = base;
26+
this->height = height;
2327
}
2428
void virtual display_area() = 0; // pure virtual function
2529
};
2630

2731
// Triangle class to calculate the area of triangle
28-
class Triangle : public Shape {
32+
class Triangle : public Shape
33+
{
2934
public:
30-
void display_area() {
35+
void display_area()
36+
{
3137
cout << "Area of triangle: " << 0.5 * base * height << endl;
3238
}
3339
};
3440

3541
// Rectangle class to calculate the area of rectangle
36-
class Rectanglee : public Shape {
42+
class Rectanglee : public Shape
43+
{
3744
public:
38-
void display_area() {
45+
void display_area()
46+
{
3947
cout << "Area of Rectangle: " << base * height << endl;
4048
}
4149
};
4250

43-
44-
class Circle : public Shape{
51+
class Circle : public Shape
52+
{
4553
public:
46-
void display_area() {
54+
void display_area()
55+
{
4756
cout << "Area of Circle: " << base * base * 3.1415 << endl;
4857
}
4958
};
5059

51-
52-
int main() {
53-
for (int k = 0; k < 1000; k++) {
60+
int main()
61+
{
62+
for (int k = 0; k < 1000; k++)
63+
{
5464
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";
5672
switch (_getch())
5773
{
5874
// First program
59-
case 49:
75+
case 49:
6076
F_First();
61-
break;
77+
break;
6278
// second program
63-
case 50:
79+
case 50:
6480
F_Second();
65-
break;
66-
default:
81+
break;
82+
default:
6783
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;
7087
case 48:
7188
return 0;
7289
break;
7390
} // Switch
74-
} // For loop
91+
} // For loop
7592
system("pause");
7693
}
7794

78-
79-
void F_First(){
80-
for (int k = 0; k < 1000; k++) {
95+
void F_First()
96+
{
97+
for (int k = 0; k < 1000; k++)
98+
{
8199
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";
100+
cout << " C A L C U L A T I N G A R E A S\n"
101+
<< "-------------------------------------------\n"
102+
<< "1. Triangle\n"
103+
<< "2. Rectangle\n"
104+
<< "0. Back\n"
105+
<< "Your choice: \n";
83106
switch (_getch())
84107
{
85108
// Case to Exit from the program
86-
case 48: {
109+
case 48:
110+
{
87111
system("cls");
88112
main();
89-
}break;
113+
}
114+
break;
90115

91116
// First program
92-
case 49: {
117+
case 49:
118+
{
93119
system("cls");
94120
cout << " T R I A N G L E \n";
95121
cout << "----------------------------------\n";
96-
Shape* shape; //
122+
Shape *shape; //
97123
Triangle triangle;
98124
shape = &triangle; // overriding functions for triangle
99-
cout << "Enter the base: "; cin >> b;
100-
cout << "Entet the height: "; cin >> h;
101-
shape->get_data(b,h);
125+
cout << "Enter the base: ";
126+
cin >> b;
127+
cout << "Entet the height: ";
128+
cin >> h;
129+
shape->get_data(b, h);
102130
shape->display_area();
103131
cout << "-----------------------------------\n\n";
104132
system("pause");
105-
}break;
133+
}
134+
break;
106135

107136
// second program
108-
case 50: {
137+
case 50:
138+
{
109139
system("cls");
110140
cout << " R E C T A N G L E \n";
111141
cout << "----------------------------------\n";
112-
Shape* shape; //
142+
Shape *shape; //
113143
Rectanglee rectangle;
114144
shape = &rectangle;
115-
cout << "Enter the base: "; cin >> b;
116-
cout << "Entet the height: "; cin >> h;
117-
shape->get_data(b,h);
145+
cout << "Enter the base: ";
146+
cin >> b;
147+
cout << "Entet the height: ";
148+
cin >> h;
149+
shape->get_data(b, h);
118150
shape->display_area();
119151
cout << "------------------------------------\n\n";
120152
system("pause");
121-
}break;
153+
}
154+
break;
122155

123-
default: {
156+
default:
157+
{
124158
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);
126161
}
127-
break;
162+
break;
128163
} // Switch
129-
} // For loop
164+
} // For loop
130165
system("pause");
131166
}
132167

133-
134-
void F_Second() {
135-
for (int k = 0; k < 1000; k++) {
168+
void F_Second()
169+
{
170+
for (int k = 0; k < 1000; k++)
171+
{
136172
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";
138180
switch (_getch())
139181
{
140182
// Case to Exit from the program
141-
case 48: {
183+
case 48:
184+
{
142185
system("cls");
143186
main();
144-
}break;
187+
}
188+
break;
145189

146190
// First program
147-
case 49: {
191+
case 49:
192+
{
148193
system("cls");
149194
cout << " T R I A N G L E \n";
150195
cout << "----------------------------------\n";
151-
Shape* shape; //
196+
Shape *shape; //
152197
Triangle triangle;
153198
shape = &triangle; // overriding functions for triangle
154-
cout << "Enter the base: "; cin >> b;
155-
cout << "Entet the height: "; cin >> h;
156-
shape->get_data(b,h);
199+
cout << "Enter the base: ";
200+
cin >> b;
201+
cout << "Entet the height: ";
202+
cin >> h;
203+
shape->get_data(b, h);
157204
shape->display_area();
158205
cout << "-----------------------------------\n\n";
159206
system("pause");
160-
}break;
207+
}
208+
break;
161209

162210
// second program
163-
case 50: {
211+
case 50:
212+
{
164213
system("cls");
165214
cout << " R E C T A N G L E \n";
166215
cout << "----------------------------------\n";
167-
Shape* shape; //
216+
Shape *shape; //
168217
Rectanglee rectangle;
169218
shape = &rectangle;
170-
cout << "Enter the base: "; cin >> b;
171-
cout << "Entet the height: "; cin >> h;
172-
shape->get_data(b,h);
219+
cout << "Enter the base: ";
220+
cin >> b;
221+
cout << "Entet the height: ";
222+
cin >> h;
223+
shape->get_data(b, h);
173224
shape->display_area();
174225
cout << "------------------------------------\n\n";
175226
system("pause");
176-
}break;
227+
}
228+
break;
177229
// Third program
178-
case 51: {
230+
case 51:
231+
{
179232
system("cls");
180233
cout << " C I R C L E \n";
181234
cout << "----------------------------------\n";
182-
Shape* shape; //
235+
Shape *shape; //
183236
Circle circle1;
184237
shape = &circle1;
185-
cout << "Enter the radius: "; cin >> b;
238+
cout << "Enter the radius: ";
239+
cin >> b;
186240
shape->get_data(b, 0.0);
187241
shape->display_area();
188242
cout << "------------------------------------\n\n";
189243
system("pause");
190-
}break;
244+
}
245+
break;
191246

192-
default: {
247+
default:
248+
{
193249
cout << "Your choice is not available in Menu.\nPlease, enter one more time.\n";
194-
Sleep(0700); Sleep(0700);
250+
Sleep(0700);
251+
Sleep(0700);
195252
}
196-
break;
253+
break;
197254
} // Switch
198-
} // For loop
255+
} // For loop
199256
system("pause");
200257
}

0 commit comments

Comments
 (0)