实验3 类和对象||
task4.cpp源码
#include<iostream> #include"vector_int.hpp" using namespace std; int main(){ Vector_int x(5); cout<<"x(0)="<<x.at(0)<<endl; Vector_int x1(5,6); cout<<"x1(0)="<<x1.at(0)<<endl; Vector_int y(x1); cout<<"y(3)="<<y.at(3)<<endl; }
vector_int.hpp源码
#ifndef Vector_int_H #define Vector_int_H #include<iostream> using namespace std; class Vector_int{ public: Vector_int(int n); Vector_int(int n,int num0); Vector_int(const Vector_int &x); ~Vector_int(); int &at(int index); private: int size; int num; int *p; }; Vector_int::Vector_int(int n):size(n){ cout<<"dynamic create array..."<<endl; p=new int[size]; for(int i=0;i<size;i++) p[i]=0; } Vector_int::Vector_int(int n,int num0):size(n),num(num0){ cout<<"dynamic create array..."<<endl; p=new int[size]; for(int i=0;i<size;i++) p[i]=num; } Vector_int::Vector_int(const Vector_int &x):size(x.size){ cout<<"d-copy create array..."<<endl; p=new int[size]; for(int i=0;i<size;i++) p[i]=x.num; } Vector_int::~Vector_int(){ cout<<"deleting..."<<endl; delete[] p; } int &Vector_int::at(int index){ assert( index>=0 && index<size ); return p[index]; } #endif
task4测试结果截图:
task5.cpp源码
#include<iostream> #include"Matrix.hpp" int main(){ using namespace std; double x[]={1,2,3,4,5,6}; Matrix m1(3,2); m1.set(x); m1.print(); cout<<"the first line is:"<<endl; cout<<m1.at(0,0)<<" "<<m1.at(0,1)<<endl; cout<<endl; Matrix m2(2,3); m2.set(x); m2.print(); cout<<"the first line is:"<<endl; cout<<m2.at(0,0)<<" "<<m2.at(0,1)<<" "<<m2.at(0,2)<<endl; cout<<endl; Matrix m3(m2); m3.set(0,0,999); m3.print(); }
Matrix.hpp源码
#ifndef MATRIX_H #define MATRIX_H #include<iostream> using namespace std; class Matrix{ public: Matrix(int n); Matrix(int n,int m); Matrix(const Matrix &X); ~Matrix(); void set(const double *pvalue); void set(int i, int j , int value); double &at(int i, int j); double at(int i , int j) const; int get_lines() const; int get_cols() const; void print() const; private: int lines; int cols; double *p; }; Matrix::Matrix(int n):lines(n),cols(n){ p=new double[lines*cols]; for(int i=0;i<lines;i++){ for(int j=0;j<cols;j++) p[i*cols+j]=0; } } Matrix::Matrix(int n, int m):lines(n),cols(m){ p=new double[lines*cols]; for(int i=0;i<lines;i++){ for(int j=0;j<cols;j++) p[i*cols+j]=0; } } Matrix::Matrix(const Matrix &X):lines(X.lines),cols(X.cols){ p=new double[lines*cols]; for(int i=0;i<lines;i++){ for(int j=0;j<cols;j++) p[i*cols+j]=X.p[i*cols+j]; } } Matrix::~Matrix(){ delete[] p; } void Matrix::set(const double *pvalue){ int count=0; for(int i =0;i<lines;i++){ for(int j=0;j<cols;j++){ p[i*cols+j]=pvalue[count]; count++; } } } void Matrix::set(int i, int j , int value){ p[i*cols+j]=value; } double &Matrix::at(int i, int j){ return p[i*cols+j]; } double Matrix::at(int i, int j) const{ return p[i*cols+j]; } int Matrix::get_lines() const{ return lines; } int Matrix::get_cols() const{ return cols; } void Matrix::print() const{ for(int i=0;i<lines;i++){ for(int j=0;j<cols;j++){ cout<<p[i*cols+j]<<","; } cout<<"\n"; } } #endif
task5原实验结果截图
task5.cpp修改实验数据后的源码
#include<iostream> #include"Matrix.hpp" int main(){ using namespace std; double x[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; Matrix m1(3,5); m1.set(x); m1.print(); cout<<"the first line is:"<<endl; cout<<m1.at(0,0)<<" "<<m1.at(0,1)<<" "<<m1.at(0,2)<<" "<<m1.at(0,3)<<" "<<m1.at(0,4)<<endl; cout<<endl; Matrix m2(5,3); m2.set(x); m2.print(); cout<<"the first line is:"<<endl; cout<<m2.at(0,0)<<" "<<m2.at(0,1)<<" "<<m2.at(0,2)<<endl; cout<<endl; Matrix m3(m2); m3.set(0,0,999); m3.print(); }
task5修改后的实验结果截图: