实验3 类与对象Ⅱ
实验任务4
实现动态整型数组类Vector_int
<vector_int.hpp>
#ifndef VECTOR_INT_HPP #define VECTOR_INT_HPP #include <iostream> #include <cassert> using namespace std; class Vector_int { public: Vector_int(int n); Vector_int(int n, int vlue); Vector_int(const Vector_int &vi); ~Vector_int(); int &at(int index); //返回下标为index的元素引用 private: int size; //数组大小 int *p; //该指针指向分配的内存空间 }; Vector_int::Vector_int(int n) : size(n) { p = new int[n]; cout << "dynamic int array creating..." << endl; } Vector_int::Vector_int(int n, int vlue) : size(n) { cout << "dynamic int array with initial value creating..." << endl; p = new int[n]; for (auto i = 0; i < n; i++) p[i] = vlue; } Vector_int::Vector_int(const Vector_int &vi) : size(vi.size) { cout << "copy constructor is called" << endl; p = new int[size]; for (auto i = 0; i < size; i++) p[i] = vi.p[i]; } Vector_int::~Vector_int() { cout << "destructor is called " << endl; delete[] p; //使用new []申请的内存应该用delete []释放 } int &Vector_int::at(int index) { assert(index >= 0 && index < size); return p[index]; } #endif
<task4.cpp>
#include<iostream> #include"vector_int.hpp" using namespace std; int main() { int n; cin >> n; Vector_int x(n); cout << x.at(0) << endl; Vector_int y(n,6); cout << y.at(0) << endl; Vector_int z(y); cout << z.at(0) << endl; y.at(0) = 4; cout << y.at(0) << endl; cout << z.at(0) <<endl; }
测试结果:
可以看到,构造函数,复制构造函数,at()方法,析构函数正常执行,且复制构造实现了深复制。
实验任务5
实现动态矩阵类Matrix
<Matrix.hpp>
#ifndef MATRIX_H #define MATRIX_H #include <iostream> #include <cassert> using std::cout; using std::endl; class Matrix { public: Matrix(int n); // 构造函数,构造一个n*n的矩阵 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 ~Matrix(); //析构函数 void set(const double *pvalue); // 用pvalue指向的连续内存块数据为矩阵赋值 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value double &at(int i, int j); //返回矩阵第i行第j列元素的引用 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 int get_lines() const; //返回矩阵行数 int get_cols() const; //返回矩列数 void print() const; // 按行打印输出矩阵 private: int lines; // 矩阵行数 int cols; // 矩阵列数S double *p; // 指向存放矩阵数据的内存块的首地址 }; //委托构造 Matrix::Matrix(int n) : Matrix(n, n) {} Matrix::Matrix(int n, int m) : lines(n), cols(m) { p = new double[lines * cols]; } Matrix::Matrix(const Matrix &X) : lines(X.lines), cols(X.cols) { p = new double[lines * cols]; for (int i = 0; i < lines * cols; i++) { p[i] = X.p[i]; } } Matrix::~Matrix() { delete[] p; } void Matrix::set(const double *pvalue) { for (int i = 0; i < lines * cols; i++) { p[i] = pvalue[i]; } } void Matrix::set(int i, int j, int value) { p[i * cols + j] = value; } double &Matrix::at(int i, int j) { assert(i >= 0 && i < lines && j >= 0 && j < cols); return p[i * cols + j]; } double Matrix::at(int i, int j) const { assert(i >= 0 && i < lines && j >= 0 && j < cols); 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 << "\b\b \n"; } } #endif
<task5.cpp>
#include <iostream> #include "matrix.hpp" int main() { using namespace std; double x[] = {2, 4, 8, 16, 18, 26}; Matrix m1(3, 2); // 创建一个3×2的矩阵 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 m1.print(); // 打印矩阵m1的值 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, 250); m3.print(); }
测试结果:
分析总结
- 使用new [ ] 申请的内存应用 delete [ ]释放
- 委托构造函数的使用,减少代码冗余
- 用一维数组来处理二维空间