实验三 类和对象II

task4.hpp

1
#ifndef VECTOR_INT_HPP 2 #define VERTOR_INT_HPP 3 4 #include <iostream> 5 #include <cassert> 6 7 using namespace std; 8 9 class Vector_int 10 { 11 public: 12 Vector_int(int n); //构造函数,创建int型数组对象时,指定其大小 13 Vector_int(int n,int value); //构造函数,创建int型数组对象时,指定其大小,并将数组对象中每个数据项初始化到特定的值value 14 Vector_int(const Vector_int &x); //复制构造函数,用已经存在的对象构造新的对象(实现深复制) 15 ~Vector_int(); //析构函数,释放占用的内存资源 16 int &at(int i); //支持 x.at(i)通过索引访问动态组对象中第i个数据项 17 void show() const; 18 private: 19 int size; //动态数组的大小 20 int *p; 21 }; 22 23 Vector_int::Vector_int(int n):size(n) 24 { 25 cout<<"dynamic create array..."<<endl; 26 p=new int[n]; 27 for(int i=0;i<n;i++) 28 p[i]=0; 29 } 30 31 Vector_int::Vector_int(int n,int value):size(n) 32 { 33 cout<<"dynamic create array..."<<endl; 34 p=new int[n]; 35 for(int i=0;i<n;i++) 36 p[i]=value; 37 } 38 39 Vector_int::Vector_int(const Vector_int &x):size(x.size) 40 { 41 cout<<"Copy constructor array..."<<endl; 42 p=new int[size]; 43 for(auto i=0;i<size;i++) 44 p[i]=x.p[i]; 45 } 46 47 Vector_int::~Vector_int() 48 { 49 cout<<"deleting..."<<endl; 50 delete[] p; 51 } 52 53 int &Vector_int::at(int i) 54 { 55 assert(i>=0 && i<size); 56 return p[i]; 57 } 58 59 void Vector_int::show() const 60 { 61 for(int i=0;i<size;i++) 62 cout<<p[i]<<","; 63 cout<<endl; 64 } 65 66 67 #endif // VECTOR_INT_HPP
task4.cpp

1
#include "vector_int.hpp" 2 #include <iostream> 3 4 int main() 5 { 6 using namespace std; 7 8 int n; 9 cin>>n; //数组元素个数 10 11 Vector_int x1(n); 12 x1.show(); 13 14 Vector_int x2(n, 6); 15 x2.show(); 16 17 Vector_int y1(x1); 18 y1.show(); 19 20 Vector_int y2(x2); 21 y2.show(); 22 23 y1.at(0) = 999; 24 y1.show(); 25 26 y2.at(0) = 999; 27 y2.show(); 28 }

 

 

 

 

task5.hpp

1
#ifndef MATRIX_H 2 #define MATRIX_H 3 #include <iostream> 4 5 class Matrix 6 { 7 public: 8 Matrix(int n); // 构造函数,构造一个n*n的矩阵 9 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 10 Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 11 ~Matrix(); //析构函数 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 private: 20 int lines; // 矩阵行数 21 int cols; // 矩阵列数 22 double *p; // 指向存放矩阵数据的内存块的首地址 23 }; 24 25 Matrix::Matrix(int n) 26 { 27 lines=n; 28 cols=n; //第一种初始化方式 29 p=new double[lines*cols]; 30 } 31 32 Matrix:: Matrix(int n, int m):lines{n},cols{m} //第二种初始化方式 33 { 34 p=new double[lines*cols]; 35 } 36 37 Matrix::Matrix(const Matrix &X) //一个一个数据复制 38 { 39 lines=X.lines; 40 cols=X.cols; 41 p=new double[lines*cols]; 42 for(auto i=0;i<lines*cols;i++) 43 p[i]=X.p[i]; 44 } 45 46 Matrix::~Matrix() 47 { 48 delete[] p; 49 } 50 51 void Matrix::set(const double *pvalue) //一个一个数据传入,pvalue为指针型数据 52 { 53 for(auto i=0;i<lines*cols;i++) 54 p[i]=pvalue[i]; 55 } 56 57 void Matrix::set(int i, int j, int value) 58 { 59 p[i*cols+j]=value; 60 } 61 62 double &Matrix::at(int i, int j) 63 { 64 return p[i*cols+j]; 65 } 66 67 double Matrix::at(int i, int j) const 68 { 69 return p[i*cols+j]; 70 } 71 72 int Matrix::get_lines() const 73 { 74 return lines; 75 } 76 77 int Matrix::get_cols() const 78 { 79 return cols; 80 } 81 82 void Matrix::print() const 83 { 84 using namespace std; 85 86 for(auto i=0;i<lines;i++) 87 for(auto j=0;j<cols;j++) 88 cout<<p[i*cols+j]<<","; 89 cout<<"\n"; 90 } 91 92 93 94 #endif // MATRIX_H
task5.cpp

1
#include <iostream> 2 #include "matrix.hpp" 3 4 int main() 5 { 6 using namespace std; 7 8 double x[] = {1, 2, 3, 4, 5, 6}; 9 10 Matrix m1(3, 2); // 创建一个3×2的矩阵 11 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 12 m1.print(); // 打印矩阵m1的值 13 cout << "the first line is: " << endl; 14 cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; // 输出矩阵m1每行的元素值 15 cout << endl; 16 17 Matrix m2(2, 3); // 创建一个2×3的矩阵 18 m2.set(x); // 用一维数组x的值按行为矩阵m2赋值 19 m2.print(); // 打印矩阵m的值 20 cout << "the first line is: " << endl; 21 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 22 23 cout << endl; Matrix m3(m2); // 用矩阵m2构造新的矩阵m3 24 m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999 25 m3.print(); 26 }

 

 

 

 

总结: 1.成员函数内部需要输出时,要采用 std::cout<<或者在成员函数内部加入 using namespace std;

     2.如果返回函数的引用,要注意函数定义时的写法  double &Matrix :: at(int i, int j)  

posted @ 2021-11-06 22:57  斍奭藇簨  阅读(32)  评论(3)    收藏  举报