问题及代码
ALL rights reserved.
*文件名称: 初学对象6
作者:李长鸿
*完成时间:2015.4.15
*问题描述: 阅读程序
*/
#include<iostream>
#include<cstring>
using namespace std;
class A
{
private:
char *a;
public:
A(char *aa)
{
a = new char[strlen(aa)+1]; //(a)这样处理的意义在于:给数组a分配了一段空间,使它名花有主,不做孤魂野鬼找不到家;
strcpy(a, aa); //(b)数据成员a与形式参数aa的关系:a赋值为aa,相等关系;
}
~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;
}