/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:呼亚萍
* 完成日期:2015年4月12日
* 版 本 号:v1.0
*
* 问题描述:阅读下面的程序,补足未完成的注释
* 程序输入:相应的程序
* 程序输出:对应得结果
*/
#include<iostream>
#include<cstring>
using namespace std;
class A
{
public:
A(char *aa)
{
a=new char[strlen(aa)+1];//这样处理的意义在于:深复制,要求在构造函数中为指针分配指向的内存空间
strcpy(a,aa);//数据成员a与形式参数aa的关系:把aa所指向的字符串复制到a指向的内存空间
}
~A()
{
delete []a;//这样处理的意义在于:释放空间,提高效率
}
void output()
{
cout<<a<<endl;
}
private:
char *a;
};
int main()
{
A a("good morning,code monkeys!");
a.output();
A b("good afternoon,codes!");
b.output();
return 0;
}
运算结果:
知识点总结:
深复制的理解
学习心得:
指针数据成员必须用深复制