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: 修饰的成员变量方法,在类的内部使用 ,在继承的子类中可用 ...
随机推荐
- hdu2013 蟠桃记【C++】
蟠桃记 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- (42)Spring Boot多数据源【从零开始学Spring Boot】
我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...
- Python 1 初识python
1.Python介绍 Python是一种高级语言,与JAVA C# 等同.可以编写各种应用程序,每种语言都有其合适的应用场景.而Python 的优势在于更加人性化.简便的语法规则,以及针对各种具体场景 ...
- 在django里用xlwt作EXCEL导出功能
参考了以前kevin的代码,搞起来快呀,哈哈哈,,阿里亚多.... 代码的意思比较明白,不注释,几乎自解释... 就是定义EXCEL时,比较硬生生的,一步一步没有快捷的方法. 另外就是取时间出来时,如 ...
- hdu_1048_The Hardest Problem Ever_201311052052
The Hardest Problem Ever Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- F2BPM中关于工作流引擎驳回设计
1.1 关于驳回 驳回,在有的应用中叫“回退”.驳回是中国特色的一种方式,驳回在流程图上也没有迁移线的表达经常也是隐性的,比如申请经费可能由于资料不足被驳回来补充资料,像这样的例子有非常多,也很常见. ...
- Oracle Internals 相关站点
http://oracle-internals.com/blog/links/ http://coll15.mapyourshow.com/6_0/sessions/session-details.c ...
- C语言实现的lisp解析器介绍
近期.由于Perl而学习函数式编程, 再进一步学习lisp, 真是一学习就发现自己的渺小. 无意中找到了一个很easy的C语言版的, lisp解析器. 代码非常短, 却非常见功底, 涨姿势了. 附带还 ...
- object-c 不定參数的遍历和原理
object-c接收随意类型的參数: /** * 接收String类型的多个參数 * @param firsParam 第一个參数 */ -(void)TestString:(NSString*)fi ...
- 修改input:file样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...