任务1:
1 #pragma once 2 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Point { 8 public: 9 Point(int x0 = 0, int y0 = 0); 10 ~Point() = default; 11 12 int get_x() const; 13 int get_y() const; 14 void show() const; 15 void move(int new_x, int new_y); 16 17 private: 18 int x, y; 19 }; 20 21 Point::Point(int x0, int y0): x{x0}, y{y0} { 22 } 23 24 int Point::get_x() const { 25 return x; 26 } 27 28 int Point::get_y() const { 29 return y; 30 } 31 32 void Point::show() const { 33 cout << "(" << x << ", " << y << ")" << endl; 34 } 35 36 void Point::move(int new_x, int new_y) { 37 x = new_x; 38 y = new_y; 39 }
1 #include <iostream> 2 #include "point.hpp" 3 #include <vector> 4 5 using std::vector; 6 using std::cin; 7 8 // 输出vector<Point>对象内所有点的坐标 9 void output(const vector<Point> &v) { 10 for(auto &t: v) 11 t.show(); 12 } 13 14 void test() { 15 int n; 16 cout << "输入动态Point数组类对象中元素个数: "; 17 cin >> n; 18 19 vector<Point> x(n); 20 cout << "x对象中所有点坐标信息: " << endl; 21 output(x); 22 23 vector<Point> y(x); // 基于vector<Point>对象x构建对象y 24 cout << "\nx对象中所有点坐标信息: " << endl; 25 output(y); 26 27 cout << "\n更新x对象......" << endl; 28 x.at(0).move(30, 50); // 更新对象x内索引为0的点对象坐标 29 x.push_back(Point(2, 2)); // 向x对象末尾添加一个点对象 30 31 cout << "\nx对象中所有点坐标信息: " << endl; 32 output(x); 33 cout << "\ny对象中所有点坐标信息: " << endl; 34 output(y); 35 } 36 37 int main() { 38 test(); 39 }
运行测试截图:
问题一:不变化
问题二:深复制
任务2:
1 #pragma once 2 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Point { 8 public: 9 Point(int x0 = 0, int y0 = 0); 10 ~Point() = default; 11 12 int get_x() const; 13 int get_y() const; 14 void show() const; 15 void move(int new_x, int new_y); 16 17 private: 18 int x, y; 19 }; 20 21 Point::Point(int x0, int y0): x{x0}, y{y0} { 22 } 23 24 int Point::get_x() const { 25 return x; 26 } 27 28 int Point::get_y() const { 29 return y; 30 } 31 32 void Point::show() const { 33 cout << "(" << x << ", " << y << ")" << endl; 34 } 35 36 void Point::move(int new_x, int new_y) { 37 x = new_x; 38 y = new_y; 39 }
1 #pragma once 2 3 #include "point.hpp" 4 #include <cassert> 5 #include <iostream> 6 7 class vectorPoint{ 8 public: 9 vectorPoint(int n); 10 ~vectorPoint(); 11 12 int get_size() const; // 获得当前动态数组内元素个数 13 Point& at(int index); // 返回下标为index的元素引用 14 Point& at(int index) const; // 返回下标为index的元素const引用 15 16 private: 17 int size; // 动态数组的大小 18 Point *ptr; 19 }; 20 21 vectorPoint::vectorPoint(int n) : size{n} { 22 ptr = new Point[n]; 23 } 24 25 vectorPoint::~vectorPoint() { 26 delete[] ptr; 27 } 28 29 int vectorPoint::get_size() const { 30 return size; 31 } 32 33 Point& vectorPoint::at(int index) { 34 assert(index >= 0 && index < size); // 宏,在测试模式下工作。如果不满足条件,则程序终止 35 return ptr[index]; 36 } 37 38 Point& vectorPoint::at(int index) const { 39 assert(index >= 0 && index < size); 40 return ptr[index]; 41 }
1 #include "vectorPoint.hpp" 2 #include <iostream> 3 4 // 输出vectorPoint对象内的所有数据 5 void output(const vectorPoint &v) { 6 for(auto i = 0; i < v.get_size(); ++i) 7 v.at(i).show(); 8 } 9 10 // 测试vectorPoint类:构造对象、复制构造对象 11 void test() { 12 using namespace std; 13 14 int n; 15 cout << "输入vectorPoint对象中元素个数: "; 16 cin >> n; 17 18 vectorPoint x(n); 19 cout << "x对象中所有点坐标信息: " << endl; 20 output(x); 21 22 vectorPoint y(x); 23 cout << "\ny对象中所有点坐标信息: " << endl; 24 output(y); 25 26 cout << "\n更新x对象中点坐标信息......" << endl; 27 x.at(0).move(30, 50); 28 x.at(1).move(-1, -1); 29 30 cout << "x对象中所有点坐标信息: " << endl; 31 output(x); 32 33 cout << "\ny对象中所有点坐标信息: " << endl; 34 output(y); 35 } 36 37 int main() { 38 test(); 39 }
运行测试截图:
问题一:发生变化
问题二:浅复刻
问题三:浅复刻