实验三 类与数组、指针
任务1
1 #pragma once 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 class Point { 6 public: 7 Point(int x0 = 0, int y0 = 0); 8 ~Point() = default; 9 int get_x() const; 10 int get_y() const; 11 void show() const; 12 void move(int new_x, int new_y); 13 private: 14 int x, y; 15 }; 16 Point::Point(int x0, int y0) : x{ x0 }, y{ y0 } { 17 } 18 int Point::get_x() const { 19 return x; 20 } 21 int Point::get_y() const { 22 return y; 23 } 24 void Point::show() const { 25 cout << "(" << x << ", " << y << ")" << endl; 26 } 27 void Point::move(int new_x, int new_y) { 28 x = new_x; 29 y = new_y; 30 }
1 #include <iostream> 2 #include "point.hpp" 3 #include <vector> 4 using std::vector; 5 using std::cin; 6 // 输出vector<Point>对象内所有点的坐标 7 void output(const vector<Point>& v) { 8 for (auto& t : v) 9 t.show(); 10 } 11 void test() { 12 int n; 13 cin >> n; 14 vector<Point> x(n); 15 cout << "x对象中所有点坐标信息: " << endl; 16 output(x); 17 vector<Point> y(x); // 基于vector<Point>对象x构建对象y 18 cout << "\nx对象中所有点坐标信息: " << endl; 19 output(y); 20 cout << "\n更新x对象: " << endl; 21 x.at(0).move(30, 50); // 更新对象x内索引为0的点对象坐标 22 x.push_back(Point(2, 2)); // 向x对象末尾添加一个点对象 23 cout << "\nx对象中所有点坐标信息: " << endl; 24 output(x); 25 cout << "\ny对象中所有点坐标信息: " << endl; 26 output(y); 27 } 28 int main() { 29 test(); 30 }

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

任务3
1 #pragma once 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 class Point { 6 public: 7 Point(int x0 = 0, int y0 = 0); 8 ~Point() = default; 9 int get_x() const; 10 int get_y() const; 11 void show() const; 12 void move(int new_x, int new_y); 13 14 private: 15 int x, y; 16 }; 17 18 Point::Point(int x0, int y0) : x{ x0 }, y{ y0 } { 19 } 20 21 int Point::get_x() const { 22 return x; 23 } 24 25 int Point::get_y() const { 26 return y; 27 } 28 29 void Point::show() const { 30 cout << "(" << x << ", " << y << ")" << endl; 31 } 32 33 void Point::move(int new_x, int new_y) { 34 x = new_x; 35 y = new_y; 36 }
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); // 返回下标为index的元素引用 12 Point& at(int index) const; // 返回下标为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 #include "vectorPoint.hpp" 2 #include <iostream> 3 // 输出vectorPoint对象内的所有数据 4 void output(const vectorPoint& v) { 5 for (auto i = 0; i < v.get_size(); ++i) 6 v.at(i).show(); 7 } 8 // 测试vectorPoint类:构造对象、复制构造对象 9 void test() { 10 using namespace std; 11 int n; 12 cout << "输入vectorPoint对象中元素个数: "; 13 cin >> n; 14 vectorPoint x(n); 15 cout << "x对象中所有点坐标信息: " << endl; 16 output(x); 17 vectorPoint y(x); 18 cout << "\ny对象中所有点坐标信息: " << endl; 19 output(y); 20 cout << "\n更新x对象中点坐标信息......" << endl; 21 x.at(0).move(30, 50); 22 x.at(1).move(-1, -1); 23 cout << "x对象中所有点坐标信息: " << endl; 24 output(x); 25 cout << "\ny对象中所有点坐标信息: " << endl; 26 output(y); 27 } 28 int main() { 29 test(); 30 }

任务4
1 #include <iostream> 2 using namespace std; 3 // 函数声明 4 void swap1(int& rx, int& ry); // 引用作为形参 5 void swap2(int* px, int* py); // 指针作为形参 6 void print(int x, int y); // 普通变量作为形参 7 // 测试代码 8 void test() { 9 int x = 3, y = 4; 10 print(x, y); 11 swap1(x, y); // 函数调用,注意:引用作为形参时,实参形式 12 print(x, y); 13 cout << endl; 14 x = 3, y = 4; 15 print(x, y); 16 swap2(&x, &y); // 函数调用,注意:指针作为形参时,实参形式 17 print(x, y); 18 } 19 int main() { 20 test(); 21 } 22 // 函数定义:交换两个变量(引用变量作为形参) 23 void swap1(int& rx, int& ry) { 24 int t; 25 t = rx; rx = ry; ry = t; 26 } 27 // 函数定义:交换两个变量(指针变量作为形参) 28 void swap2(int* px, int* py) { 29 int t; 30 t = *px; *px = *py; *py = t; 31 } 32 // 函数定义:输出两个变量(普通变量作为形参) 33 void print(int x, int y) { 34 std::cout << "x = " << x << ", y = " << y << "\n"; 35 }

