实验4 类与数组、指针
实验任务一:
point.hpp
1 #pragma once 2 #include <iostream> 3 using namespace std; 4 class Point { 5 public: 6 Point() : x{ 0 }, y{ 0 } {} 7 Point(int x0, int y0) : x{ x0 }, y{ y0 } {} 8 ~Point() = default; 9 10 int getX() const { return x; } 11 int getY() const; 12 void show() const; 13 void move(int newX, int newY); 14 private: 15 int x, y; 16 }; 17 int Point::getY() const { 18 return y; 19 } 20 void Point::show() const { 21 cout << "(" << x << "," << y << ")" << endl; 22 } 23 void Point::move(int newX, int newY) { 24 x = newX; 25 y = newY; 26 }
task1.cpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <iostream> 3 #include "Point.hpp" 4 #include <vector> 5 using namespace std; 6 7 void test1() { 8 int n; 9 cin >> n; 10 vector<Point>x(n); 11 x.at(0).show(); 12 x.at(0).move(2, 3); 13 x.at(0).show(); 14 } 15 void test2() { 16 int n; 17 cin >> n; 18 vector<Point>x(n); 19 x.at(0).show(); 20 vector<Point>y(x); 21 y.at(0).show(); 22 x.at(0).move(2, 3); 23 x.at(0).show(); 24 y.at(0).show(); 25 26 } 27 int main() { 28 test1(); 29 test2(); 30 return 0; 31 }
运行截图:
总结:这里y复制了x,但对x进行的操作,y并没有体现出来,是浅复制。
实验任务二:
point.hpp
1 #pragma once 2 #include <iostream> 3 using namespace std; 4 class Point { 5 public: 6 Point() : x(0), y(0) { cout << "default construct" << endl; } 7 Point(int x0, int y0) : x(x0), y(y0) { 8 cout << "construct" << endl; 9 } 10 ~Point() { cout << "destruct" << endl; } 11 int getX() const { return x; } 12 int getY() const { return y; } 13 void move(int newX, int newY); 14 void show() { cout << x << "," << y << endl; } 15 private: 16 int x, y; 17 }; 18 void Point::move(int newX, int newY) { 19 cout << "move to " << newX << "," << newY << endl; 20 }
vectorpoint.hpp
1 #pragma once 2 #include <iostream> 3 #include "point.hpp" 4 #include <cassert> 5 6 7 class VectorPoint { 8 public: 9 VectorPoint(int size) : size(size) { 10 cout << "create" << endl; 11 points = new Point[size]; 12 } 13 ~VectorPoint() { 14 cout << "delete" << endl; 15 delete[] points; 16 } 17 Point& element(int index); 18 private: 19 int size; 20 Point* points; 21 }; 22 23 Point& VectorPoint::element(int index) { 24 assert(index >= 0 && index < size); 25 return points[index]; 26 }
task2.cpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <iostream> 3 #include "vectorpoint.hpp" 4 #include "point.hpp" 5 6 #include <cstdlib> 7 8 using namespace std; 9 10 void test1() { 11 int n; 12 cin >> n; 13 VectorPoint x(n); 14 x.element(0).show(); 15 x.element(0).move(1, 2); 16 x.element(0).show(); 17 18 } 19 20 void test2() { 21 int n; 22 cin >> n; 23 VectorPoint x(n); 24 x.element(0).show(); 25 VectorPoint y(x); 26 y.element(0).show(); 27 x.element(0).move(1, 2); 28 x.element(0).show(); 29 y.element(0).show(); 30 } 31 32 int main() { 33 cout << "------" << endl; 34 test1(); 35 36 system("pause"); 37 cout << "------" << endl; 38 test2(); 39 return 0; 40 }
运行截图
实验任务三:
task3_2.cpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <iostream> 3 #include <typeinfo> 4 using namespace std; 5 int main() { 6 int a; 7 int& ra = a; 8 ra = 4; 9 int* pa = &a; 10 *pa = 5; 11 cout << hex << &a << endl; 12 cout << hex << &ra << endl; 13 cout << hex << pa << endl; 14 15 cout << a << endl; 16 cout << ra << endl; 17 cout << *pa << endl; 18 19 cout << typeid(a).name() << endl; 20 cout << typeid(ra).name() << endl; 21 cout << typeid(pa).name() << endl; 22 return 0; 23 }
程序运行截图
实验任务五:
vectorint.hpp
1 #pragma once 2 #include <iostream> 3 #include <cassert> 4 using namespace std; 5 class VectorInt { 6 public: 7 VectorInt(int size) : size(size) { 8 cout << "default construct" << endl; 9 arr = new int[size]; 10 } 11 VectorInt(int size, int value) : size(size) { 12 cout << "construct and value" << endl; 13 arr = new int[size]; 14 for (int i = 0; i < size; i++) 15 arr[i] = value; 16 } 17 VectorInt(const VectorInt& y); 18 ~VectorInt(); 19 int& at(int i); 20 int get_size() const { 21 return size; 22 } 23 friend void output(VectorInt& x); 24 private: 25 int size; 26 int* arr; 27 }; 28 VectorInt::VectorInt(const VectorInt& y) : size(y.size) { 29 cout << "deep copy" << endl; 30 arr = new int[size]; 31 for (auto i = 0; i < size; i++) { 32 arr[i] = y.arr[i]; 33 } 34 } 35 VectorInt::~VectorInt() { 36 cout << "destruct" << endl; 37 delete[] arr; 38 } 39 int& VectorInt::at(int i) { 40 assert(i >= 0 && i < size); 41 return arr[i]; 42 } 43 void output(VectorInt& x) { 44 for (auto i = 0; i < x.size; i++) { 45 cout << x.arr[i] << " "; 46 } 47 cout << endl; 48 }
task5.cpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <iostream> 3 #include "vectorint.hpp" 4 void test() { 5 using namespace std; 6 int n; 7 cin >> n; 8 9 VectorInt x1(n); 10 for (auto i = 0; i < n; ++i) 11 x1.at(i) = i * i; 12 output(x1); 13 VectorInt x2(n, 42); 14 VectorInt x3(x2); 15 output(x2); 16 output(x3); 17 x2.at(0) = 77; 18 output(x2); 19 output(x3); 20 } 21 int main() { 22 test(); 23 }
运行截图:
实验六:
matrix.cpp
1 #pragma once 2 #include <iostream> 3 #include <cassert> 4 using std::cout; 5 using std::endl; 6 class Matrix { 7 public: 8 Matrix(int n); // 构造函数,构造一个n*n的矩阵 9 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 10 Matrix(const Matrix& X); // 复制构造函数,使用已有的矩阵X构造 11 ~Matrix(); //析构函数 12 void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 13 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 14 double& at(int i, int j); //返回矩阵第i行第j列元素的引用 15 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 16 int get_lines() const; //返回矩阵行数 17 int get_cols() const; //返回矩列数 18 void print() const; // 按行打印输出矩阵 19 private: 20 int lines; // 矩阵行数 21 int cols; // 矩阵列数 22 double* p; // 指向存放矩阵数据的内存块的首地址 23 }; 24 Matrix::Matrix(int n) : lines(n), cols(n) { 25 cout << "construct1" << endl; 26 p = new double[n * n](); 27 } 28 Matrix::Matrix(int n, int m) : lines(n), cols(m) { 29 cout << "construct2" << endl; 30 p = new double[n * m](); 31 } 32 Matrix::Matrix(const Matrix& x) { 33 lines = x.lines; 34 cols = x.cols; 35 for (auto i = 0; i < lines ; i++) { 36 for (auto j = 0; j < cols; j++) { 37 p[i * lines + j] = x.p[i * lines + j]; 38 } 39 } 40 } 41 Matrix::~Matrix() { 42 cout << "destruction" << endl; 43 delete[] p; 44 } 45 void Matrix::set(const double* pvalue) { 46 for (auto i = 0; i < lines * cols; i++) { 47 p[i] = *pvalue; 48 pvalue++; 49 } 50 }// 用pvalue指向的连续内存块数据按行为矩阵赋值 51 void Matrix::set(int i, int j, int value) { 52 p[i * cols + j - 1] = value; 53 54 }//设置矩阵第i行第j列元素值为value 55 double& Matrix::at(int i, int j) { 56 assert(i >= 0 && j >= 0 && i < lines&& j < cols); 57 58 return p[i * lines + j]; 59 } //返回矩阵第i行第j列元素的引用 60 double Matrix::at(int i, int j) const { 61 assert(i >= 0 && j >= 0 && i < lines&& j < cols); 62 63 return p[i * lines + j]; 64 } // 返回矩阵第i行第j列元素的值 65 int Matrix::get_lines() const { 66 return lines; 67 } //返回矩阵行数 68 int Matrix::get_cols() const { 69 return cols; 70 } //返回矩列数 71 void Matrix::print() const { 72 for (auto i = 0; i < lines; i++) { 73 for (auto j = 0; j < cols; j++) { 74 cout << p[i * lines + j] << " "; 75 } 76 cout << endl; 77 } 78 } // 按行打印输出矩阵
task6.cpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <iostream> 3 #include "matrix.hpp" 4 void test() { 5 using namespace std; 6 double x[] = { 1, 2, 3, 4, 5, 6 }; 7 Matrix m1(3, 2); // 创建一个3×2的矩阵 8 m1.set(x);// 用一维数组x的值按行为矩阵m1赋值 9 m1.print();// 打印矩阵m1的值 10 cout << "the first line is: " << endl; 11 cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;// 输出矩阵m1第1行两个元素的值 12 cout << endl; 13 Matrix m2(2, 3); 14 m2.set(x); 15 m2.print(); 16 cout << "the first line is: " << endl; 17 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 18 cout << endl; 19 Matrix m3(m2); // 用矩阵m2构造新的矩阵m3 20 m3.set(0, 0, 999); 21 m3.print(); 22 } 23 int main() { 24 test(); 25 }
这一题的代码敲的有问题,看了半天没看出来问题,我后面再来改。