实验任务5
vectorInt.hpp
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 class vectorInt{ 5 public: 6 vectorInt(int n); 7 vectorInt(int n,int value); 8 vectorInt(const vectorInt &); 9 ~vectorInt(); 10 int &at(int t){ return p[t];} 11 int get_size(){return size;} 12 friend void output(vectorInt &p1); 13 private: 14 int size; 15 int *p; 16 }; 17 vectorInt::vectorInt(int n):size{n} { 18 cout<<"constructor 1 called"<<endl; 19 p=new int[n]; 20 } 21 vectorInt::vectorInt(int n,int value):size{n}{ 22 cout<<"constructor 2 called"<<endl; 23 p=new int[n]; 24 for(auto i=0;i<n;i++){ 25 p[i]=value; 26 } 27 } 28 vectorInt::vectorInt(const vectorInt &vi):size{vi.size} 29 { 30 p=new int[size]; 31 cout<<"copy constructor called."<<endl; 32 for(auto i=0;i<vi.size;i++) 33 { 34 p[i]=vi.p[i]; 35 } 36 } 37 void output(vectorInt &p1) 38 { 39 for(auto i=0;i<p1.size-1;++i) { 40 cout<<p1.at(i)<<","; 41 } 42 cout<<p1.at(p1.size-1)<<endl; 43 } 44 vectorInt::~vectorInt(){ 45 cout<<"destructor called."<<endl; 46 delete[] p; 47 }
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 { 21 test(); 22 }
实验任务6
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); 8 Matrix(int n,int m); 9 Matrix(const Matrix &x); 10 ~Matrix(); 11 void set(const double *pvalue); 12 void set(int i,int j,int value); 13 double &at(int i,int j); 14 double at(int i,int j) const; 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::Matrix(int n):lines{n},cols{n}{ 24 p=new double[n*n]; 25 } 26 Matrix::Matrix(int n,int m):lines{n},cols{m}{ 27 p=new double[n*m]; 28 } 29 Matrix::Matrix(const Matrix &x) 30 { 31 lines=x.lines; 32 cols=x.cols; 33 p=new double[lines*cols]; 34 for(int i=0;i<lines;i++) 35 { 36 for(int j=0;j<cols;j++) 37 { 38 p[i*cols+j]=x.p[i*cols+j]; 39 } 40 } 41 } 42 Matrix::~Matrix() 43 { 44 delete[] p; 45 } 46 void Matrix::set(const double *pvalue){ 47 for(int i=0;i<lines;i++) 48 { 49 for(int j=0;j<cols;j++) 50 { 51 p[i*cols+j]=*pvalue; 52 pvalue++; 53 } 54 } 55 } 56 void Matrix::set(int i,int j,int value) 57 { 58 p[i*cols+j]=value; 59 } 60 double& Matrix::at(int i,int j){ 61 return p[i*cols+j]; 62 } 63 double Matrix::at(int i,int j)const{ 64 return p[i*cols+j]; 65 } 66 int Matrix::get_lines()const{ 67 return lines; 68 } 69 int Matrix::get_cols()const{ 70 return cols; 71 } 72 void Matrix::print()const{ 73 for(int i=0;i<lines;i++) 74 { 75 for(int j=0;j<cols-1;j++) 76 { 77 cout<<p[i*cols+j]<<","; 78 } 79 cout<<p[i*cols+cols-1]<<endl; 80 } 81 }
task6.cpp
1 #include<iostream> 2 #include"matrix.hpp" 3 void test(){ 4 using namespace std; 5 double x[]={1,2,3,4,5,6}; 6 Matrix m1(3,2); 7 m1.set(x); 8 m1.print(); 9 cout<<"the first line is: "<<endl; 10 cout<<m1.at(0,0)<<" "<<m1.at(0,1)<<endl; 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); 19 m3.set(0,0,999); 20 m3.print(); 21 } 22 int main() 23 { 24 test(); 25 }
标签:const,Matrix,int,double,cols,实验,数组,vectorInt,指针 From: https://www.cnblogs.com/cwj202183290470/p/16852650.html