C++学习笔记(6)——多重继承类对象的构造函数参数的传递方法
来源:互联网 发布:js object 删除元素 编辑:程序博客网 时间:2024/05/13 06:38
本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三二一、小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!
设计基类、派生类、子派生类,定义子派生类的对象,观察参数的传递顺序
代码如下:
/**//************************************************************************
* 多重继承(多层继承)
************************************************************************/
#include <iostream.h>
//基类CBase
class CBase
...{
int a;
public:
CBase(int na)
...{
a=na;
cout<<"CBase constructor! ";
}
~CBase()...{cout<<"CBase deconstructor! ";}
int GetA()...{return a;}
};
//CBase的派生类CDerive
class CDerive : public CBase
...{
int b;
public:
//将参数na传递给基类CBase
CDerive(int nb,int na):CBase(na)
...{
b=nb;
cout<<"CDerive constructor! ";
}
~CDerive()...{cout<<"CDerive deconstructor! ";}
int GetB()...{return b;}
};
//CDerive的派生类CSubDerive
class CSubDerive : public CDerive
...{
int c;
public:
//向直接基类传递参数,不负责向间接基类传递参数
CSubDerive(int nc,int nb,int na):CDerive(nb,na)
...{
c=nc;
cout<<"CSubDerive constructor! ";
}
~CSubDerive()...{cout<<"CSubDerive deconstructor! ";}
int GetC()...{return c;}
void show()
...{
//因为是公有继承,子派生类可以访问其父类和父类的父类的公有成员函数
cout<<"int the subderive class object : ";
cout<<"a = "<<GetA()<<endl;
cout<<"b = "<<GetB()<<endl;
cout<<"c = "<<GetC()<<endl;
}
};
void main()
...{
CSubDerive obj(30,20,10);
cout<<"call the display function of the object: ";
obj.show();
cout<<" call the interface function of the object: ";
cout<<"a = "<<obj.GetA()<<endl;
cout<<"b = "<<obj.GetB()<<endl;
cout<<"c = "<<obj.GetC()<<endl;
}
* 多重继承(多层继承)
************************************************************************/
#include <iostream.h>
//基类CBase
class CBase
...{
int a;
public:
CBase(int na)
...{
a=na;
cout<<"CBase constructor! ";
}
~CBase()...{cout<<"CBase deconstructor! ";}
int GetA()...{return a;}
};
//CBase的派生类CDerive
class CDerive : public CBase
...{
int b;
public:
//将参数na传递给基类CBase
CDerive(int nb,int na):CBase(na)
...{
b=nb;
cout<<"CDerive constructor! ";
}
~CDerive()...{cout<<"CDerive deconstructor! ";}
int GetB()...{return b;}
};
//CDerive的派生类CSubDerive
class CSubDerive : public CDerive
...{
int c;
public:
//向直接基类传递参数,不负责向间接基类传递参数
CSubDerive(int nc,int nb,int na):CDerive(nb,na)
...{
c=nc;
cout<<"CSubDerive constructor! ";
}
~CSubDerive()...{cout<<"CSubDerive deconstructor! ";}
int GetC()...{return c;}
void show()
...{
//因为是公有继承,子派生类可以访问其父类和父类的父类的公有成员函数
cout<<"int the subderive class object : ";
cout<<"a = "<<GetA()<<endl;
cout<<"b = "<<GetB()<<endl;
cout<<"c = "<<GetC()<<endl;
}
};
void main()
...{
CSubDerive obj(30,20,10);
cout<<"call the display function of the object: ";
obj.show();
cout<<" call the interface function of the object: ";
cout<<"a = "<<obj.GetA()<<endl;
cout<<"b = "<<obj.GetB()<<endl;
cout<<"c = "<<obj.GetC()<<endl;
}
分析:
1.运行结果如下:
2.函数执行过程:
上图的序号表示程序执行的流程;真正执行语句的是1、2、3的构造函数调用语句和4、6、8的执行赋值语句。
3.执行第9步返回到主函数中,对象的详细信息:
4.对象的成员函数的地址:
5. 对对象从基类继承来的成员变量的不同的访问方法:
注意:obj.CSubDerive 是子派生类CSubDerive类的构造函数,而obj.CDerive和obj.CBase则是指从基类继承来的成员;对最原始的基类的访问也可以通过obj.CDerive.CBase来访问,他和obj.CBase指的是完全相同的空间。此处的访问方法,指的是在VC环境下进行跟踪运行(调用阶段)的时候使用“watch”工具查看变量时的访问方法。可以通过“watch”工具查看对象的内存空间。
- C++学习笔记(6)——多重继承类对象的构造函数参数的传递方法
- C++学习 (3)--多重继承类对象的构造函数参数的传递方法
- Objective-C学习笔记---类别(实现多重继承的方法)
- Objective-C学习笔记---类别(实现多重继承的方法)
- 多重继承派生类的构造函数
- 多重继承类构造函数的声明
- 多重继承派生类的构造函数
- 多重继承派生类的构造函数
- (1.4.1)虚继承、虚函数继承、多重继承和“含对象成员类的构造顺序”
- c++学习笔记5,多重继承中派生类的构造函数与析构函数的调用顺序(二)
- JavaSE视频学习阶段性总结 三(类的组成,定义,构造方法;对象的创建,内存模型,参数传递)
- c++中子类的继承和调用父类构造函数的方法——学习c++笔记
- Kotlin学习记录(二)—— Kotlin的变量、属性、类、构造函数、继承、方法
- 多重继承 - 构造函数的规则
- 学习笔记——继承中子父类变量,函数,构造函数的特点
- C++学习笔记12——函数的参数传递
- C++学习笔记——函数的参数传递
- 7.27多重继承派生类的构造函数
- RemObjects Sdk 使用简述
- 看看菜鸟怎么实现软件的在线更新
- 什么是实模式、保护模式和虚拟8086方式
- 终于可以弹完“童年的回忆”了!
- 很好用的SQLPLUS命令汇总【转】
- C++学习笔记(6)——多重继承类对象的构造函数参数的传递方法
- Java-关于static
- 关于在javascript 和C#进行base64编码解码的问题
- 海
- 转: Source Insight使用教程
- C++学习笔记(7)——多基类继承的构造函数的调用
- 使用ObjectListDataProvider
- 用VB模拟键盘事件的N种方法
- 汤唯遭“封杀”被证实 广电总局:对事不对人