问题:
为类A增加复制构造函数,用下面的main函数测试:
int main()
{
A a("good morning, code monkeys!");
a.output();
A b(a);
b.output();
return 0;
}
代码:
/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
*文件名称:test.cpp
* 作 者:曾建强
* 完成日期:2015年4月15日
* 版 本 号:v1.0
*
* 问题描述:根据所要用的main函数,编写程序
* 输入描述:
* 程序输出:
*/
#include<iostream>
#include<cstring>
using namespace std;
class A
{
private:
char *a;
public:
A(char *aa)
{
a = new char[strlen(aa)+1];
strcpy(a, aa);
}
A(A &b)
{
a = new char[strlen(b.a)+1];
strcpy(a,b.a);
}
~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;
}
运行结果: