欢迎使用CSDN-markdown编辑器

来源:互联网 发布:mac air散热 编辑:程序博客网 时间:2024/06/06 18:45

在学习继承的时候看见的派生类的练习,在定义其基类和派生类中都存在着动态存储分配,且定义一般的构造函数、拷贝构造函数、析构函数和赋值重载函数。“` #include

include

include

include

using namespace std;
class B1{
char* a;
public:
B1(): a(new char(‘\0’)){} //类B1的无参构造函数
B1(char* aa){ //类B1的带参构造函数
a=new char[strlen(aa)+1];
strcpy(a,aa);
}
B1(B1& x){ //类B1的拷贝构造函数
a=new char[strlen(x.a)+1];
strcpy(a,x.a);
}
B1& operator=(B1& x){ // 类B1的赋值重载函数
delete []a;
a=new char[strlen(x.a)+1];
strcpy(a,x.a);
return *this;
}
~B1() { delete []a;} //类B1的析构函数
void Output(){ //类B1的输出函数
cout<

0 0