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 }
c++虚函数、子类中调用父类方法的更多相关文章
- python基础----继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法
一.什么是继承 继承是一种创建新的类的方式,在pyth ...
- python基础之类的继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法
一.什么是继承 继承是一种创建新的类的方式,新建的类可以继承自一个或者多个父类,原始类称为基类或超类,新建的类称为派生类或子类. 派生:子类继承了父类的属性,然后衍生出自己新的属性,如果子类衍生出的新 ...
- Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)
Python 在子类中调用父类方法详解(单继承.多层继承.多重继承) by:授客 QQ:1033553122 测试环境: win7 64位 Python版本:Python 3.3.5 代码实践 ...
- 在子类中调用父类的方法super
1.没有super之前,在子类里面需要父类里面的逻辑,但是我们是通过派生(自己定义了一个init,增加了一条line) class vehichle:#定义一个交通工具的类 Country=" ...
- 【Python】Python中子类怎样调用父类方法
python中类的初始化方法是__init__(),因此父类子类的初始化方法都是这个,如果子类不实现这个函数,初始化时调用父类的初始化函数,如果子类实现这个函数,就覆盖了父类的这个函数,既然继承父类, ...
- c++与java中子类中调用父类成员的方法
java中: import java.util.Scanner; public class ClassTest{ public static void main(String args[]){ chi ...
- C++——子类调用父类方法
原创声明:本文系博主原创文章,转载或引用请注明出处. 1. 如果类B是类A的子类,则在类B的成员方法中调用类A的方法时,可以直接以 A::method(paramlist); 来调用. 2. 若子类B ...
- python面向对象的三大特征--继承子类调用父类方法
#在子类中调用父类方法 class Vehicle: country="China" def __init__(self,name,speed,load,power): self. ...
- Odoo(OpenERP) 多个子类重载同一个父类方法的执行顺序及如何调用父类的父类方法
首先说下起因,在修改英国会计模块(没错,就是那个安格鲁撒克逊记账模式!)中不符合中国国情的部分供能时,碰到了一个棘手的问题,简单的说就是B类继承它的父类A并重载了A的方法M,同时C类也继承了A类也重载 ...
随机推荐
- 容器编排系统K8s之HPA资源
前文我们了解了用Prometheus监控k8s上的节点和pod资源,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14287942.html:今天我们来了解下 ...
- 【Jboss】一台服务器上如何部署多个jboss
一台服务器上如何部署多个jboss呢?直接把整个部署环境copy一份到相应的目录下? 这样只是前提,但是启动复制后的jboss就会发现,有很多端口被占用 3873,8080,8009,8443,808 ...
- 【Oracle】Oracle 10g下载路径
ORACLE 10g下载地址 下载方法: 直接复制下面的链接,打开迅雷,自动会识别下载的内容 Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise ...
- PHP反序列化 - Pikachu
概述 序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象: class S{ public $test="pikachu"; } $s ...
- 阿里云 RTC QoS 屏幕共享弱网优化之若干编码器相关优化
屏幕共享是视频会议中使用频率最高的功能之一,但在实际场景中用户所处网络环境复杂,常遇到丢包或者拥塞的情况,所以如何优化弱网环境下的用户体验也成为了音视频通信中重要的一环.本文主要分享阿里云 RTC Q ...
- Windows+.Net Framework+svn+IIS在Jenkins上的自动化部署入门
关于Jenkins的使用及安装,上一篇文章我已经介绍过了,Windows+.NetCore+git+IIS在Jenkins上的自动化部署入门.这篇主要是在jenkins如何安装SVN和MSBuild. ...
- .NET 项目中的单元测试
.NET 项目中的单元测试 Intro "不会写单元测试的程序员不是合格的程序员,不写单元测试的程序员不是优秀的工程师." -- 一只想要成为一个优秀程序员的渣逼程序猿. 那么问题 ...
- hello2 部分代码解析
ResponseServlet.java源码文件 1 @WebServlet("/response") //以@WebServlet注释开头,注释指定相对于上下文根的URL模式, ...
- Spring Boot(IDEA,Gradle)超详细用户管理项目(一)——Hello World
1.构建工具的配置(Gradle):自定义-所有设置:构建.执行.部署-构建工具-Gradle: 设置Gradle用户主目录:(该目录相当于仓库,gradle将下载所需依赖到此目录下),此目录下可新建 ...
- Mybatis参数预编译
Mybatis参数预编译 一.数据库预编译介绍 1.数据库SQL语句编译特性: 数据库接受到sql语句之后,需要词法和语义解析,优化sql语句,制定执行计划.这需要花费一些时间.但是很多情况,我们的一 ...