项目1-深复制的体验(3)

#include
#include
using namespace std;
class A
{
private:
    char *a;
public:
    A(char *aa)
    {
        a = new char[strlen(aa)+1];  //
        strcpy(a, aa);  //
    }
    ~A()
    {
        delete []a;   //
    }
    void output()
    {
        cout<



 

(3)为类A增加复制构造函数,用下面的main函数测试

int main()
{
    A a("good morning, code monkeys!");
    a.output();
    A b(a);
    b.output();
    return 0;
}


 

#include
#include
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<


项目1-深复制的体验(3)_第1张图片

你可能感兴趣的:(项目1-深复制的体验(3))