文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>向上类型转换和拷贝构造函数

向上类型转换和拷贝构造函数

时间:2010-11-17  来源:PhoenixZq

 

class Parent{
int i;
public:
Parent(int ii):i(ii){
cout << "Parent(int ii)\n";
}
Parent(const Parent& b):i(b.i){
cout << "Parent(const Parent&)\n";
}
Parent():i(0){cout << "Parent()\n";}
friend ostream& operator<<(ostream& os,const Parent& b){
return os << "Parent: " << b.i << endl;
}
};

class Member{
int i;
public:
Member(int ii):i(ii){
cout << "Member(int ii)\n";
}
Member(const Member& m):i(m.i){
cout << "Member(const Member&)\n";
}
friend ostream& operator<<(ostream& os,const Member& m){
return os << "Member: " << m.i << endl;
}
};
class Child:public Parent{
int i;
Member m;
public:
Child(int ii):Parent(ii),i(ii),m(ii){
cout << "Child(int ii)\n";
}
Child(const Child& c):Parent(c),i(c.i),m(c.m){//无论何时我们在创建自己的拷贝构造函数时,都要正确地调用基类拷贝构造函数
cout << "Child(Child&)\n";
}
friend ostream& operator<<(ostream& os,const Child& c){
return os << (Parent&)c << c.m
<< "Child: " << c.i << endl;
}
};

int main(){
Child c(2);
cout << "calling copy-constructor: " << endl;
Child c2 = c;
cout << "Values in c2:\n" << c2;
return 0;
}

 

相关阅读 更多 +
排行榜 更多 +
超级冒险王安卓版

超级冒险王安卓版

休闲益智 下载
玩具小镇手机版

玩具小镇手机版

休闲益智 下载
这一关特上头手机版

这一关特上头手机版

休闲益智 下载