实验任务5
vectorInt.hpp1 #pragma once 2 #include<iostream> 3 #include<cassert> 4 using namespace std; 5 6 class vectorInt { 7 public: 8 vectorInt(int n); 9 vectorInt(int n, int value); 10 vectorInt(const vectorInt& vi); 11 ~vectorInt(); 12 int& at(int index); 13 int get_size(); 14 friend void output(const vectorInt& vi); 15 private: 16 int size; 17 int* p; 18 }; 19 vectorInt::vectorInt(int n) :size{ n } { 20 cout << "constructor 1 called." << endl; 21 p = new int[n]; 22 } 23 vectorInt::~vectorInt() { 24 cout << "destructor called." << endl; 25 delete p; 26 } 27 int &vectorInt::at(int index) { 28 assert(index >= 0 && index < size); 29 return p[index]; 30 } 31 vectorInt::vectorInt(const vectorInt& vi) :size{vi.size} { 32 cout << "copy constructor called." << endl; 33 p = new int[size]; 34 for (auto i = 0; i < size; i++) 35 p[i] = vi.p[i]; 36 } 37 vectorInt::vectorInt(int n, int value) :size{ n } { 38 cout << "constructor 2 called." << endl; 39 p = new int[n]; 40 for (auto i = 0; i < n; i++) { 41 p[i] = value; 42 } 43 } 44 int vectorInt::get_size() { 45 return size; 46 } 47 void output(const vectorInt& vi) { 48 for (auto i = 0; i < vi.size; i++) 49 cout << vi.p[i] << ", "; 50 cout << "\b\b \n"; 51 }
task5.cpp
1 #include<iostream> 2 #include"vectorInt.hpp" 3 4 void test() { 5 using namespace std; 6 7 int n; 8 cin >> n; 9 10 vectorInt x1(n); 11 for (auto i = 0; i < n; ++i) 12 x1.at(i) = i * i; 13 14 output(x1); 15 16 vectorInt x2(n, 42); 17 vectorInt x3(x2); 18 output(x2); 19 output(x3); 20 21 x2.at(0) = 77; 22 output(x2); 23 output(x3); 24 } 25 int main() { 26 test(); 27 }
实验截图
实验任务6
Matrix.hpp
1 #pragma once 2 #include<iostream> 3 4 using std::cout; 5 using std::endl; 6 7 class Matrix { 8 public: 9 Matrix(int n); // 构造函数,构造一个n*n的矩阵 10 Matrix(int n, int m);// 构造函数,构造一个n*m的矩阵 11 Matrix(const Matrix& x);// 复制构造函数,使用已有的矩阵X构造 12 ~Matrix();//析构函数 13 14 void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 15 void set(int i, int j, int value);//设置矩阵第i行第j列元素值为value 16 double at(int i, int j)const;// 返回矩阵第i行第j列元素的值 17 int get_lines()const {return lines; }//返回矩阵行数 18 int get_cols()const { return cols; }//返回矩列数 19 void print() const;// 按行打印输出矩阵 20 21 private: 22 int lines; 23 int cols; 24 double* p; 25 }; 26 Matrix::Matrix(int n) :lines{ n }, cols{ n } { 27 p = new double[n * n]; 28 } 29 Matrix::Matrix(int n, int m) :lines{ n }, cols{ m } { 30 p = new double[n * m]; 31 } 32 Matrix::Matrix(const Matrix& x) :lines{ x.lines }, cols{x.cols} { 33 34 p = new double[lines * cols]; 35 for (auto i = 0; i < lines * cols; i++) { 36 p[i] = x.p[i]; 37 } 38 } 39 Matrix::~Matrix() { 40 delete p; 41 } 42 void Matrix::set(const double* pvalue) { 43 for (auto i = 0; i < lines * cols; i++) { 44 p[i] = pvalue[i]; 45 } 46 } 47 void Matrix::print()const { 48 int k = 0; 49 for (auto j = 0; j < lines; j++) { 50 for (auto i = 0; i < cols; i++) { 51 cout << p[k++] << ", "; 52 } 53 cout << "\b\b \n"; 54 } 55 } 56 void Matrix::set(int i, int j, int value) { 57 p[i*cols+j] = value; 58 } 59 double Matrix::at(int i, int j)const{ 60 return p[i*cols+j]; 61 }
task6.cpp
1 #include<iostream> 2 #include"Matrix.hpp" 3 4 void test() { 5 using namespace std; 6 7 double x[] = { 1,2,3,4,5,6 }; 8 9 Matrix m1(3, 2); 10 m1.set(x); 11 m1.print(); 12 cout << "the first line is: " << endl; 13 cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; 14 cout << endl; 15 16 Matrix m2(2, 3); 17 m2.set(x); 18 m2.print(); 19 cout << "the first line is: " << endl; 20 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 21 cout << endl; 22 23 Matrix m3(m2); 24 m3.set(0, 0, 999); 25 m3.print(); 26 cout << endl; 27 28 } 29 int main() 30 { 31 test(); 32 }
实验截图
标签:const,Matrix,int,lines,cols,实验,vectorInt From: https://www.cnblogs.com/lee404error/p/16867919.html