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类也重载 ...
随机推荐
- 【Oracle LISTNER】oracle Listener 宕机解决办法
今天想起了很久没用的oracle库,用plsql尝试连接,发现报超时错误,以为是偶然,多次尝试连接,发现还是超时,于是登录到系统中,查看数据库情况,发现正常查询和修改添加,感觉不是数据库问题,查看监听 ...
- CVE-2018-1273 Spring Data Commons 远程命令执行漏洞复现
一.漏洞描述 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架,Spring Data Commons是Spring Data下所有子项目共享的基础框架.Spring Data ...
- vue路由切换和用location切换url的区别
最近的业务涉及到了axios的拦截器,要在request.js里面要根据状态码来跳转页面,这时候我就面对了几种跳转选择: 1.使用location.href='/url'来跳转,简单方便,但是刷新了页 ...
- day128:MySQL进阶:
目录 1.介绍和安装 2.基础管理 2.1 用户管理 2.2 权限管理 2.3 连接管理 2.4 配置管理 3.MySQL的体系结构 4.SQL 5.索引和执行计划 1.介绍和安装 1.1 数据库分类 ...
- 解决ubuntu获取root账号并开通ssh
1.设置root密码 sudo passwd root 2.修改etc/ssh/sshd_config文件 su - root vi /etc/ssh/sshd_config LoginGraceTi ...
- Error Code: 2006 - MySQL 5.7 server has gone away
使用 Navicat 执行 sql 脚本失败 出现 Error Code: 2006 - MySQL server has gone away 原因 当MySQL客户端或mysqld服务器收到大于ma ...
- Base64原理 bits 3->4 8bits/byte-->6bits/byte
实践: window.btoa('a')a YQ==abcdef YWJjZGVmabc YWJjab YWI= https://en.wikipedia.org/wiki/Base64 The Ba ...
- 从tcp层面研究java socket 的使用
本文主要通过wireshark抓包来分析java socket程序的一些细节, 解决以前的一些疑问: 1.当一方的socket先关闭后,另一方尚未关闭的socket 还能做什么? 2.当基于socke ...
- Java内存溢出处理
在解决java内存溢出问题之前,需要对jvm(java虚拟机)的内存管理有一定的认识. jvm管理的内存大致包括三种不同类型的内存区域:Permanent Generation space(永久保存区 ...
- MVC与三层架构解析学习
概要 MVC与三层架构不是简单的相等,二者之间存在一些区别. 今天,看到一位博主总结笔记,借鉴而来,以供以后学习. 将javaweb开发中的MVC(SSM框架)与三级架构比较,来解析二者之间的关系. ...