实验三类与对象2
task4:
vector_int.hpp
1 #include<iostream> 2 using namespace std; 3 4 class vector_int{ 5 public: 6 ~vector_int(){ 7 delete a; 8 show(); 9 } 10 vector_int(int n){ 11 size = n; 12 a = new int[size]; 13 show(); 14 } 15 vector_int(int n,int t){ 16 size = n; 17 a = new int[size]; 18 for(int i = 0; i < size; i++) 19 a[i] = t; 20 show(); 21 } 22 vector_int(const vector_int &obj){ 23 size = obj.size; 24 a = new int[size]; 25 for(int i = 0; i < size; i++) 26 a[i] = obj.a[i]; 27 show(); 28 } 29 void show(){ 30 cout << "size: " << size << endl; 31 cout << "data:" << endl; 32 for(int i = 0; i < size; i++) 33 cout << a[i] << ' '; 34 cout << endl; 35 } 36 int &at(int t){ 37 return a[t]; 38 } 39 40 private: 41 int size; 42 int *a; 43 };
task4.cpp
1 #include<iostream> 2 #include"vector_int.hpp" 3 using namespace std; 4 5 int main() 6 { 7 int n,t; 8 cin >> n >> t; 9 vector_int x(n); 10 vector_int y(n,t); 11 vector_int z(y); 12 z.at(1) = 999; 13 cout << z.at(1) << endl; 14 return 0; 15 }

task5:
Matrix.hpp
1 #ifndef MATRIX_H 2 #define MATRIX_H 3 4 #include<iostream> 5 #include<cassert> 6 using namespace std; 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 void set(const double *pvalue); // 用pvalue指向的连续内存块数据为矩阵赋值 14 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 15 double &at(int i, int j); //返回矩阵第i行第j列元素的引用 16 double at(int i, int j) const; // 返回矩阵第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 *p; // 指向存放矩阵数据的内存块的首地址 24 }; 25 26 Matrix::Matrix(int n):lines(n),cols(n) 27 { 28 p=new double[n*n]; 29 } 30 31 Matrix::Matrix(int n,int m):lines(n),cols(m) 32 { 33 p=new double[n*m]; 34 } 35 36 Matrix::Matrix(const Matrix &X) 37 { 38 lines=X.lines; 39 cols=X.cols; 40 p=new double[lines*cols]; 41 for(int i = 0; i < lines * cols; i++) 42 p[i] = X.p[i]; 43 } 44 45 Matrix::~Matrix() 46 { 47 delete[] p; //delete[] 调用每个成员的析构函数 与new[]配套 48 } 49 50 void Matrix::set(const double *pvalue) 51 { 52 for(int i = 0; i < lines * cols; i++) 53 p[i] = *(pvalue++); 54 } 55 56 void Matrix::set(int i, int j, int value) 57 { 58 p[i * cols + j] = value; 59 } 60 61 double &Matrix::at(int i, int j) 62 { 63 return p[i * cols + j]; 64 } 65 66 double Matrix::at(int i, int j) const 67 { 68 return p[i * cols + j]; 69 } 70 71 int Matrix::get_lines() const 72 { 73 return lines; //返回行数 74 } 75 76 int Matrix::get_cols() const 77 { 78 return cols; //返回列数 79 } 80 81 void Matrix::print() const 82 { 83 for(int i = 0; i < lines; i++){ 84 for(int j = 0; j < cols; j++) 85 cout << p[i * cols + j] << ' '; 86 cout << endl; 87 } 88 } 89 90 #endif
task5.cpp
1 #include <iostream> 2 #include "matrix.hpp" 3 4 int main() 5 { 6 using namespace std; 7 8 double x[] = {1, 2, 3, 4, 5, 6}; 9 10 Matrix m1(3, 2); // 创建一个3×2的矩阵 11 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 12 m1.print(); // 打印矩阵m1的值 13 cout << "the first line is: " << endl; 14 cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; 15 cout << endl; 16 17 Matrix m2(2, 3); 18 m2.set(x); 19 m2.print(); 20 cout << "the first line is: " << endl; 21 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 22 cout << endl; 23 24 Matrix m3(m2); 25 m3.set(0, 0, 999); 26 m3.print(); 27 return 0; 28 }


浙公网安备 33010602011771号