1 #include <iostream> 2 #include <typeinfo> 3 using namespace std; 4 int main() { 5 int a; 6 int& ra = a; 7 ra = 4; 8 int* pa = &a; 9 *pa = 5; 10 // 以十六进制形式输出普通变量a, 引用变量ra,指针变量pa的地址 11 cout << "&a = " << hex << &a << endl; 12 cout << "&ra = " << hex << &ra << endl; 13 cout << "&pa = " << hex << &pa << "\n\n"; 14 // 输出普通变量a, 引用变量ra,指针变量pa的值 15 cout << "a = " << a << endl; 16 cout << "ra = " << a << endl; 17 cout << "pa = " << hex << pa << endl; 18 // 输出指针变量pa指向的变量的值 19 cout << "*pa = " << *pa << "\n\n"; 20 // 输出普通变量a,引用变量ra, 指针变量pa的类型信息 21 cout << "type a: " << typeid(a).name() << endl; 22 cout << "type ra: " << typeid(ra).name() << endl; 23 cout << "type pa: " << typeid(pa).name() << endl; 24 }

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 #include<cassert> 3 using namespace std; 4 class vectorInt 5 { 6 public: 7 vectorInt(int n); 8 vectorInt(int n, int m); 9 vectorInt(const vectorInt & vc); 10 int get_size() const; 11 ~vectorInt(); 12 int& at(int index); 13 int& at(int index) const; 14 private: 15 int size; 16 int* ptr; 17 }; 18 vectorInt::vectorInt(int n) 19 { 20 size = n; 21 ptr = new int[n]; 22 cout << "constructor vectorInt(int n) called." << endl; 23 } 24 vectorInt::vectorInt(int n, int m) 25 { 26 size = n; ptr = new int[n]; 27 for (int i = 0; i < size; i++) 28 ptr[i] = m; 29 cout << "constructor vectorInt(int n,int value) called" << endl; 30 } 31 vectorInt::vectorInt(const vectorInt & vc) 32 { 33 size = vc.size; 34 ptr = new int[size]; 35 for (int i = 0; i < size; ++i) 36 ptr[i] = vc.ptr[i]; 37 cout << "copy constructor called" << endl; 38 } 39 vectorInt::~vectorInt() 40 { 41 delete[] ptr; 42 cout << "destructor called" << endl; 43 } 44 int vectorInt::get_size() const 45 { 46 return size; 47 } 48 int& vectorInt::at(int index) 49 { 50 assert(index >= 0 && index < size); // 宏,在测试模式下工作。如果不满足条件,则程序终止 51 return ptr[index]; 52 } 53 int& vectorInt::at(int index) const 54 { 55 assert(index >= 0 && index < size); 56 return ptr[index]; 57 }
1 #include "vectorInt.hpp" 2 #include <iostream> 3 using std::cout; 4 using std::cin; 5 using std::endl; 6 // 函数output()的定义:遍历输出vectorInt对象内的所有元素 7 void output(const vectorInt& vi) { 8 for (int i = 0; i < vi.get_size(); ++i) 9 cout << vi.at(i) << ", "; 10 cout << "\b\b \n"; 11 } 12 void test() { 13 int n; 14 cout << "输入vectorInt对象中元素个数: "; 15 cin >> n; 16 vectorInt x1(n); // 构造动态int数组对象x1,包含n个元素,不对元素初始化 17 for (auto i = 0; i < n; ++i) 18 x1.at(i) = i * i; 19 cout << "vectorInt对象x1: "; 20 output(x1); 21 vectorInt x2(n, 42); // 构造动态int数组对象x1,包含n个元素,每个元素初始值为42 22 cout << "vectorInt对象x2: "; 23 output(x2); 24 vectorInt x3(x2); // 使用x2构造x3 25 cout << "vectorInt对象x3: "; 26 output(x3); 27 cout << "更新vectorInt对象x2......\n"; 28 x2.at(0) = 77; 29 x2.at(1) = -999; 30 cout << "vectorInt对象x2: "; 31 output(x2); 32 cout << "vectorInt对象x3: "; 33 output(x3); 34 } 35 int main() { 36 test(); 37 }

