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: 修饰的成员变量方法,在类的内部使用 ,在继承的子类中可用 ...
随机推荐
- 【DEBUG】不能将参数 1 从“CString”转换为“const char *”
1. 在vc6.0下用CString str;num = atoi(str);就可以顺利取到num: 但是同样代码拿到vs2008就报错,error C2664: "atoi": ...
- [bzoj1935][Shoi2007]Tree 园丁的烦恼 _树状数组
Tree 园丁的烦恼 bzoj-1935 Shoi-2007 题目大意:给定平面上的$n$个点,$m$次查询矩形点个数. 注释:$1\le n,m\le 5\cdot 10^5$. 想法:静态二维数点 ...
- Spring MVC-表单(Form)标签-单选按钮(RadioButton)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_radiobutton.htm 说明:示例基于Spring MVC 4.1.6. ...
- POJ 2960
也算是一道模板题吧,只需按照SG函数的定义求出每个值的SG,然后异或就可以了. #include <iostream> #include <cstdio> #include & ...
- JCE, Java Cryptography Extension
JCE, Java Cryptography Extension Java 8 JCE下载地址: http://www.oracle.com/technetwork/java/javase/downl ...
- zippo打火机的特点:
1. 耐寒.零下30多度环境下仍然能打火 2.抗压.被1.5吨的汽车压过之后仍然能打火 3.抗风,在7级下面的风速中火焰都不会熄灭 4.防水,放入水中.取出来之后任然能打火
- MySql免安装版l配置方法
初次接触mysql,折腾了一天,总是安装不成功,服务启动不了.后来从官网下载了ZIP Archive版,不用安装,直接把它解压到磁盘,做一些简单的配置就可以. 软件下载地址:http://dev.my ...
- xpath元素查找提示is not clickable
1.用xpath可以在chrome找到 $x("//mandatory-config-dialog[@is-show='isShowMandatoryConfig']/div/div[2]/ ...
- CentOS-6.5安装配置Tomcat-7
https://my.oschina.net/u/593517/blog/304483 http://blog.csdn.net/lgh0824/article/details/51194116 摘要 ...
- luogu2149 [SDOI2009] Dlaxia的路线
题目大意 在一个无向图中,定义两个点s,t的最短路径子图为一个极大边集,对于该边集内的所有有向边e,总存在一条起点为s,终点为t且经过边e的路径,使得该路径长度为s到t的最短路径长度.现给出一个无向图 ...