matrix.hpp
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[lines*cols]; 27 } 28 Matrix::Matrix(int n, int m):lines(n),cols(m) { 29 p = new double[lines*cols]; 30 } 31 Matrix::Matrix(const Matrix& X):lines(X.lines),cols(X.cols) { 32 p = new double[lines * cols]; 33 for (auto i = 0; i < lines*cols; i++) { 34 p[i] = X.p[i]; 35 } 36 } 37 Matrix::~Matrix() { 38 delete[] p; 39 } 40 void Matrix::set(const double* pvalue) { 41 for (auto i = 0; i < lines * cols; i++) { 42 p[i] = pvalue[i]; 43 } 44 } 45 void Matrix:: set(int i, int j, int value) { 46 p[i * cols + j] = value; 47 } 48 double& Matrix::at (int i, int j) { 49 return p[i * cols + j]; 50 } 51 double Matrix:: at(int i, int j) const { 52 return p[i * cols + j]; 53 } 54 int Matrix::get_lines() const { 55 return lines; 56 } 57 int Matrix::get_cols() const { 58 return cols; 59 } 60 void Matrix::print() const { 61 for (auto i = 0; i < lines * cols; i++) { 62 cout << p[i] << " "; 63 if ((i+1) % cols == 0) { 64 cout << endl; 65 } 66 } 67 }
task5.cpp
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 }
vectorInt.hpp
1 #pragma once 2 #include<iostream> 3 #include <cassert> 4 using namespace std; 5 class vectorInt { 6 friend void output(vectorInt& x); 7 private: 8 int* a; 9 int size=0; 10 public: 11 vectorInt(int n) { 12 cout << "constructor 1 called" << endl; 13 a = new int[n]; 14 int i; 15 for (i = 0; i < n; i++) { 16 a[i] = i; 17 } 18 size = n; 19 } 20 vectorInt(int n,int value) { 21 cout << "constructor 2 called" << endl; 22 a = new int[n]; 23 int i; 24 for (i = 0; i < n; i++) { 25 a[i] = value; 26 } 27 size = n; 28 } 29 vectorInt(const vectorInt& y):a(new int[y.size]) { 30 if (y.size == 0) { 31 return ; 32 } 33 size = y.size; 34 cout << "copy constructor called" << endl; 35 int i; 36 for (i = 0; i < y.size; i++) { 37 a[i] = y.a[i]; 38 } 39 } 40 ~vectorInt() { 41 if (a != NULL) { 42 delete[] a; 43 cout << "destroyed constructor called " << endl; 44 a = NULL; 45 } 46 } 47 int& at(int i) { 48 assert(i >= 0 && i < size); 49 return a[i]; 50 } 51 int get_size() { 52 return size; 53 } 54 }; 55 void output(vectorInt& x) { 56 for (auto i = 0; i < x.size; i++) { 57 cout << x.a[i] << " "; 58 } 59 cout << endl; 60 }
task6.cpp
1 #include <iostream> 2 #include "matrix.hpp" 3 void test() { 4 using namespace std; 5 double x[] = { 2,4,6,8,10,12 }; 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(1, 1, 666); // 将矩阵m3第0行第0列元素值设为999 20 m3.print(); 21 } 22 int main() { 23 test(); 24 }
实验总结:
1.当成员变量为指针时,进行拷贝构造时候,需要进行深拷贝,当对指针变量进行赋值的时候,需要在堆区手动开辟一块空间(new);
2.在堆区手动开辟一块空间的时候,需要在析构函数的时候释放,当new的是一个简单基本类型的时候,delete就可以释放空间,而当new的是对于对象数组的时候,
需要delete[]一次性进行释放空间,而使用delete 的时候相当于只释放了一个对象数组元素,可能会造成内存泄漏。
标签:const,Matrix,int,double,lines,cols,实验 From: https://www.cnblogs.com/1916wenle/p/16857562.html