任务6
1 #pragma once 2 #include <iostream> 3 #include <cassert> 4 using std::cout; 5 using std::endl; 6 // 类Matrix的声明 7 class Matrix { 8 public: 9 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 10 Matrix(int n); // 构造函数,构造一个n*n的矩阵 11 Matrix(const Matrix& x); // 复制构造函数, 使用已有的矩阵X构造 12 ~Matrix(); 13 void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 14 void set(int i, int j, double value); // 设置矩阵对象索引(i,j)的元素值为value 15 double& at(int i, int j) const; // 返回矩阵对象索引(i,j)的元素引用 16 double& at(int i, int j); // 返回矩阵对象索引(i,j)的元素引用 17 int get_lines() const; // 返回矩阵对象行数 18 int get_cols() const; // 返回矩阵对象列数 19 void print() const; // 按行打印输出矩阵对象元素值 20 private: 21 int lines; // 矩阵对象内元素行数 22 int cols; // 矩阵对象内元素列数 23 double* ptr; 24 }; 25 // 类Matrix的实现:待补足 26 // xxx 27 Matrix::Matrix(int n, int m) : lines{ n }, cols{ m } { 28 assert(n > 0 && m > 0); 29 ptr = new double[n * m]; 30 } 31 32 Matrix::Matrix(int n) : Matrix(n, n) {} 33 34 Matrix::Matrix(const Matrix& x) : lines{ x.lines }, cols{ x.cols } { 35 ptr = new double[lines * cols]; 36 for (int i = 0; i < lines * cols; ++i) { 37 ptr[i] = x.ptr[i]; 38 } 39 } 40 41 Matrix::~Matrix() { 42 delete[] ptr; 43 } 44 45 void Matrix::set(const double* pvalue) { 46 for (int i = 0; i < lines * cols; ++i) { 47 ptr[i] = pvalue[i]; 48 } 49 } 50 51 void Matrix::set(int i, int j, double value) { 52 assert(i >= 0 && i < lines && j >= 0 && j < cols); 53 ptr[i * cols + j] = value; 54 } 55 56 double& Matrix::at(int i, int j) const { 57 assert(i >= 0 && i < lines && j >= 0 && j < cols); 58 return ptr[i * cols + j]; 59 } 60 61 double& Matrix::at(int i, int j) { 62 assert(i >= 0 && i < lines && j >= 0 && j < cols); 63 return ptr[i * cols + j]; 64 } 65 66 int Matrix::get_lines() const { 67 return lines; 68 } 69 70 int Matrix::get_cols() const { 71 return cols; 72 } 73 74 void Matrix::print() const { 75 for (int i = 0; i < lines; ++i) { 76 for (int j = 0; j < cols; ++j) { 77 cout << ptr[i * cols + j] << " "; 78 } 79 cout << endl; 80 } 81 }
1 #include <iostream> 2 #include "matrix.hpp" 3 using namespace std; 4 const int N1 = 3; 5 const int N2 = 2; 6 // 输出一个矩阵对象中索引为index对应的行的所有元素值 7 void output(const Matrix& m, int index) { 8 for (auto j = 0; j < m.get_cols(); ++j) 9 cout << m.at(index, j) << ", "; 10 cout << "\b\b \n"; 11 } 12 void test() { 13 double x[N1 * N2] = { 1, 2, 3, 4, 5, 6 }; 14 Matrix m1(N1, N2); // 创建一个N1×N2矩阵 15 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 16 cout << "矩阵对象m1: " << endl; 17 m1.print(); // 打印矩阵m1的值 18 cout << "矩阵对象m1第0行是: " << endl; 19 output(m1, 0); 20 cout << endl; 21 Matrix m2(N2, N1); 22 m2.set(x); 23 cout << "矩阵对象m2: " << endl; 24 m2.print(); 25 cout << "矩阵对象m2第0行是: " << endl; 26 output(m2, 0); 27 cout << endl; 28 Matrix m3(m2); // 用矩阵m2构造新的矩阵m3 29 m3.set(0, 0, 999); // 讲矩阵对象m2索引(0,0)元素设为999 30 cout << "矩阵对象m3:" << endl; 31 m3.print(); 32 cout << endl; 33 Matrix m4(2); // 创建一个2*2矩阵对象 34 m4.set(x); // 用一维数组x的值按行为矩阵m4赋值 35 cout << "矩阵对象m4:" << endl; 36 m4.print(); 37 } 38 int main() { 39 test(); 40 }


浙公网安备 33010602011771号