第九周-深复制体验

代码:

/*
*Copyright (c) 2016, 烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:深复制体验;
*作    者:岳成艳;
*完成日期:2016年4月28号;
*版 本 号:vc++6.0;
*问题描述:为类A增加复制构造函数
*输入描述:无;
*程序输出:运行测试;
*/
#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;
}


运行测试:

第九周-深复制体验_第1张图片

你可能感兴趣的:(编程,C++,vc++6.0)