#include <iostream>
using namespace std;
class CBase
{
int a;
public:
CBase(int a_=0) :a(a_)
{
cout << "constructor_of_CBase is called!" << endl;
cout << "a=" << a << endl;
}
CBase(CBase& c)
{
cout << "CBase::copy constructor called" << endl;
}
CBase& operator=(const CBase& b)
{
cout << "CBase::opeartor= called" << endl;
this->a = b.a;
return *this;
}
};
class CDerived:public CBase
{
/*在此之前,已经从CBase那里继承过来所有的成员.
(生成一个CDerived对象当然也优先调用基类CBase的构造函数.*/
int b;
public:
CDerived(int a_=0,int b_ = 0) :b(b_),CBase(a_)
{
cout << "\tconstructor_of_CDerived is called!" << endl;
cout << "\t\tb=" << b << endl;
}
CDerived(CDerived& objBeCopyed)
{
cout << "copy_constructor_of_CDerived is called!" << endl;
*this = objBeCopyed;
}
};
int main()
{
CDerived
d1(1,1),
d2(2,2);
d2 = d1;
CDerived d3(d2);
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-1768.html