• C++——深拷贝


    要实现深拷贝就需要自己编写拷贝构造函数。

    深拷贝

    #include<iostream>

    using namespace std;

    class Point

    { public:

           Point()

          {   X=Y=0;     cout<<"Default Constructor called."<<endl;     }

           Point(int xx,int yy)

          {   X=xx;     Y=yy;     cout<< "Constructor called."<<endl;     }

           ~Point()

          {   cout<<"Destructor called."<<endl;    }

           int GetX() {return X;}

           int GetY() {return Y;}

              void Move(int x,int y)

                       {  X=x;  Y=y;   }

      private:

           int  X,Y;

    };

    class ArrayOfPoints

    {   public:

         ArrayOfPoints(ArrayOfPoints& pointsArray);

         ArrayOfPoints(int n)

         {   numberOfPoints=n;  points=new Point[n];  }

         ~ArrayOfPoints()

         {   cout<<"Deleting..."<<endl;

             numberOfPoints=0;  delete[] points;    

           }

         Point& Element(int n)

         {  return points[n];  }

       private:

         Point *points;

         int numberOfPoints;

    };

    ArrayOfPoints ::ArrayOfPoints(ArrayOfPoints& pointsArray)//自己写拷贝构造函数,用本类对象的引用作参数

    {   numberOfPoints=pointsArray.numberOfPoints;

        points=new Point[numberOfPoints];

        for (int i=0; i<numberOfPoints; i++)

          points[i].Move(pointsArray.Element(i).GetX(),pointsArray.Element(i).GetY());

    }

    int main()

    {

             int number;

             cout<<"Please enter the number of points:";

             cin>>number;

         ArrayOfPoints pointsArray1(number);    //创建对象数组

         pointsArray1.Element(0).Move(5,10);     //通过指针访问数组元素的成员

         pointsArray1.Element(1).Move(15,20);   //通过指针访问数组元素的成员

         ArrayOfPoints pointsArray2(pointsArray1); //创建对象数组副本

         cout<<"Copy of pointsArray1:"<<endl;

         cout<<"Point_0 of array2: "

             <<pointsArray2.Element(0).GetX()

             <<", "<<pointsArray2.Element(0).GetY()<<endl;

         cout<<"Point_1 of array2: "

             <<pointsArray2.Element(1).GetX()

             <<", "<<pointsArray2.Element(1).GetY()<<endl;

         pointsArray1.Element(0).Move(25,30);     //通过指针访问数组元素的成员

         pointsArray1.Element(1).Move(35,40);   //通过指针访问数组元素的成员

         cout<<"After the moving of pointsArray1:"<<endl;

         cout<<"Point_0 of array2: "

             <<pointsArray2.Element(0).GetX()

             <<", "<<pointsArray2.Element(0).GetY()<<endl;

         cout<<"Point_1 of array2: "

             <<pointsArray2.Element(1).GetX()

             <<", "<<pointsArray2.Element(1).GetY()<<endl;

    } //程序的运行结果如下:

    Please enter the number of points:2

    Default Constructor called.           

    Default Constructor called.

    Default Constructor called.

    Default Constructor called.

    Copy of pointsArray1:

    Point_0 of array2: 5, 10

    Point_1 of array2: 15, 20

    After the moving of pointsArray1:

    Point_0 of array2: 5, 10

    Point_1 of array2: 15, 20

    Deleting...

    Destructor called.

    Destructor called.

    Deleting...

    Destructor called.

    Destructor called.

  • 相关阅读:
    Flutter实战视频-移动电商-35.列表页_上拉加载更多制作
    Flutter实战视频-移动电商-34.列表页_小BUG的修复
    Flutter实战视频-移动电商-33.列表页_子类和商品列表交互效果
    Flutter实战视频-移动电商-32.列表页_小类高亮交互效果制作
    Flutter实战视频-移动电商-31.列表页_列表切换交互制作
    Flutter实战视频-移动电商-30.列表页_商品列表UI界面布局
    Flutter实战视频-移动电商-29.列表页_商品列表数据模型建立
    Flutter实战视频-移动电商-28.列表页_商品列表后台接口调试
    Flutter实战视频-移动电商-27.列表页_现有Bug修复和完善
    Flutter实战视频-移动电商-26.列表页_使用Provide控制子类-2
  • 原文地址:https://www.cnblogs.com/lemaden/p/10238046.html
Copyright © 2020-2023  润新知