派生类构造函数与析构函数的构造规则
来源:互联网 发布:永年招聘网络推广专员 编辑:程序博客网 时间:2024/04/28 00:14
1.派生类的构造函数可以不显示式的写出基类的构造函数。例如:
Third()
{e=0;}
此时,系统自动调用基类的无参构造函数(没有,则为默认的构造函数)。也可显示式的指出调用基类的哪一个构造函数。例如:
Third(int x,int y,int z):Second(x,y)
{
e=z;
}
2.要调用的基类构造函数的参数可在派生类的构造函数中明确给出。例如:
Third(int x,int y,int z):Second(x,y)
{
e=z;
}
也可不给出:
Second():First(1,1)
{
c=0;
d=0;
}
还可以对派生类构造函数的参数进行一些交换,再传递给要调用的基类的构造函数,例如:
Second(int x,int y):First(x+1,y+1)
{
c=x;
d=y;
}
一个完整的例子如下:
#include<iostream>using namespace std;class First{public:First(){a=0;b=0;}First(int x,int y){a=x; b=y;}~First(){cout<<"destructing First"<<endl;}void show(){ cout<<"\n a="<<a<<"\n b="<<b<<endl;}private:int a;int b;};class Second:public First{public:Second():First(1,1){c=0;d=0;}Second(int x,int y):First(x+1,y+1){c=x;d=y;}Second(int x,int y,int m,int n):First(m,n){c=x;d=y;}~Second(){cout<<"destructing Second"<<endl;}void show(){First::show();cout<<"c="<<c<<"d="<<d<<endl;}private:int c,d;};class Third:public Second{public:Third(){e=0;}Third(int x,int y,int z):Second(x,y){ e=z;}Third(int x,int y,int z,int m,int n):Second(x,y,m,n){e=z;}~Third(){cout<<"destructing Third"<<endl;}void show(){Second::show();cout<<"e="<<e<<endl;}private:int e;};int main(){First f;f.show();cout<<"======================"<<endl;Second d1;d1.show();cout<<"======================"<<endl;Second d2(10,20,30,40); d2.show();cout<<"====================="<<endl;Second d3(12,13);d3.show();cout<<"======================="<<endl;Third t;t.show();cout<<"======================"<<endl;Third t1(14,15,16);t1.show();cout<<"======================="<<endl;Third t2(50,60,70,80,90);t2.show();return 0;}
- 派生类构造函数与析构函数的构造规则
- 派生类构造函数和析构函数的构造规则
- 派生类构造函数和析构函数的构造规则
- 派生类构造函数规则
- 派生类构造函数规则
- 派生类的构造函数与析构函数
- 派生类的构造函数与析构函数
- 派生类的构造函数与析构函数
- 8.2 派生类的构造函数与析构函数
- 派生类的构造函数与析构函数
- 派生类的构造函数与析构函数
- 派生类构造与析构函数
- 继承与派生3:派生类的构造函数与析构函数
- 虚基类派生的构造函数与析构函数
- 派生类中的构造函数与析构函数
- 派生类的构造函数和析构函数
- 派生类的构造函数和析构函数
- 关于派生类的构造函数和析构函数
- poj 3974
- ELF:From The Programmer's Perspective
- 自己扩展的几个多维数组处理函数
- hdu 4311 Meeting point-1 #manhattan距离
- JLink-GDBServer + eclipse 的调试环境
- 派生类构造函数与析构函数的构造规则
- vmware7.0 安装leopard 10.6
- javascript function、指针及内置对象
- cocos2d获取点击坐标
- poj 2533 最长递增子序列
- Spring JDBC与IOC集成使用
- linux中ELF加载过程分析
- 一些感悟
- C++中参数的持续性,作用域和连接性。