-
Notifications
You must be signed in to change notification settings - Fork 0
/
16_5.cpp
53 lines (48 loc) · 1.06 KB
/
16_5.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
//dynamic allocation and polymorphism
#include <iostream>
using namespace std;
class CPolygon
{
protected:
int width, height;
public:
void set_values(int a, int b)
{
width = a;
height = b;
}
virtual int area(void) = 0;
void printarea(void)
{
cout << this->area() << endl;
}
};
class CRectangle: public CPolygon
{
public:
int area(void)
{
return (width*height);
}
};
class CTriangle: public CPolygon
{
public:
int area(void)
{
return (width*height/2);
}
};
int main()
{
CPolygon *poly1 = new CRectangle;
CPolygon *poly2 = new CTriangle;
poly1->set_values(20,5);
poly2->set_values(40,8);
poly1->printarea();
poly2->printarea();
delete poly1;
delete poly2;
getchar();
return 0;
}