任務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 }
運行結果截圖:

問題一:發生變化
問題二:淺復刻
問題三:淺復刻
任務3:
1 #include "vectorPoint.hpp" 2 #include <iostream> 3 void output(const vectorPoint &v) { 4 for(auto i = 0; i < v.get_size(); ++i) 5 v.at(i).show(); 6 } 7 void test() { 8 using namespace std; 9 int n; 10 cout << "input vectorPoint how much number: "; 11 cin >> n; 12 vectorPoint x(n); 13 cout << "x information: " << endl; 14 output(x); 15 vectorPoint y(x); 16 cout << "\ny information: " << endl; 17 output(y); 18 cout << "\nupdate x information......" << endl; 19 x.at(0).move(30, 50); 20 x.at(1).move(-1, -1); 21 cout << "x information: " << endl; 22 output(x); 23 cout << "\ny information: " << endl; 24 output(y); 25 } 26 int main() { 27 test(); 28 }
1 #pragma once 2 #include "point.hpp" 3 #include <cassert> 4 #include <iostream> 5 class vectorPoint{ 6 public: 7 vectorPoint(int n); 8 vectorPoint(const vectorPoint &vp); 9 ~vectorPoint(); 10 int get_size() const; 11 Point& at(int index); 12 Point& at(int index) const; 13 private: 14 int size; 15 Point *ptr; 16 }; 17 vectorPoint::vectorPoint(int n) : size{n} { 18 ptr = new Point[n]; 19 } 20 vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new Point[size]} { 21 for(auto i = 0; i < size; ++i) 22 ptr[i] = vp.ptr[i]; 23 } 24 vectorPoint::~vectorPoint() { 25 delete[] ptr; 26 } 27 int vectorPoint::get_size() const { 28 return size; 29 } 30 Point& vectorPoint::at(int index) { 31 assert(index >= 0 && index < size); 32 return ptr[index]; 33 } 34 Point& vectorPoint::at(int index) const { 35 assert(index >= 0 && index < size); 36 return ptr[index]; 37 }
1 #pragma once 2 #include "point.hpp" 3 #include <cassert> 4 #include <iostream> 5 class vectorPoint{ 6 public: 7 vectorPoint(int n); 8 vectorPoint(const vectorPoint &vp); 9 ~vectorPoint(); 10 int get_size() const; 11 Point& at(int index); 12 Point& at(int index) const; 13 private: 14 int size; 15 Point *ptr; 16 }; 17 vectorPoint::vectorPoint(int n) : size{n} { 18 ptr = new Point[n]; 19 } 20 vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new 21 Point[size]} { 22 for(auto i = 0; i < size; ++i) 23 ptr[i] = vp.ptr[i]; 24 } 25 vectorPoint::~vectorPoint() { 26 delete[] ptr; 27 } 28 int vectorPoint::get_size() const { 29 return size; 30 } 31 Point& vectorPoint::at(int index) { 32 assert(index >= 0 && index < size); 33 return ptr[index]; 34 } 35 Point& vectorPoint::at(int index) const { 36 assert(index >= 0 && index < size); 37 return ptr[index]; 38 }
運行結果截圖:

問題1:發生變化
問題2:淺復制
問題3:一般情況下,淺復制沒有任何副作用,但是當類中有指針,并且此指針有動態分配空間,析構函數做了動態內存釋放的處理,往往需要自定義復制構造函數,自行給指針動態分配空間,深復制。
任務4:
1 #include <iostream> 2 using namespace std; 3 void swap1(int &rx, int &ry); 4 void swap2(int *px, int *py); 5 void print(int x, int y); 6 void test() { 7 int x = 3, y = 4; 8 print(x, y); 9 swap1(x, y); 10 print(x, y); 11 12 cout << endl; 13 14 x = 3, y = 4; 15 print(x, y); 16 swap2(&x, &y); 17 print(x, y); 18 } 19 20 int main() { 21 test(); 22 } 23 24 void swap1(int &rx, int &ry) { 25 int t; 26 27 t = rx; rx = ry; ry = t; 28 } 29 30 void swap2(int *px, int *py) { 31 int t; 32 33 t = *px; *px = *py; *py = t; 34 } 35 36 void print(int x, int y) { 37 std::cout << "x = " << x << ", y = " << y << "\n"; 38 }
運行結果截圖:

