实验三 类和对象 II
vector_int.hpp
1 #include<iostream> 2 3 using namespace std; 4 5 class vector_int 6 { 7 public: 8 vector_int(int n, int number = 0); 9 vector_int(vector_int &v); 10 ~vector_int() 11 { 12 cout<<"deleting..."<<endl; 13 delete []p; 14 } 15 int &at(int index); 16 private: 17 int size; 18 int *p; 19 }; 20 21 22 vector_int::vector_int(int n, int number) 23 { 24 cout<<"Calling the constructor"<<endl; 25 size = n; 26 p = new int[size]; 27 for(int i = 0; i < size; i++) 28 p[i] = number; 29 } 30 31 vector_int::vector_int(vector_int &v) 32 { 33 cout<<"Calling the copy constructor"<<endl; 34 p = new int[v.size]; 35 size = v.size; 36 for(int i= 0; i < v.size; i++) 37 p[i] = v.p[i]; 38 } 39 40 int &vector_int::at(int index) 41 { 42 if((index >= 0) && (index < size)) 43 return p[index]; 44 }
task4.cpp
1 #include <iostream> 2 #include"vector_int.hpp" 3 4 using namespace std; 5 6 int main() 7 { 8 int n = 6, i; 9 vector_int x(n, 6); 10 // 输出X的值 11 cout<<endl; 12 cout<<"x的值为:"<<endl; 13 for(i = 0; i< n - 1; i++) 14 cout<<x.at(i)<<" "; 15 cout<<x.at(i)<<endl; 16 cout<<endl; 17 18 // 调用复制构造函数 19 vector_int y(x); 20 21 y.at(0) = 999; 22 cout<<endl; 23 cout<<"y的值为:"<<endl; 24 for(i = 0; i< n - 1; i++) 25 cout<<y.at(i)<<" "; 26 cout<<y.at(i)<<endl; 27 cout<<endl; 28 return 0; 29 }

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

浙公网安备 33010602011771号