-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathVirtualFunction_ComputeArea.cpp
More file actions
82 lines (72 loc) · 1.07 KB
/
VirtualFunction_ComputeArea.cpp
File metadata and controls
82 lines (72 loc) · 1.07 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
#include<iostream>
#include<math.h>
using namespace std;
class shape
{
protected:
double length;
double width;
double area;
public:
void read()
{
cin>>length>>width;
}
virtual void compute_area()
{
;
}
};
class rectangle:public shape
{
public:
void compute_area()
{
area=length*width;
cout<<"Rectangle area is:"<<area<<endl;
}
};
class triangle:public shape
{
protected:
double height;
double peri;
public:
void compute_area()
{
cout<<"enter the value of length height and width:";
cin>>length>>height>>width;
peri=(length+width+height)/2;
area=(peri)*(peri-length)*(peri-width)*(peri-height);
area=sqrt(area);
cout<<"Triangle area is:"<<area<<endl;
}
};
int main()
{
rectangle r;
triangle t;
shape *s[2];
s[0]=&r;
s[1]=&t;
int ch;
do
{
cout<<"\n1)read"<<endl<<"2)area of rectangle."<<endl<<"3)area of triangle"<<endl<<"4)quit"<<endl;
cout<<"enter ur choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"enter the value of length and width:";
s[0]->read();
break;
case 2: s[0]->compute_area();
break;
case 3: s[1]->compute_area();
break;
case 4:
break;
}
}while(ch!=4);
return 0;
}