您的位置:首页 > 其它

第九周项目训练1.3

2016-05-19 08:16 197 查看
/*

*Copyright (c) 2016,烟台大学计算机学院

*All rights reserved.

*文件名称 :

*作 者 : 王玙璠

*完成日期 : 2016年5月19号

*版 本 号 : v1.0

*

问题描述:

阅读程序,完成注释。

程序:

#include<iostream>
#include<cstring>
using namespace std;
class A
{
private:
char *a;
public:
A(char *aa)
{
a=new char[strlen(aa)+1]; //(a)这样处理的意义在于:深复制,要求在构造函数中为指针分配其指向内存空间
strcpy(a,aa);  //(b)数据成员a与形式参数aa的关系:把aa所指向的字符串复制到a指向的内存空间
}
~A()
{
delete []a;  //(c)这样处理的意义在于:释放内存,优化程序运行
}
void output()
{
cout<<a<<endl;
}
};
int main()
{
A a("good morning,code monkeys!");
a.output();
A b("good afternoon,codes!");
b.output();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: