实验五:
vectorInt.hpp
1 #pragma once 2 #include<cassert> 3 using namespace std; 4 5 class vectorInt{ 6 public: 7 vectorInt(int n); 8 vectorInt(int n,int value); 9 vectorInt(const vectorInt &); 10 ~vectorInt(){ 11 delete p; 12 cout << "destructor called" << endl; 13 } 14 int &at(int index); 15 int get_size(){ 16 return size; 17 } 18 friend void output(vectorInt &v); 19 20 private: 21 int size; 22 int *p; 23 24 }; 25 int &vectorInt::at(int index){ 26 assert(index>=0&&index<size); 27 return p[index]; 28 } 29 30 vectorInt::vectorInt(int n):size{n}{ 31 p=new int[size]; 32 cout << "constructor 1 called" << endl; 33 }; 34 35 vectorInt::vectorInt(int n,int value):size{n}{ 36 p=new int[size]; 37 for(int i=0;i<size;i++){ 38 p[i]=value; 39 } 40 cout << "constructor 2 called" << endl; 41 } 42 43 vectorInt::vectorInt(const vectorInt &a):size{a.size}{ 44 p=new int[size]; 45 for(int i=0;i<size;i++){ 46 p[i]=a.p[i]; 47 } 48 cout << "copy constructor called" << endl; 49 } 50 51 void output(vectorInt &v){ 52 int i; 53 for(i=0;i<v.size-1;i++){ 54 cout << v.p[i] << ","; 55 } 56 cout << v.p[v.size-1] << endl; 57 }
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 19 output(x2); 20 output(x3); 21 22 x2.at(0) = 77; 23 24 output(x2); 25 output(x3); 26 } 27 28 int main() { 29 test(); 30 }
实验六:
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 12 void set(const double *pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 13 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 14 double &at(int i, int j); //返回矩阵第i行第j列元素的引用 15 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 16 int get_lines() const; //返回矩阵行数 17 int get_cols() const; //返回矩列数 18 void print() const; // 按行打印输出矩阵 19 20 private: 21 int lines; // 矩阵行数 22 int cols; // 矩阵列数 23 double *p; // 指向存放矩阵数据的内存块的首地址 24 }; 25 26 Matrix::Matrix(int n):lines{n},cols{n}{ 27 p=new double[n*n]; 28 } 29 30 Matrix::Matrix(int n,int m):lines{n},cols{m}{ 31 p=new double[n*m]; 32 } 33 34 Matrix::Matrix(const Matrix &x):lines{x.lines},cols{x.cols}{ 35 p=new double[x.lines*x.cols]; 36 for(int i=0;i<x.lines;i++){ 37 for(int j=0;j<x.cols;j++){ 38 p[i*x.cols+j]=x.p[i*x.cols+j]; 39 } 40 } 41 } 42 43 Matrix::~Matrix(){ 44 delete p; 45 } 46 47 void Matrix::set(const double *pvalue){ 48 for(int line=0;line<lines;line++){ 49 for(int col=0;col<cols;col++){ 50 p[line*cols+col]=pvalue[line*cols+col]; 51 } 52 } 53 } 54 55 void Matrix::set(int i,int j,int value){ 56 p[i*cols+j]=value; 57 } 58 59 double &Matrix::at(int i,int j){ 60 return p[i*cols+j]; 61 } 62 63 double Matrix::at(int i,int j)const{ 64 return p[i*cols+j]; 65 } 66 67 int Matrix::get_lines()const{ 68 return lines; 69 } 70 71 int Matrix::get_cols()const{ 72 return cols; 73 } 74 75 void Matrix::print() const{ 76 int i,j; 77 for(i=0;i<lines;i++){ 78 for(j=0;j<cols-1;j++){ 79 cout << p[i*cols+j] <<","; 80 } 81 cout << p[i*cols+cols-1] << endl; 82 } 83 }
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, 1) << " " << m2.at(0, 2) << " " << m2.at(0, 3) << endl; 21 cout << endl; 22 23 Matrix m3(m2); 24 m3.set(0, 0, 888); 25 m3.print(); 26 } 27 28 int main() { 29 test(); 30 }
标签:const,Matrix,int,double,矩阵,实验,数组,vectorInt,指针 From: https://www.cnblogs.com/ljhakuna/p/16860087.html