T1:

vector_int.hpp

 1 #include<iostream>
 2 using namespace std;
 3 class Vector_int {
 4 public:
 5     Vector_int(int n);
 6     Vector_int(int n, int value);
 7     Vector_int(Vector_int& x);
 8     int& at(int i);
 9     ~Vector_int() {
10         cout << "Destructor Start!" << endl;
11         delete[] vec;
12         cout << "Destructor Success!" << endl;
13     }
14 private:
15     int* vec;
16     int size;
17 };
18 Vector_int::Vector_int(int n) {
19     cout << "Constructor Start!" << endl;
20     vec = new int[n];
21     size = n;
22     cout << "Constructor Success!" << endl;
23 }
24 Vector_int::Vector_int(int n, int value) {
25     cout << "Constructor Start!" << endl;
26     vec = new int[n];
27     for (int i = 0; i < n; i++)
28         vec[i] = value;
29     size = n;
30     cout << "Constructor Success!" << endl;
31 }
32 Vector_int::Vector_int(Vector_int& x) {
33     cout << "Copy Construtor Start!" << endl;
34     vec = new int[x.size];
35     size = x.size;
36     for (int i = 0; i < size; i++)
37         vec[i] = x.vec[i];
38     cout << "Copy Constructor Success!" << endl;
39 }
40 int& Vector_int::at(int i) {    
41     if (i < size && i>=0)
42         return vec[i];
43 }

vector_int.cpp

 1 #include"vector_int.hpp"
 2 #include<iostream>
 3 using namespace std;
 4 int main() {
 5     int n;
 6     cin >> n;
 7     Vector_int x(n);
 8     Vector_int y(n, 1);
 9     Vector_int z(y);
10     z.at(2) = 0;
11 }

 T2:

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 Matrix::Matrix(int n) {
28     lines = n;
29     cols = n;
30     p = new double[n*n];
31 }
32 Matrix::Matrix(int n, int m) {
33     lines = n;
34     cols = m;
35     p = new double[n * m];
36 }
37 Matrix::Matrix(const Matrix& X) {
38     lines = X.lines;
39     cols = X.cols;
40     p = new double[lines * cols];
41     for (auto i = 0; i < lines * cols; i++) {
42         p[i] = X.p[i];
43     }
44 }
45 Matrix::~Matrix() {
46     delete[] p;
47 }
48 void Matrix::set(const double* pvalue) {
49     for (int i = 0; i < lines * cols; i++) {
50         p[i] = pvalue[i];
51     }
52 }
53 void Matrix::set(int i, int j, int value) {
54     p[i * cols + j] = value;
55 }
56 double& Matrix::at(int i, int j){
57     return p[i * cols + j];
58 }
59 double Matrix::at(int i, int j) const {
60     return p[i * cols + j];
61 }
62 int Matrix::get_lines() const {
63     return lines;
64 }
65 int Matrix::get_cols() const {
66     return cols;
67 }
68 void Matrix::print() const {
69     for (int i = 0; i < lines; i++) {
70         for (int j = 0; j < cols; j++) {
71             std::cout << p[i * cols + j]<<" ";
72         }
73         std::cout << std::endl;
74     }
75 }
76 #endif

Matrix.cpp

 1 #include <iostream>
 2 #include "Matrix.hpp"
 3 
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     double x[] = {0,2,4,6,8,10,12,14,16};
 9     double x2[] = { 3,6,9,12,15,18 };
10     Matrix m1(3);    // 创建一个3×3的矩阵
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) << " "<< m1.at(0,2) << endl;
15     cout << endl;
16 
17     Matrix m2(2, 3);
18     m2.set(x2);
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.at(0, 1) = 233;
27     m3.print();
28 }