09--c++ 类的继承与派生
c++ 类的继承与派生
一、基本概念


#include <iostream>
#include <time.h>
using namespace std; class B1
{
public:
B1(int i)
{
cout<<"constructing B1 "<<i<<endl;
}
}; class B2
{
public:
B2(int j)
{
cout<<"constructing B2 "<<j<<endl;
}
}; class B3
{
public:
B3()
{
cout<<"constructing B3"<<endl;
}
}; class C: public B2, public B1, public B3
{
public:
C(int a, int b, int c, int d):B1(a), memberB2(d), memberB1(c),B2(b)
{ }
private:
B1 memberB1;
B2 memberB2;
B3 memberB3;
}; int main()
{
C obj(1,2,3,4); return 0;
}


#include <iostream>
#include <time.h>
using namespace std; class B1
{
public:
B1(int i)
{
cout<<"constructing B1 "<<i<<endl;
}
~B1()
{
cout<<"destructing B1"<<endl;
}
}; class B2
{
public:
B2(int j)
{
cout<<"constructing B2 "<<j<<endl;
}
~B2()
{
cout<<"destructing B2"<<endl;
}
}; class B3
{
public:
B3()
{
cout<<"constructing B3"<<endl;
}
~B3()
{
cout<<"destructing B3"<<endl;
}
}; class C: public B2, public B1, public B3
{
public:
C(int a, int b, int c, int d):B1(a), memberB2(d), memberB1(c),B2(b)
{ }
private:
B1 memberB1;
B2 memberB2;
B3 memberB3;
}; int main()
{
C obj(1,2,3,4); return 0;
}


#include <iostream>
#include <time.h>
using namespace std; class B1
{
public:
int nV;
void fun()
{
cout<<"member of B1 "<<nV<<endl;
}
}; class B2
{
public:
int nV;
void fun()
{
cout<<"member of B2 "<<nV<<endl;
}
}; class D1: public B1, public B2
{
public:
int nV;
void fun()
{
cout<<"member of D1 "<<nV<<endl;
}
}; int main()
{
D1 d1;
d1.nV = 1;
d1.fun();
d1.B1::nV = 2;
d1.B1::fun();
d1.B2::nV = 3;
d1.B2::fun(); return 0;
}


#include <iostream>
#include <time.h>
using namespace std; class B0
{
public:
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
}; class B1:public B0
{
public:
int nV1;
}; class B2:public B0
{
public:
int nV2;
}; class D1:public B1, public B2
{
public:
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
}; int main()
{
D1 d1;
d1.B1::nV = 2;
d1.B1::fun();
d1.B2::nV = 3;
d1.B2::fun(); return 0;
}


#include <iostream>
#include <time.h>
using namespace std; class B0
{
public:
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
}; class B1:virtual public B0
{
public:
int nV1;
}; class B2:virtual public B0
{
public:
int nV2;
}; class D1:public B1, public B2
{
public:
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
}; int main()
{
D1 d1;
d1.nV = 2;
d1.fun(); return 0;
}


#include <iostream>
#include <time.h>
using namespace std; class B0
{
public:
B0(int n)
{
nV = n;
}
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
}; class B1:virtual public B0
{
public:
B1(int a):B0(a)
{
}
int nV1;
}; class B2:virtual public B0
{
public:
B2(int a):B0(a)
{
}
int nV2;
}; class D1:public B1, public B2
{
public:
D1(int a):B0(a), B1(a), B2(a)
{
}
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
}; int main()
{
D1 d1(1);
d1.nV = 2;
d1.fun(); return 0;
}

才是真正的调用了B0构造函数。




#include <iostream>
#include <time.h>
using namespace std; class B0
{
public:
void display()
{
cout<<"B0::display()"<<endl;
}
}; class B1:public B0
{
public:
void display()
{
cout<<"B1::display()"<<endl;
}
}; class B2:public B0
{
public:
void display()
{
cout<<"B2::display()"<<endl;
}
}; void fun(B0 *ptr)
{
ptr->display();
} int main()
{
B0 b0;
B1 b1;
B2 b2;
fun(&b0);
b0 = b1;
fun(&b0);
b0 = b2;
fun(&b0); return 0;
}

