实验3 类和对象Ⅱ
实验任务4:
不使用标准库模板类vector,自己动手设计并实现一个动态的整型数组类Vector_int。
程序包括vector_int.hpp和task4.cpp。
vector_int.hpp:
1 #ifndef VECTOR_INT_HPP 2 #define VECTOR_INT_HPP 3 #include<iostream> 4 #include<cassert> 5 #define value 1 6 using std::cout; 7 using std::endl; 8 class Vector_int { 9 public: 10 Vector_int(int n, int m = value); 11 Vector_int(const Vector_int& a); 12 ~Vector_int(); 13 void print(); 14 int& at(int index); 15 private: 16 int size; 17 int* p; 18 }; 19 Vector_int::Vector_int(int n, int m) :size(n) { 20 cout << "constructor called." << endl; 21 p = new int[size]; 22 for(int i=0;i<size;i++) 23 { 24 p[i] = m; 25 } 26 27 }; 28 Vector_int::Vector_int(const Vector_int& a) :size{a.size} 29 { 30 cout << "copying constructor called." << endl; 31 p = new int[size]; 32 for (int i = 0; i < size; i++) 33 { 34 p[i] = a.p[i]; 35 } 36 } 37 Vector_int::~Vector_int() 38 { 39 cout << "delete" << endl; 40 delete[] p; 41 } 42 inline void Vector_int::print() 43 { 44 for(int i=0;i<size;i++) 45 { 46 cout<<p[i]<<","; 47 } 48 cout << endl; 49 50 } 51 inline int& Vector_int::at(int index) 52 { 53 assert(index >= 0 && index < size); 54 return p[index]; 55 56 } 57 #endif
task4.cpp:
1 #include <stdio.h> 2 #include "vector_int.hpp" 3 int main() 4 { 5 int n; 6 std::cin >> n; 7 Vector_int x1(n); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间 8 x1.print(); 9 Vector_int x2(n, 6); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间, 初始值均为6 10 x2.print(); 11 Vector_int y(x2); // 用已经存在的对象x构造新的对象y 12 y.print(); 13 y.at(0) = 888; // 通过at()方法访问对象y中索引为0的数据项 14 y.print(); 15 return 0; 16 }
运行截图:

实验任务五:
实现一个动态矩阵类Matrix,类Matrix的声明见文件Matrix.hpp。
程序包括Matrix.hpp和task5.cpp。
Matrix.hpp:
1 #ifndef MATRIX_H 2 #define MATRIX_H 3 4 #include <iostream> 5 #include <cassert> 6 7 class Matrix 8 { 9 public: 10 Matrix(int n); // 构造函数,构造一个n*n的矩阵 11 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 12 Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 13 ~Matrix(); //析构函数 14 void set(const double *pvalue); // 用pvalue指向的连续内存块数据为矩阵赋值 15 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 16 double &at(int i, int j); //返回矩阵第i行第j列元素的引用 17 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 18 int get_lines() const; //返回矩阵行数 19 int get_cols() const; //返回矩列数 20 void print() const; // 按行打印输出矩阵 21 private: 22 int lines; // 矩阵行数 23 int cols; // 矩阵列数 24 double *p; // 指向存放矩阵数据的内存块的首地址 25 }; 26 27 28 // 类Matrix的实现:待补足 29 // ××× 30 Matrix::Matrix(int n) :lines{n}, cols{ n } 31 { 32 p = new double[n * n]; 33 } 34 inline Matrix::Matrix(int n, int m):lines(n),cols(m) 35 { 36 p = new double[n * m]; 37 } 38 inline Matrix::Matrix(const Matrix& X):lines{X.lines},cols{X.cols} 39 { 40 p = new double[lines * cols]; 41 for(int i=1;i<=lines*cols;i++) 42 p[i] = X.p[i]; 43 } 44 inline Matrix::~Matrix() 45 { 46 delete[]p; 47 } 48 inline void Matrix::set(const double* pvalue) 49 { 50 for (int i = 0; i < lines * cols; i++) 51 p[i] = pvalue[i]; 52 } 53 inline void Matrix::set(int i, int j, int value) 54 { 55 p[i * cols + j] = value; 56 } 57 inline double& Matrix::at(int i, int j) 58 { 59 return p[i * cols + j]; 60 } 61 inline double Matrix::at(int i, int j) const 62 { 63 return p[i*cols+j]; 64 } 65 inline int Matrix::get_lines() const 66 { 67 return lines; 68 } 69 inline int Matrix::get_cols() const 70 { 71 return cols; 72 } 73 inline void Matrix::print() const 74 { 75 for (int i = 0; i < lines; i++) 76 { 77 for (int j = 0; j < cols; j++) 78 std::cout << p[i*cols+j]<<","; 79 std::cout<<std::endl; 80 } 81 } 82 #endif
task5.cpp:
1 #include <iostream> 2 #include "matrix.hpp" 3 4 int main() 5 { 6 using namespace std; 7 8 double x[] = { 6,7,8,9,10,11 }; 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; 15 cout << endl; 16 17 Matrix m2(2, 3); 18 m2.set(x); 19 m2.print(); 20 cout << "the first line is: " << endl; 21 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 22 cout << endl; 23 24 Matrix m3(m2); 25 m3.set(0, 0, 888); 26 m3.print(); 27 }
运行截图:


浙公网安备 33010602011771号