class customer
{
public:
customer(string s):name_(s){}
customer(const customer & rhs):name_(rhs.name_)
{
cout<<"customer(const customer & rhs) run"<<endl;
}
customer& operator=(const customer & rhs)
{
name_=rhs.name_;
cout<<"customer& operator=(const customer & rhs)"<<endl;
return *this;
}
void print() const
{
cout<<name_<<endl;
}
protected:
string name_;
};
class pcustomer:public customer
{
public:
pcustomer(string s,int n):customer(s),p_(n){}
pcustomer(const pcustomer& rhs):
customer(rhs),p_(rhs.p_)
{
cout<<"pcustomer(const pcustomer& rhs run"<<endl;
}
pcustomer& operator=(const pcustomer& rhs)
{
cout<<" pcustomer& operator=(const pcustomer& rhs)"<<endl;
customer::operator=(rhs);
p_=rhs.p_;
return *this;
}
void print() const
{
customer::print();
cout<<p_<<endl;
}
private:
int p_;
};
int main(int argc, const char *argv[])
{
// pcustomer p1("hello",0);
// pcustomer p2("world",1);
// pcustomer p3(p1);
// p1=p2;
// p3.print();
return 0;
}
注意base类里拷贝构造函数的调用
小白学c++之effective c++条款12实现代码
最新推荐文章于 2022-06-04 22:13:31 发布