输出结果为:
B0::display()
B0::display()
B0::display()
通过这种赋值兼容后,每次调用的同名函数都是基类的同名函数,如果想调用派生类的,则需要使用虚函数。
作者:凡程子
出处:http://www.cnblogs.com/fzhe/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
09--c++ 类的继承与派生的更多相关文章
- [C++]类的继承与派生
继承性是面向对象程序设计的第二大特性,它允许在既有类的基础上创建新类,新类可以继承既有类的数据成员和成员函数,可以添加自己特有的数据成员和成员函数,还可以对既有类中的成员函数重新定义.利用类的继承和派 ...
- 模块的封装之C语言类的继承和派生
[交流][微知识]模块的封装(二):C语言的继承和派生 在模块的封装(一):C语言的封装中,我们介绍了如何使用C语言的结构体来实现一个类的封装,并通过掩码结构体的方式实 现了类成员的保护.这一部分,我 ...
- C++学习笔记:07 类的继承与派生
课程<C++语言程序设计进阶>清华大学 郑莉老师) 基本概念 继承与派生的区别: 继承:保持已有类的特性而构造新类的过程称为继承. 派生:在已有类的基础上新增自己的特性(函数方法.数据成员 ...
- Day 5-2 类的继承和派生,重用
类的继承 派生 在子类中重用父类 组合 抽象类 定义: 继承指的是类与类之间的关系,是一种什么“是”什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创建新类的方式,在python中,新 ...
- 4-13 object类,继承和派生( super) ,钻石继承方法
1,object 类 object class A: ''' 这是一个类 ''' pass a = A() print(A.__dict__) # 双下方法 魔术方法 创建一个空对象 调用init方法 ...
- Python基础(16)_面向对象程序设计(类、继承、派生、组合、接口)
一.面向过程程序设计与面向对象程序设计 面向过程的程序设计:核心是过程,过程就解决问题的步骤,基于该思想设计程序就像是在设计一条流水线,是一种机械式的思维方式 优点:复杂的问题的简单化,流程化 缺点: ...
- 对C++类的继承和派生的理解
C++中的继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承类似,例如儿子继承父亲的财产. 1.继承(Inheritance)可以理解为一个类从另一个类获取成员变量和成员函数的过程. ...
- Python3 面向对象-类的继承与派生
1.什么是继承? 继承是一种创建新类的方式,新建的类可以继承一个或多个父类(python支持多继承),父类可称为基类或超类,新建的类称为派生类和或子类. 子类会遗传父类的属性,从而解决代码重用问题. ...
- C++——类的继承(派生)
类的继承就是子类可以拥有父类的成员变量和成员函数 //public 修饰的成员变量 方法 在类的内部 类的外部都能使用//protected: 修饰的成员变量方法,在类的内部使用 ,在继承的子类中可用 ...
随机推荐
- Java 注解之总结
注解是Spring和Mybatis框架所大量使用的技术,要想掌握框架相关技术,注解是必须要掌握的. 掌握注解的优势: 1.能够读懂别人写的代码,特别是框架相关的代码. 2.本来可能需要很多配置文件,需 ...
- redis 初学
1.网站:http://redis.cn/ 2.下载安装和配置 http://www.tuicool.com/articles/aQbQ3u 3.简述redis http://www.jb51.net ...
- [bzoj1455]罗马游戏_左偏树_并查集
罗马游戏 bzoj-1455 题目大意:给你n个人,2种操作,m次操作:1.将i号士兵所在的集合的最小值删除 2.合并i和j两个士兵所在的团体 注释:$1\le n\le 10^6$,$1\le m ...
- 洛谷 P1186 玛丽卡
P1186 玛丽卡 题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道 ...
- MRO 方法解释顺序
MRO是用在多重继承中的.考虑这种情况,整个环境中父类是两个 P1,P2 子类是两个 C1,C2 而 孙子类是G1. 我们知道 G1会从 P1,P2,C1,C2中继承属性,但是如果有多个属性重名,那么 ...
- Apache Traffic Server 5.3.1公布
本文来源于我在InfoQ中文站翻译的文章,原文地址是:www.infoq.com/cn/news/2015/07/traffic-server-5.3.1-release 近日,Apache软件基金会 ...
- C语言函数--H
函数名: harderr 功 能: 建立一个硬件错误处理程序 用 法: void harderr(int (*fptr)()); 程序例: /*This program will trap disk ...
- xpath元素查找提示is not clickable
1.用xpath可以在chrome找到 $x("//mandatory-config-dialog[@is-show='isShowMandatoryConfig']/div/div[2]/ ...
- hdu4430之枚举+二分
Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- web认证方案
web构建在http之上,而它又是无状态协议,如何控制用户访问服务器上的受限资源呢? 最原始你想法通过http基本认证,每次发请求时都向后台传递用户名密码信息,服务器每次收到请求后都先验证用户是否合法 ...