全部 代码:

  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 }

c++虚函数、子类中调用父类方法的更多相关文章

  1. python基础----继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法

    一.什么是继承                                                                          继承是一种创建新的类的方式,在pyth ...

  2. python基础之类的继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法

    一.什么是继承 继承是一种创建新的类的方式,新建的类可以继承自一个或者多个父类,原始类称为基类或超类,新建的类称为派生类或子类. 派生:子类继承了父类的属性,然后衍生出自己新的属性,如果子类衍生出的新 ...

  3. Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)

    Python 在子类中调用父类方法详解(单继承.多层继承.多重继承)   by:授客 QQ:1033553122   测试环境: win7 64位 Python版本:Python 3.3.5 代码实践 ...

  4. 在子类中调用父类的方法super

    1.没有super之前,在子类里面需要父类里面的逻辑,但是我们是通过派生(自己定义了一个init,增加了一条line) class vehichle:#定义一个交通工具的类 Country=" ...

  5. 【Python】Python中子类怎样调用父类方法

    python中类的初始化方法是__init__(),因此父类子类的初始化方法都是这个,如果子类不实现这个函数,初始化时调用父类的初始化函数,如果子类实现这个函数,就覆盖了父类的这个函数,既然继承父类, ...

  6. c++与java中子类中调用父类成员的方法

    java中: import java.util.Scanner; public class ClassTest{ public static void main(String args[]){ chi ...

  7. C++——子类调用父类方法

    原创声明:本文系博主原创文章,转载或引用请注明出处. 1. 如果类B是类A的子类,则在类B的成员方法中调用类A的方法时,可以直接以 A::method(paramlist); 来调用. 2. 若子类B ...

  8. python面向对象的三大特征--继承子类调用父类方法

    #在子类中调用父类方法 class Vehicle: country="China" def __init__(self,name,speed,load,power): self. ...

  9. Odoo(OpenERP) 多个子类重载同一个父类方法的执行顺序及如何调用父类的父类方法

    首先说下起因,在修改英国会计模块(没错,就是那个安格鲁撒克逊记账模式!)中不符合中国国情的部分供能时,碰到了一个棘手的问题,简单的说就是B类继承它的父类A并重载了A的方法M,同时C类也继承了A类也重载 ...

随机推荐

  1. TCP/IP五层模型-应用层-DNS协议

    ​1.定义:域名解析协议,把域名解析成对应的IP地址. 2.分类:①迭代解析:DNS所在服务器若没有可以响应的结果,会向客户机提供其他能够解析查询请求的DNS服务器地址,当客户机发送查询请求时,DNS ...

  2. kubernets之DaemonSet

    一  k8s资源之DaemonSet 1.1 介绍认识DaemonSet DaemonSet可以理解为一种比较特殊的RS,DaemonSet的作用是永远保持被指定的节点只运行一个pod的副本,可用作集 ...

  3. mysql:如何解决数据修改冲突(事务+行级锁的实际运用)

    摘要:最近做一个接诊需求遇到一个问题,假设一个订单咨询超过3次就不能再接诊,但如果两个医生同时对该订单进行咨询,查数据库的时候查到的接诊次数都是2次,那两个医生都能接诊,所谓接诊可以理解为更新了接诊次 ...

  4. openpose c++ 配置教程 + python api

    之前有介绍过基于tensorflow的openpose版本安装,但是我觉得没有caffe框架那么好用,很多功能也实现不了,比如调节net_resolution的调节,通过调节分辨率来提高检测的精确性和 ...

  5. 入门OJ:八中生成树2

    题目描述 八中里面有N个建设物,M条边.对于这种要建最小生成树的问题,你应该很熟练了.现在老大决定降低某条边的费用,然后这条边必须要被选中,因为这条路他每天都要走,自然......问选了这条边后是否可 ...

  6. saltstack 服务器批量管理

    学习saltstack 服务器批量管理 1.saltstack 简介 SaltStack是一个开源的.新的基础平台管理工具,使用Python语言开发,同时提供Rest API方便二次开发以及和其他运维 ...

  7. spring源码分析之玩转ioc:bean初始化和依赖注入(一)

    最近赶项目,天天加班到十一二点,终于把文档和代码都整完了,接上继续整. 上一篇聊了beanProcess的注册以及对bean的自定义修改和添加,也标志着创建bean的准备工作都做好了,接下来就是开大招 ...

  8. TCP半连接队列和全连接

    概述   如上图所示, 在TCP三次握手中,服务器维护一个半连接队列(sync queue) 和一个全连接队列(accept queue). 当服务端接收到客户端第一次SYN握手请求时,将创建的req ...

  9. makefile自动生成学习

    https://www.cnblogs.com/jrglinux/p/6964169.html 关键是如何写Makefile.am  其他的交给 自动工具完成 添加一个 很好的博客 学习下 https ...

  10. XV6学习(9)Lab cow: Copy-on-write fork

    代码在github上.总体来说如果理解了COW机制的话,这个实验的完成也没有很复杂. 这一个实验是要完成COW(copy on write)fork.在原始的XV6中,fork函数是通过直接对进程的地 ...