c++虚函数、子类中调用父类方法

时间:2023-03-10 00:33:42
c++虚函数、子类中调用父类方法

c++虚函数、子类中调用父类方法

c++虚函数、子类中调用父类方法

全部 代码:

  1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 using namespace std;
6 class point
7 {
8 private:
9 int x,y,z;
10 public:
11 point(int xx,int yy,int zz) //这个参数的名字尽量不要和类中变量名字重复
12 {
13 x=xx;
14 y=yy;
15 z=zz;
16 }
17 void Show()
18 {
19 printf("x:%d,y:%d,z:%d",x,y,z);
20 }
21
22 };
23 class color:public point
24 {
25 private:
26 string s;
27 public:
28 color(int x,int y,int z,string fp):point(x,y,z)
29 {
30 s=fp;
31 }
32 void Show()
33 {
34 point::Show(); //要调用父类的函数就这样写
35 //除此之外还可以子类和父类返回值参数相同,函数名相同,有virtual关键字,则由对象的类型决定调用哪个函数。
36 cout<<" "<<s;
37 }
38 };
39 int main()
40 {
41 point a(2,3,5);
42 color b(10,20,50,"red");
43 a.Show();
44 printf("\n");
45 b.Show();
46 printf("\n");
47 return 0;
48 }
49
50 #include<stdio.h>
51 #include<string.h>
52 #include<iostream>
53 #include<algorithm>
54 using namespace std;
55 class cellphone
56 {
57 private:
58 string nature;
59 int phone;
60 public:
61 void set_nature(string y)
62 {
63 nature=y;
64 }
65 void get_nature()
66 {
67 cout<<"品牌:"<<nature<<endl;
68 }
69 void set_phone(int y)
70 {
71 phone=y;
72 }
73 void get_phone()
74 {
75 cout<<"电话号码:"<<phone<<endl;
76 }
77 void pickup(int tel)
78 {
79 printf("接听来自%d的电话\n",tel);
80 }
81 void call(int tel)
82 {
83 printf("呼叫号码为%d的电话\n",tel);
84 }
85 };
86 class smartphone:public cellphone
87 {
88 private:
89 int full,sizes;
90 public:
91 void set_full(int y)
92 {
93 full=y;
94 }
95 void get_full()
96 {
97 cout<<"容量:"<<full<<endl;
98 }
99 void set_sizes(int y)
100 {
101 sizes=y;
102 }
103 void get_sizes()
104 {
105 cout<<"屏幕大小:"<<sizes<<endl;
106 }
107 void playmusic(char *mname)
108 {
109 printf("播放音乐:%s",mname);
110 }
111 };
112 int main()
113 {
114 cellphone x;
115 x.pickup(123);
116 x.call(123456);
117 smartphone y;
118 y.set_full(123);
119 y.get_full();
120 y.set_sizes(123);
121 y.get_sizes();
122 return 0;
123 }
124
125 #include<stdio.h>
126 #include<string.h>
127 #include<iostream>
128 #include<algorithm>
129 using namespace std;
130 class base
131 {
132 public:
133 int x,y;
134 base(int xx,int yy)
135 {
136 x=xx;
137 y=yy;
138 }
139 virtual void Show(){} //定义纯虚函数要这样写,而不要像”virtual void Show();“这样
140 };
141 class rectangular:public base
142 {
143 public:
144 rectangular(int x,int y):base(x,y) //这就是调用子类构造函数并传参
145 {
146
147 }
148 void Show()
149 {
150 printf("周长为:%d\n",(x+y)*2);
151 }
152 };
153 int main()
154 {
155 rectangular x(1,2);
156 x.Show();
157
158 return 0;
159 }
160
161 #include<stdio.h>
162 #include<string.h>
163 #include<iostream>
164 #include<algorithm>
165 using namespace std;
166 class geometry
167 {
168 public:
169 virtual void draw(){}
170 };
171 class rectangular:public geometry
172 {
173 public:
174 int x,y;
175 rectangular(int xx,int yy)
176 {
177 x=xx; //高
178 y=yy; //宽
179 }
180 void draw()
181 {
182 for(int i=0;i<y;++i)
183 {
184 printf("*");
185 }
186 printf("\n");
187 if(x<=1) return;
188 for(int i=0;i<x-2;++i)
189 {
190 printf("*");
191 for(int j=0;j<y-2;++j)
192 printf(" ");
193 printf("*\n");
194 }
195 for(int i=0;i<y;++i)
196 {
197 printf("*");
198 }
199 printf("\n");
200 }
201 };
202 class triangle:public geometry
203 {
204 public:
205 int x;
206 triangle(int xx)
207 {
208 x=xx;
209 }
210 void draw()
211 {
212 for(int i=0;i<x-1;++i)
213 {
214 printf("*");
215 for(int j=0;j<i-1;++j)
216 printf(" ");
217 if(i)
218 printf("*");
219 printf("\n");
220 }
221 for(int i=0;i<x;++i)
222 printf("*");
223 printf("\n");
224 }
225 };
226 int main()
227 {
228 rectangular x(2,3);
229 x.draw();
230 printf("\n\n");
231 triangle y(5);
232 y.draw();
233 return 0;
234 }
235
236 #include<stdio.h>
237 #include<string.h>
238 #include<iostream>
239 #include<algorithm>
240 using namespace std;
241 class complexs
242 {
243 public:
244 int x,y,z;
245 complexs(int xx,int yy,int zz)
246 {
247 x=xx;
248 y=yy;
249 z=zz;
250 }
251 friend complexs operator+(complexs& a,complexs& b);
252
253 friend complexs operator-(complexs& a,complexs& b);
254
255 };
256 complexs operator+(complexs& a,complexs& b)
257 {
258 complexs temp(0,0,0); //之前没有给temp传参数,然后就一直报错 no matching function for call to 'complexs::complexs()'|
259 temp.x=a.x+b.x;
260 temp.y=a.y+b.y;
261 temp.z=a.z+b.z;
262 return temp;
263 }
264 complexs operator-(complexs& a,complexs& b)
265 {
266 complexs temp(0,0,0);
267 temp.x=a.x-b.x;
268 temp.y=a.y-b.y;
269 temp.z=a.z-b.z;
270 return temp;
271 }
272 int main()
273 {
274 complexs x(1,2,3);
275 complexs y(2,3,4);
276 complexs z=x+y;
277 printf("%d %d %d\n",z.x,z.y,z.z);
278 z=x-y;
279 printf("%d %d %d\n",z.x,z.y,z.z);
280 return 0;
281 }