1 #include <iostream> 2 #include <typeinfo> 3 using namespace std; 4 5 int main() { 6 int a; 7 8 int &ra = a; 9 ra = 4; 10 11 int *pa = &a; 12 *pa = 5; 13 cout << "&a = " << hex << &a << endl; 14 cout << "&ra = " << hex << &ra << endl; 15 cout << "&pa = " << hex << &pa << "\n\n"; 16 cout << "a = " << a << endl; 17 cout << "ra = " << a << endl; 18 cout << "pa = " << hex << pa << endl; 19 cout << "*pa = " << *pa << "\n\n"; 20 cout << "type a: " << typeid(a).name() << endl; 21 cout << "type ra: " << typeid(ra).name() << endl; 22 cout << "type pa: " << typeid(pa).name() << endl; 23 }
運行結果截圖:

1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 template<typename T> 5 void output(const T &x) { 6 for(auto i: x) 7 std::cout << i << ", "; 8 std::cout << "\b\b \n"; 9 } 10 template<typename T> 11 void square1(T &x) { 12 for(auto i: x) // i是普通類型 13 i *= i; 14 } 15 template<typename T> 16 void square2(T &x) { 17 for(auto &i: x) // i是引用類型 18 i *= i; 19 } 20 void test1() { 21 vector<int> x {1, 2, 3, 4, 5}; 22 cout << "動態int型數組對象x內的元素值: "; 23 output(x); 24 cout << "調用函數square1()......" << endl; 25 square1(x); 26 cout << "動態int型數組對象x內的元素值: "; 27 output(x); 28 } 29 void test2() { 30 vector<int> x {1, 2, 3, 4, 5}; 31 cout << "動態int型數組對象x內的元素值: "; 32 output(x); 33 cout << "調用函數square2()......" << endl; 34 square2(x); 35 cout << "動態int型數組對象x內的元素值: "; 36 output(x); 37 } 38 int main() { 39 cout << "測試1: " << endl; 40 test1(); 41 cout << "\n測試2: " << endl; 42 test2(); 43 }
運行結果截圖:

指針用來表示或存儲一個存儲器地址,這個地址的值直接指向存在該地址的對象的值
引用是一種簡單的引用數據類型,用于函數參數和返回值類型,更安全
任務5:
1 #include<iostream> 2 class vectorInt { 3 public: 4 vectorInt(int n); 5 vectorInt(int n, int value); 6 vectorInt(const vectorInt &v); 7 ~vectorInt(); 8 int& at(int n); 9 int& at(int n) const; 10 int get_size() const; 11 private: 12 int size; 13 int *s; 14 }; 15 vectorInt::vectorInt(int n) 16 : size{n}, s{new int[n]} { 17 std::cout << "constructor vectorInt(int n) called. \n"; 18 } 19 vectorInt::vectorInt(int n, int value) 20 : size{n}, s{new int[n]} { 21 for (int i = 0; i < n; i++) 22 s[i] = value; 23 std::cout << "constructor vectorInt(int n, int value) called. \n"; 24 } 25 vectorInt::vectorInt(const vectorInt &v) 26 : size{v.size}, s{new int[v.size]} { 27 for (int i = 0; i < size; ++i) 28 s[i] = v.s[i]; 29 std::cout << "copy constructor called. \n"; 30 } 31 vectorInt::~vectorInt() { 32 delete[] s; 33 std::cout << "destructor called" << std::endl; 34 } 35 int vectorInt::get_size() const { 36 return size; 37 } 38 int& vectorInt::at(int n) { 39 return s[n]; 40 } 41 int& vectorInt::at(int n) const { 42 return s[n]; 43 }
1 #include "vectorInt.hpp" 2 #include <iostream> 3 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 8 void output(const vectorInt &vi) { 9 for(auto i = 0; i < vi.get_size(); ++i) 10 cout << vi.at(i) << ", "; 11 cout << "\b\b \n"; 12 } 13 14 void test() { 15 int n; 16 cout << "輸入vectorInt對象中元素個數: "; 17 cin >> n; 18 19 vectorInt x1(n); // 構造動態int數組對象x1,包含n個元素,不對元素初始化 20 for(auto i = 0; i < n; ++i) 21 x1.at(i) = i*i; 22 cout << "vectorInt對象x1: "; 23 output(x1); 24 25 vectorInt x2(n, 42); // 構造動態int數組對象x1,包含n個元素,每個元素初始值為42 26 cout << "vectorInt對象x2: "; 27 output(x2); 28 vectorInt x3(x2); // 使用x2構造x3 29 cout << "vectorInt對象x3: "; 30 output(x3); 31 32 cout << "更新vectorInt對象x2......\n"; 33 x2.at(0) = 77; 34 x2.at(1) = -999; 35 36 cout << "vectorInt對象x2: "; 37 output(x2); 38 cout << "vectorInt對象x3: "; 39 output(x3); 40 } 41 42 int main() { 43 test(); 44 }
運行結果截圖:

任務6:
1 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 6 using std::cout; 7 using std::endl; 8 9 // 類Matrix的聲明 10 class Matrix { 11 public: 12 Matrix(int n, int m); // 構造函數,構造一個n*m的矩陣 13 Matrix(int n); // 構造函數,構造一個n*n的矩陣 14 Matrix(const Matrix &x); // 復制構造函數, 使用已有的矩陣X構造 15 ~Matrix(); 16 17 void set(const double *pvalue); // 用pvalue指向的連續內存塊數據按行為矩陣賦值 18 void set(int i, int j, double value); // 設置矩陣對象索引(i,j)的元素值為value 19 20 double& at(int i, int j) const; // 返回矩陣對象索引(i,j)的元素引用 21 double& at(int i, int j); // 返回矩陣對象索引(i,j)的元素引用 22 23 int get_lines() const; // 返回矩陣對象行數 24 int get_cols() const; // 返回矩陣對象列數 25 26 void print() const; // 按行打印輸出矩陣對象元素值 27 28 private: 29 int lines; // 矩陣對象內元素行數 30 int cols; // 矩陣對象內元素列數 31 double *ptr; 32 }; 33 34 // 類Matrix的實現 35 Matrix::Matrix(int n, int m) : lines{n}, cols{m} { 36 ptr = new double[n * m]; 37 } 38 Matrix::Matrix(int n) : lines{n}, cols{n} { 39 ptr = new double[n * n]; 40 } 41 Matrix::Matrix(const Matrix &x) : lines{x.lines}, cols{x.cols} { 42 ptr = new double[x.lines * x.cols]; 43 for (int i = 0; i < lines * cols; i++) 44 ptr[i] = x.ptr[i]; 45 } 46 Matrix::~Matrix() { 47 delete[] ptr; 48 } 49 50 void Matrix::set(const double *pvalue) { 51 for (int i = 0, j = 0; i < lines * cols; i++, j++) 52 ptr[i] = pvalue[j]; 53 } 54 void Matrix::set(int i, int j, double value) { 55 ptr[i * lines + j] = value; 56 } 57 58 double& Matrix::at(int i, int j) const { 59 return ptr[i * lines + j]; 60 } 61 double& Matrix::at(int i, int j) { 62 return ptr[i * lines + j]; 63 } 64 65 int Matrix::get_lines() const { 66 return lines; 67 } 68 int Matrix::get_cols() const { 69 return cols; 70 } 71 72 void Matrix::print() const { 73 int s = 0; 74 for (int i = 0; i < lines; i++) { 75 cout << ptr[s]; 76 s++; 77 for (int j = 1; j < cols; j++, s++) 78 cout << ", " << ptr[s]; 79 cout << '\n'; 80 } 81 }
1 #include <iostream> 2 #include "matrix.hpp" 3 4 using namespace std; 5 6 const int N1 = 3; 7 const int N2 = 2; 8 9 // 輸出一個矩陣對象中索引為index對應的行的所有元素值 10 void output(const Matrix &m, int index) { 11 for(auto j = 0; j < m.get_cols(); ++j) 12 cout << m.at(index, j) << ", "; 13 cout << "\b\b \n"; 14 } 15 16 void test() { 17 18 19 double x[N1*N2] = {3, 1, 4, 1, 5, 9}; 20 21 Matrix m1(N1, N2); // 創建一個N1×N2矩陣 22 m1.set(x); // 用一維數組x的值按行為矩陣m1賦值 23 cout << "矩陣對象m1: " << endl; 24 m1.print(); // 打印矩陣m1的值 25 cout << "矩陣對象m1第0行是: " << endl; 26 output(m1, 0); 27 cout << endl; 28 29 Matrix m2(N2, N1); 30 m2.set(x); 31 cout << "矩陣對象m2: " << endl; 32 m2.print(); 33 cout << "矩陣對象m2第0行是: " << endl; 34 output(m2, 0); 35 cout << endl; 36 37 Matrix m3(m2); // 用矩陣m2構造新的矩陣m3 38 m3.set(0, 0, 999); // 講矩陣對象m2索引(0,0)元素設為999 39 cout << "矩陣對象m3:" << endl; 40 m3.print(); 41 cout << endl; 42 43 Matrix m4(2); // 創建一個2*2矩陣對象 44 m4.set(x); // 用一維數組x的值按行為矩陣m4賦值 45 cout << "矩陣對象m4:" << endl; 46 m4.print(); 47 } 48 49 int main() { 50 test(); 51 }
運行結果截圖:
