实验三 类和对象Ⅱ
任务4 源代码
vector_int.hpp
#pragma once #include <iostream> #include <cassert> class Vector_int { public: Vector_int(int n, int init_num = 0); Vector_int(const Vector_int &vec0); ~Vector_int(); int& at(int pos); private: int *vec; int capacity; }; Vector_int::Vector_int(int n, int init_num): capacity(n) { std::cout << "constructor called" << std::endl; this->vec = new int[this->capacity]; for (int i = 0; i < this->capacity; i++) this->vec[i] = init_num; } Vector_int::Vector_int(const Vector_int &vec0) { std::cout << "copy_constructor called" << std::endl; this->capacity = vec0.capacity; this->vec = new int[this->capacity]; for (int i = 0; i < this->capacity; i++) this->vec[i] = vec0.vec[i]; } Vector_int::~Vector_int() { std::cout << "destructor called" << std::endl; delete[] this->vec; this->vec = nullptr; this->capacity = 0; } int& Vector_int::at(int pos) { assert(pos < this->capacity); return this->vec[pos]; }
task4.cpp
#include "vector_int.hpp" int main() { int n = 10; Vector_int x1(n); Vector_int x2(n, 6); Vector_int y(x2); y.at(0) = 999; for (int i = 0; i < n; i++) std::cout << x1.at(i) << " "; std::cout << std::endl; for (int i = 0; i < n; i++) std::cout << x2.at(i) << " "; std::cout << std::endl; for (int i = 0; i < n; i++) std::cout << y.at(i) << " "; std::cout << std::endl; return 0; }
任务5 源代码
Matrix.hpp
#ifndef MATRIX_H #define MATRIX_H #include <iostream> #include <cassert> 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, double 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; // 矩阵列数 double *p; // 指向存放矩阵数据的内存块的首地址 }; // 类Matrix的实现:待补足 // ××× Matrix::Matrix(int n): lines(n), cols(n) { this->p = new double[this->lines * this->cols]; } Matrix::Matrix(int n, int m): lines(n), cols(m) { this->p = new double[this->lines * this->cols]; } Matrix::Matrix(const Matrix &X): lines(X.lines), cols(X.cols) { this->p = new double[this->lines * this->cols]; for (int i = 0; i < this->lines * this->cols; i++) p[i] = X.p[i]; } Matrix::~Matrix() { delete[] this->p; this->p = nullptr; } void Matrix::set(const double *pvalue) { int tail = 0; for (int i = 0; i < this->lines * this->cols; i++) p[i] = pvalue[tail++]; } void Matrix::set(int i, int j, double value) { p[i * this->cols + j] = value; } double& Matrix::at(int i, int j) { return p[i * this->cols + j]; } double Matrix::at(int i, int j) const { return p[i * this->cols + j]; } int Matrix::get_lines() const { return this->lines; } int Matrix::get_cols() const { return this->cols; } void Matrix::print() const { for (int i = 0; i < this->lines; i++) { for (int j = 0; j < this->cols; j++) std::cout << this->p[i * this->cols + j] << " "; std::cout << std::endl; } } #endif
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}; Matrix m1(3, 4); // 创建一个3×2的矩阵 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 m1.print(); // 打印矩阵m1的值 cout << "the first line is: " << endl; for (int i = 0; i < 4; i++) cout << m1.at(0, i) << " "; cout << endl << endl; Matrix m2(4, 3); m2.set(x); m2.print(); cout << "the first line is: " << endl; for (int i = 0; i < 3; i++) cout << m2.at(0, i) << " "; cout << endl << endl; Matrix m3(m2); m3.set(0, 0, 999); m3.print(); }