问题及代码:
/*。
*Copyright(c)2014,烟台大学计算机学院
*All right reserved,
*文件名:test.cpp
*作者:liu_feng_zi_
*完成日期:2015年4月15日
*版本号:v1.0
*问题描述:为类A增加复制构造函数
*输入描述:
*程序输出:
*/
#include<iostream>
#include<cstring>
using namespace std;
class A
{
private:
char *a;
public:
A(const A &c)
{
a=new char [strlen(c.a)+1];
strcpy(a,c.a);
}
A(char *aa)
{
a = new char[strlen(aa)+1];
strcpy(a, aa);
}
~A()
{
delete []a;
}
void output()
{
cout<<a<<endl;
}
};
int main()
{
A a("good morning, code monkeys!");
a.output();
A b(a);
b.output();
return 0;
}
运行结果: