/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:郭永恒
*完成日期:2016年4月27日
*版 本 号:v1.0
*
*问题描述:阅读下面的程序,补足未完成的注释,添加复制构造函数
*/
#include <iostream>
#include <cstring>
using namespace std;
class A
{
public:
A(char *aa)
{
a = new char[strlen(aa)+1];//此处+1是用来保存空字符
strcpy(a,aa);//将字符串从aa指向的内存复制到a指向的内存
}
~A()
{
delete []a;//释放空间
}
A(const A &t)
{
a = new char[strlen(t.a)+1];
strcpy(a,t.a);
}
void output(){cout << a << endl;}
private:
char *a;
};
int main()
{
A a("good morning, code monkeys!");
a.output();
A b(a);
b.output();
return 0;
}
运行结果: