自考新教材-p240_2

源程序:

#include <iostream>
using namespace std;

class Base
{
private:
int radius, width;
public:
Base()
{
cout << "Base默认构造函数" << endl;
}
Base(int r, int w) :radius(r), width(w)
{
cout << "Base带2个参数的构造函数" << endl;
}
~Base()
{
cout << "Base析构函数" << endl;
}
};

class D_Base
{
private:
int price;
Base ty;
public:
D_Base()
{
cout << "D_Base默认构造函数" << endl;
}
D_Base(int p, int tr, int w) :price(p), ty(tr, w)
{
cout << "D_Base带3个参数的构造函数" << endl;
}
~D_Base()
{
cout << "D_Base析构函数" << endl;
}
};

int main()
{
D_Base();
D_Base db(1,2,3);
system("pause");
return 1;
}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/12260810.html