-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual-fn.cpp
More file actions
52 lines (52 loc) · 794 Bytes
/
virtual-fn.cpp
File metadata and controls
52 lines (52 loc) · 794 Bytes
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
#include<iostream>
using namespace std;
class shape
{
protected:
int width, height;
public:
shape(){width=height=0;}
shape(int a,int b)
{
width=a;
height=b;
}
virtual void area(){}
};
class rectangle:public shape
{
int r;
public:
rectangle(){r=0;}
rectangle(int a,int b):shape(a,b)
{}
void area()
{
r=width*height;
cout<<"Area of rectangle: "<<r<<endl;
}
};
class triangle:public shape{
float t;
public:
triangle(){t=0;}
triangle(int a, int b):shape(a,b)
{}
void area()
{
t=0.5*width*height;
cout<<"Area of triangle: "<<t<<endl;
}
};
void calculate_area(shape *p)
{
p->area();
}
int main()
{
rectangle R(10,20);
triangle T(100,200);
calculate_area(&R);
calculate_area(&T);
return 0;
}