源程序:
#include<iostream>
using namespace std;
class BaseClass
{
public:
int v1, v2;
BaseClass();
BaseClass(int, int);
~BaseClass();
};
BaseClass::BaseClass()
{
cout << "BaseClass 无参构造函数" << endl;
}
BaseClass::BaseClass(int m, int n)
{
v1 = m;
v2 = n;
cout << "BaseClass 带2个参构造函数" << endl;
}
BaseClass::~BaseClass()
{
cout << "BaseClass 析构函数" << endl;
}
class DerivedClass :public BaseClass
{
public:
int v3;
public:
DerivedClass();
DerivedClass(int);
DerivedClass(int, int, int);
~DerivedClass();
};
DerivedClass::DerivedClass()
{
cout << "DerivedClass 无参构造函数" << endl;
}
DerivedClass::DerivedClass(int k) :v3(k)
{
cout << "DerivedClass 带1个参构造函数" << endl;
}
DerivedClass::DerivedClass(int m, int n, int k) : BaseClass(m, n), v3(k)
{
cout << "DerivedClass 带3个参构造函数" << endl;
}
DerivedClass::~DerivedClass()
{
cout << "DerivedClass 析构函数" << endl;
}
int main()
{
cout << "无参对象的创建" << endl;
BaseClass baseCla;
DerivedClass derivedCla;
system("pause");
return 0;
}
运行结果:
析构过程没有显示出来,析构过程与构造过程相反