c++基类和派生类的构造函数
2016-04-07 11:30
351 查看
1. 顺序
当创建一个派生类的对象时,系统首先自动创建一个基类对象,也就是说,在调用派生类构造函数创建派生类对象之前,系统首先调用基类的构造函数创建基类对象。当派生类对象生命期结束时,首先调用派生类的析构函数,然后调用基类的析构函数。简而言之,就是说,构造函数:基类->派生类。析构函数:派生类->基类。
这个我们完全可以通过一个小程序来说明:
//通过输出就可以看出在创建派生类对象b1时各个函数的调用顺序了
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"A(Based Class) constructor is called"<<endl;
}
~A()
{
cout<<"A(Based Class) destructor is called"<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"B(Derived Class) constructor is called"<<endl;
}
~B()
{
cout<<"B(Derived Class) destructor is called"<<endl;
}
};
int main()
{
B b1;
return 0;
}
2. 通过派生类的构造函数调用基类的构造函数有两种方式,隐式和显式两种。所谓隐式方式就是在派生类的构造函数中不指定对应的基类的构造函数,这个时候调用的是基类的默认构造函数(即含有默认参数值或不带参数的构造函数)。而所谓显式方式,就是在派生类的构造函数中指定要调用的基类的构造函数,并将派生类构造函数的部分参数值传递给基类构造函数。注:除非基类有默认的构造函数,否则必须采用显式调用方式。
下面分别给出一个隐式和显式调用的例子:
隐式调用的例子:
#include <iostream>
using namespace std;
class A
{
public:
A(int x = 0,int y = 0)
{
a = x;
b = y;
}
private:
int a;
int b;
};
//基类A有默认的构造函数,可以隐式调用
class B:public A
{
public:
B(int z = 0)
{
c = z;
}
private:
int c;
};
int main()
{
B b1;
return 0;
}
显示调用的例子:
#include <iostream>
using namespace std;
class A
{
public:
A(int x,int y)
{
a = x;
b = y;
}
private:
int a;
int b;
};
//基类A没有默认的构造函数,其现有的构造函数需要传递参数,通过
//派生类构造函数调用A构造函数时必须如下显式调用
class B:public A
{
public:
B(int x,int y,int z):A(x,y)
{
c = z;
}
private:
int c;
};
int main()
{
B b1(1,2,3);
return 0;
}
当创建一个派生类的对象时,系统首先自动创建一个基类对象,也就是说,在调用派生类构造函数创建派生类对象之前,系统首先调用基类的构造函数创建基类对象。当派生类对象生命期结束时,首先调用派生类的析构函数,然后调用基类的析构函数。简而言之,就是说,构造函数:基类->派生类。析构函数:派生类->基类。
这个我们完全可以通过一个小程序来说明:
//通过输出就可以看出在创建派生类对象b1时各个函数的调用顺序了
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"A(Based Class) constructor is called"<<endl;
}
~A()
{
cout<<"A(Based Class) destructor is called"<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"B(Derived Class) constructor is called"<<endl;
}
~B()
{
cout<<"B(Derived Class) destructor is called"<<endl;
}
};
int main()
{
B b1;
return 0;
}
2. 通过派生类的构造函数调用基类的构造函数有两种方式,隐式和显式两种。所谓隐式方式就是在派生类的构造函数中不指定对应的基类的构造函数,这个时候调用的是基类的默认构造函数(即含有默认参数值或不带参数的构造函数)。而所谓显式方式,就是在派生类的构造函数中指定要调用的基类的构造函数,并将派生类构造函数的部分参数值传递给基类构造函数。注:除非基类有默认的构造函数,否则必须采用显式调用方式。
下面分别给出一个隐式和显式调用的例子:
隐式调用的例子:
#include <iostream>
using namespace std;
class A
{
public:
A(int x = 0,int y = 0)
{
a = x;
b = y;
}
private:
int a;
int b;
};
//基类A有默认的构造函数,可以隐式调用
class B:public A
{
public:
B(int z = 0)
{
c = z;
}
private:
int c;
};
int main()
{
B b1;
return 0;
}
显示调用的例子:
#include <iostream>
using namespace std;
class A
{
public:
A(int x,int y)
{
a = x;
b = y;
}
private:
int a;
int b;
};
//基类A没有默认的构造函数,其现有的构造函数需要传递参数,通过
//派生类构造函数调用A构造函数时必须如下显式调用
class B:public A
{
public:
B(int x,int y,int z):A(x,y)
{
c = z;
}
private:
int c;
};
int main()
{
B b1(1,2,3);
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- PostgreSQL教程(三):表的继承和分区表详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua教程(一):在C++中嵌入Lua脚本
- 浅析Ruby中继承和消息的相关知识
- Lua教程(二):C++和Lua相互传递数据示例
- 设计引导--一个鸭子游戏引发的设计理念(多态,继承,抽象,接口,策略者模式)
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法