1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 class vector_int{ 5 public: 6 vector_int(int n); 7 vector_int(int n,int starting); 8 vector_int(const vector_int &x); 9 ~vector_int(); 10 int& at(int i); 11 void print(); 12 private: 13 int size; 14 int *p; 15 }; 16 17 vector_int:: vector_int(int n){ 18 p = new int[n](); 19 size=n; 20 cout<<"constructor1 "<<endl; 21 } 22 vector_int::vector_int(int n,int starting){ 23 p=new int[n](); 24 size = n; 25 for(int i=0;i<size;i++ ) 26 p[i]=starting; 27 cout<<"constructor2"<<endl; 28 } 29 vector_int::vector_int(const vector_int& x) 30 { 31 p=new int[x.size]; 32 size=x.size; 33 for(int i=0;i<size;i++) 34 p[i]=x.p[i]; 35 cout<<"constructor3"<<endl; 36 } 37 vector_int::~vector_int(){ 38 delete []p; 39 cout<<"destructed"<<endl; 40 } 41 42 int &vector_int:: at(int i) { 43 return p[i]; 44 }; 45 46 void vector_int::print() { 47 for (int i = 0; i < size; i++) 48 cout << p[i] << " "; 49 cout << endl; 50 }; 51 int main(){ 52 using namespace std; 53 int n,x; 54 vector_int x1(n); 55 56 vector_int x2(n, 6); 57 58 vector_int y(x2); 59 60 y.at(0) = 999; 61 for(int i;i<n;i++) 62 cout<<setw(5)<<y.at(i); 63 cout<<endl; 64 system("pause"); 65 }
1 #ifndef MATRIX_H 2 #define MATRIX_H 3 #include <iostream> 4 #include <iomanip> 5 using namespace std; 6 class Matrix 7 { 8 public: 9 Matrix(int n); 10 Matrix(int n, int m); 11 Matrix(const Matrix &X); 12 ~Matrix(); 13 void set(const double *pvalue); 14 void set(int i, int j, int value); 15 double &at(int i, int j); 16 double at(int i, int j) const; 17 int get_lines() const; 18 int get_cols() const; 19 void print() const; 20 private: 21 int lines; 22 int cols; 23 double *p; 24 } ; 25 Matrix::Matrix(int n):lines(n),cols(n){ 26 p=new double[n*n]; 27 } 28 Matrix::Matrix(int n,int m):lines(n),cols(m){ 29 p=new double[n*m]; 30 } 31 Matrix::Matrix(const Matrix &X):lines(X.lines),cols(X.cols){ 32 p=new double[lines*cols]; 33 for(int i=0;i<lines*cols;i++) 34 p[i]=X.p[i]; 35 } 36 Matrix::~Matrix(){ 37 delete[]p; 38 p=NULL; 39 } 40 void Matrix::set(const double*pvalue){ 41 for(int i=0;i<cols*lines;i++) 42 p[i]=pvalue[i]; 43 }; 44 void Matrix::set(int i,int j,int value){ 45 int pos=i*cols+j; 46 p[pos]=value; 47 } 48 double& Matrix::at(int i,int j){ 49 int pos=i*lines+j; 50 return p[pos]; 51 } 52 double Matrix::at(int i,int j) const{ 53 int pos=i*cols+j; 54 return p[pos]; 55 } 56 int Matrix::get_lines()const 57 { 58 return lines; 59 } 60 int Matrix::get_cols()const{ 61 return cols; 62 } 63 void Matrix::print() const 64 { 65 int i,j,t=0; 66 for(i=0;i<lines;i++) 67 { 68 for(j=0;j<cols;j++) 69 { 70 cout<<setw(3)<<p[t++]<<","; 71 cout<<"\b "<<endl; 72 } 73 } 74 } 75 #endif 76