阅读背景:

第六周项目1.3 深复制体验——为类A增加复制构造函数

来源:互联网 
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:score.cpp
*作    者:惠睿
*完成日期:2015年4月10日
*版 本 号:v1.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()
    {
        delete []a;
    }
    A(A &b)
    {
        a = new char[strlen(b.a)+1];
        strcpy(a, b.a);
    }
    void output()
    {
        cout<<a<<endl;
    }
};
int main()
{
    A a("good morning, code monkeys!");
    a.output();
    A b(a);
    b.output();
    return 0;
}/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All right



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: