纯虚函数
1 #include <iostream.h>
2
3
4
5 class point
6
7 {
8
9 public:
10
11 point(int i=0, int j=0) { x0=i; y0=j; }
12
13 virtual void set() = 0;
14
15 virtual void draw() = 0;
16
17 protected:
18
19 int x0, y0;
20
21 };
22
23
24
25 class line : public point
26
27 {
28
29 public:
30
31 line(int i=0, int j=0, int m=0, int n=0):point(i, j)
32
33 {
34
35 x1=m; y1=n;
36
37 }
38
39 void set() { cout<<"line::set() called.\n"; }
40
41 void draw() { cout<<"line::draw() called.\n"; }
42
43 protected:
44
45 int x1, y1;
46
47 };
48
49
50
51 class ellipse : public point
52
53 {
54
55 public:
56
57 ellipse(int i=0, int j=0, int p=0, int q=0):point(i, j)
58
59 {
60
61 x2=p; y2=q;
62
63 }
64
65 void set() { cout<<"ellipse::set() called.\n"; }
66
67 void draw() { cout<<"ellipse::draw() called.\n"; }
68
69 protected:
70
71 int x2, y2;
72
73 };
74
75
76
77 void drawobj(point *p)
78
79 {
80
81 p->draw();
82
83 }
84
85
86
87 void setobj(point *p)
88
89 {
90
91 p->set();
92
93 }
94
95
96
97 void main()
98
99 {
100
101 line *lineobj = new line;
102
103 ellipse *elliobj = new ellipse;
104
105 drawobj(lineobj);
106
107 drawobj(elliobj);
108
109 cout<<endl;
110
111 setobj(lineobj);
112
113 setobj(elliobj);
114
115 cout<<"\nRedraw the object...\n";
116
117 drawobj(lineobj);
118
119 drawobj(elliobj);
120
121 }
122
2
3
4
5 class point
6
7 {
8
9 public:
10
11 point(int i=0, int j=0) { x0=i; y0=j; }
12
13 virtual void set() = 0;
14
15 virtual void draw() = 0;
16
17 protected:
18
19 int x0, y0;
20
21 };
22
23
24
25 class line : public point
26
27 {
28
29 public:
30
31 line(int i=0, int j=0, int m=0, int n=0):point(i, j)
32
33 {
34
35 x1=m; y1=n;
36
37 }
38
39 void set() { cout<<"line::set() called.\n"; }
40
41 void draw() { cout<<"line::draw() called.\n"; }
42
43 protected:
44
45 int x1, y1;
46
47 };
48
49
50
51 class ellipse : public point
52
53 {
54
55 public:
56
57 ellipse(int i=0, int j=0, int p=0, int q=0):point(i, j)
58
59 {
60
61 x2=p; y2=q;
62
63 }
64
65 void set() { cout<<"ellipse::set() called.\n"; }
66
67 void draw() { cout<<"ellipse::draw() called.\n"; }
68
69 protected:
70
71 int x2, y2;
72
73 };
74
75
76
77 void drawobj(point *p)
78
79 {
80
81 p->draw();
82
83 }
84
85
86
87 void setobj(point *p)
88
89 {
90
91 p->set();
92
93 }
94
95
96
97 void main()
98
99 {
100
101 line *lineobj = new line;
102
103 ellipse *elliobj = new ellipse;
104
105 drawobj(lineobj);
106
107 drawobj(elliobj);
108
109 cout<<endl;
110
111 setobj(lineobj);
112
113 setobj(elliobj);
114
115 cout<<"\nRedraw the object...\n";
116
117 drawobj(lineobj);
118
119 drawobj(elliobj);
120
121 }
122