实验任务5
1 #pragma once 2 #include<iostream> 3 4 using std::cout; 5 using std::endl; 6 7 class vectorInt { 8 public: 9 //构造函数和析构函数 10 vectorInt(int n); 11 vectorInt(int n, int value); 12 vectorInt(const vectorInt& X); 13 ~vectorInt(); //~vectorInt()=default; 默认合成的析构函数 14 15 //其它成员函数 16 int& at(int i); 17 int get_size(); 18 19 //友元函数 20 friend void output(const vectorInt& X); 21 22 private: 23 int size; 24 int* p; 25 }; 26 27 vectorInt::vectorInt(int n) :size{ n } { 28 cout << "constructor 1 called.\n"; 29 p = new int[size]; 30 } 31 32 vectorInt::vectorInt(int n, int value) :size{ n } 33 { 34 cout << "constuctot 2 called" << endl; 35 p = new int[size]; 36 for (int i = 0; i < n; ++i) 37 p[i] = value; 38 } 39 40 vectorInt::vectorInt(const vectorInt& X) :size{ X.size } 41 { 42 cout << "copy constuctot called" << endl; 43 p = new int[X.size]; 44 for (auto i = 0; i < X.size; ++i) 45 p[i] = X.p[i]; 46 } 47 48 vectorInt::~vectorInt() { 49 cout << "destructor called.\n"; 50 delete[]p; 51 } 52 53 int& vectorInt::at(int i) { 54 return p[i]; 55 } 56 57 int vectorInt::get_size() { 58 return size; 59 } 60 61 void output(const vectorInt& X) { 62 for (auto i = 0; i < X.size; i++) 63 cout << X.p[i] << " "; 64 cout << endl; 65 }vectorInt.hpp
1 #include <iostream> 2 #include "vectorInt.hpp" 3 void test() { 4 using namespace std; 5 int n; 6 cin >> n; 7 vectorInt x1(n); 8 for (auto i = 0; i < n; ++i) 9 x1.at(i) = i * i; 10 output(x1); 11 vectorInt x2(n, 42); 12 vectorInt x3(x2); 13 output(x2); 14 output(x3); 15 x2.at(0) = 77; 16 output(x2); 17 output(x3); 18 } 19 int main() { 20 test(); 21 }task5.cpp
实验测试截图:
实验任务6
1 #pragma once 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 class Matrix { 6 public: 7 Matrix(int n); // 构造函数,构造一个n*n的矩阵 8 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 9 Matrix(const Matrix& X); // 复制构造函数,使用已有的矩阵X构造 10 ~Matrix(); //析构函数 11 void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 12 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 13 double& at(int i, int j); //返回矩阵第i行第j列元素的引用 14 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 15 int get_lines() const; //返回矩阵行数 16 int get_cols() const; //返回矩列数 17 void print() const; // 按行打印输出矩阵 18 private: 19 int lines; // 矩阵行数 20 int cols; // 矩阵列数 21 double* p; // 指向存放矩阵数据的内存块的首地址 22 }; 23 // 类Matrix的实现: 24 25 Matrix::Matrix(int n) :lines(n), cols(n) { 26 p = new double[n * n]; 27 } 28 29 Matrix::Matrix(int n, int m) :lines(n), cols(m){ 30 p = new double[n * m]; 31 } 32 33 Matrix::Matrix(const Matrix& X) :lines(X.lines), cols(X.cols) { 34 p = new double[X.lines * X.cols]; 35 for (auto i = 0; i < X.lines * X.cols; i++) { 36 p[i] = X.p[i]; 37 } 38 } 39 40 Matrix::~Matrix() { 41 delete[]p; 42 } 43 44 void Matrix::set(const double* pvalue) { 45 for (int i = 0; i < lines * cols; i++) { 46 p[i] = pvalue[i]; 47 } 48 } 49 50 void Matrix::set(int i, int j, int value) { 51 p[i * cols + j] = value; 52 } 53 54 double& Matrix::at(int i, int j) { 55 return p[i * cols + j]; 56 } 57 58 double Matrix::at(int i, int j)const { 59 return p[i * cols + j]; 60 } 61 62 int Matrix::get_lines()const { 63 return lines; 64 } 65 66 int Matrix::get_cols()const { 67 return cols; 68 } 69 70 void Matrix::print()const { 71 for (auto i = 0; i < lines * cols; i++) { 72 cout << p[i] << " "; 73 if ((i+1) % cols == 0) cout << '\n'; 74 } 75 }matrix.hpp
1 #include <iostream> 2 #include "matrix.hpp" 3 void test() { 4 using namespace std; 5 double x[] = { 1, 2, 3, 4, 5, 6 }; 6 Matrix m1(3, 2); // 创建一个3×2的矩阵 7 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 8 m1.print(); // 打印矩阵m1的值 9 cout << "the first line is: " << endl; 10 cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; // 输出矩阵m1第1行两个元素的值 11 cout << endl; 12 Matrix m2(2, 3); 13 m2.set(x); 14 m2.print(); 15 cout << "the first line is: " << endl; 16 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 17 cout << endl; 18 Matrix m3(m2); // 用矩阵m2构造新的矩阵m3 19 m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999 20 m3.print(); 21 } 22 int main() { 23 test(); 24 }task6.cpp
测试截图:
标签:const,Matrix,int,lines,cols,实验,数组,vectorInt,指针 From: https://www.cnblogs.com/Terrence0403/p/16861280.html