动态数组类/对象的浅复制/对象深复制

来源:互联网 发布:双色球扫描查询软件 编辑:程序博客网 时间:2024/05/21 09:22

1.将动态数组封装成类

#include <iostream>#include <cassert>using namespace std;class Point{public:Point() :x(0), y(0){cout << "Default Constructor Called." << endl;}Point(int x, int y) :x(x), y(y){cout << "Construct called." << endl;}~Point(){ cout << "Destructor called." << endl; }int GetX() const{ return x; }int GetY() const{ return y; }void move(int newX, int newY){x = newX;y = newY;}private:int x, y;};class ArrayOfPoints{public:ArrayOfPoints(int _size) ;~ArrayOfPoints();Point& element(int index);private:Point *points;  //指向动态数组首地址int size;  //数组大小};ArrayOfPoints::ArrayOfPoints(int _size){size = _size;points = new Point[size];}ArrayOfPoints::~ArrayOfPoints(){cout << "Deleting ..." << endl;delete[] points;}Point& ArrayOfPoints::element(int index){assert(index >= 0 && index < size);return points[index];}int main(){int count;cout << "Please enter the count of points:";cin >> count;ArrayOfPoints points(count);  //创建数组对象points.element(0).move(5, 0);  //访问数组元素的成员points.element(1).move(15, 20);return 0;}


2.对象浅复制

#include <iostream>#include <cassert>using namespace std;class Point{public:Point() :x(0), y(0){cout << "Default Constructor Called." << endl;}Point(int x, int y) :x(x), y(y){cout << "Construct called." << endl;}~Point(){ cout << "Destructor called." << endl; }int GetX() const{ return x; }int GetY() const{ return y; }void move(int newX, int newY){x = newX;y = newY;}private:int x, y;};class ArrayOfPoints{public:ArrayOfPoints(int _size) ;~ArrayOfPoints();Point& element(int index);private:Point *points;  //指向动态数组首地址int size;  //数组大小};ArrayOfPoints::ArrayOfPoints(int _size){size = _size;points = new Point[size];}ArrayOfPoints::~ArrayOfPoints(){cout << "Deleting ..." << endl;delete[] points;}Point& ArrayOfPoints::element(int index){assert(index >= 0 && index < size);return points[index];}int main(){int count;cout << "Please enter the count of points:";cin >> count;ArrayOfPoints pointsArray1(count);  //创建数组对象pointsArray1.element(0).move(5, 0);  //访问数组元素的成员pointsArray1.element(1).move(15, 20);ArrayOfPoints pointsArray2(pointsArray1);  //调用默认的复制构造函数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;return 0;}
对象1的改变引起对象2(浅复制得到)的改变。


3.对象的深复制

#include <iostream>#include <cassert>using namespace std;class Point{public:Point() :x(0), y(0){cout << "Default Constructor Called." << endl;}Point(int x, int y) :x(x), y(y){cout << "Construct called." << endl;}~Point(){ cout << "Destructor called." << endl; }int GetX() const{ return x; }int GetY() const{ return y; }void move(int newX, int newY){x = newX;y = newY;}private:int x, y;};class ArrayOfPoints{public:ArrayOfPoints(int _size) ;~ArrayOfPoints();Point& element(int index);ArrayOfPoints(const ArrayOfPoints& pointArray);private:Point *points;  //指向动态数组首地址int size;  //数组大小};ArrayOfPoints::ArrayOfPoints(int _size){size = _size;points = new Point[size];}ArrayOfPoints::~ArrayOfPoints(){cout << "Deleting ..." << endl;delete[] points;}Point& ArrayOfPoints::element(int index){assert(index >= 0 && index < size);return points[index];}ArrayOfPoints::ArrayOfPoints(const ArrayOfPoints& v){size = v.size;points = new Point[size];for (int i = 0; i < size; i++){points[i] = v.points[i];}}int main(){int count;cout << "Please enter the count of points:";cin >> count;ArrayOfPoints pointsArray1(count);  //创建数组对象pointsArray1.element(0).move(5, 0);  //访问数组元素的成员pointsArray1.element(1).move(15, 20);ArrayOfPoints pointsArray2(pointsArray1);  //调用默认的复制构造函数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;return 0;}

对象1的改变不引起对象2的改变


来自清华MOOC课件

0 0
原创粉丝点击