ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

面向对象->实验报告三(C++)

2021-11-07 03:01:36  阅读:149  来源: 互联网

标签:const Matrix int lines cols C++ 面向对象 Vector 实验报告


欢迎访问我的个人博客:

xzajyjs.cn


task4


实验要求


main.cpp

#include <iostream>
#include "vector_int.hpp"
int main(){
    using namespace std;
    int x_len, y_len;
    cout << "请输入x的长度:";
    cin >> x_len;
    Vector_int x(x_len); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间
    x.show('x');
    cout << "请输入y的长度:";
    cin >> y_len;
    Vector_int y(y_len, 6); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间, 初始值均为6
    y.show('y');
    Vector_int z(y); // 用已经存在的对象y构造新的对象z(深拷贝的实现)
    z.show('z');
    z.at(0) = 999; // 通过at()方法访问对象y中索引为0的数据项

    cout << "---以下是深拷贝的检验---\n";
    y.show('y');
    z.show('z');
    return 0;
}

vector_int.hpp

#ifndef CPP_VECTOR_INT_HPP
#define CPP_VECTOR_INT_HPP
using namespace std;
class Vector_int{
private:
    int size;
    int *p;
public:
    //Vector_int(int n);
    Vector_int(int n, int value = 0);	// 在这里将默认值改为0,因此上一行省去
    ~Vector_int();
    Vector_int(const Vector_int &_temp);
    int &at(int wh);
    void show(char which);
};
//Vector_int::Vector_int(int n):size(n) {
//    p = new int[size];
//    cout << "created..." << endl;
//}
Vector_int::Vector_int(int n, int value):size(n) {
    p = new int[size];
    for(int i = 0;i < size;++i)
        p[i] = value;
    cout << "created..." << endl;
}
Vector_int::~Vector_int() {
    delete[] p;
    cout << "deleted..." << endl;
}
// 深拷贝的实现
Vector_int::Vector_int(const Vector_int &_temp):size(_temp.size) {
    p = new int[size];
    for(int i = 0;i < _temp.size;++i)
        p[i] = _temp.p[i];
}
int &Vector_int::at(int wh) {
    assert(wh >= 0 && wh < size);
    return p[wh];
}
void Vector_int::show(char which) {
    cout << which << ":\t";
    for(int i = 0; i < size; ++i)
        cout << p[i] << "\t";
    cout << endl;
}

#endif //CPP_VECTOR_INT_HPP

运行结果截图

小结

  • 相较于task2,每个数组成员是整形而不是自定义的Point类型。在各方面都要简单一些
  • 要注意深拷贝和默认的浅拷贝的含义的区别。在主函数中对深拷贝的效果进行了检验

task5


实验要求


main.cpp(原)

#include <iostream>
#include "matrix.hpp"
int main()
{
    using namespace std;
    double x[] = {1, 2, 3, 4, 5, 6};
    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; // 输出矩阵m1第1行两个元素的值
    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); // 用矩阵m2构造新的矩阵m3
    m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999
    m3.print();
}

main.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(4, 3);
    m1.set(x);
    m1.print();
    cout << "m1 has " << m1.get_lines() << " lines and " << m1.get_cols() << " cols.\n\n";
    cout << "the third line is: " << endl;
    cout << m1.at(2, 0) << " " << m1.at(2, 1) << " " << m1.at(2, 2) << endl;
    cout << endl;

    Matrix m2(3, 4);
    m2.set(x);
    m2.print();
    cout << "the first line is: " << endl;
    cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << " " << m2.at(0, 3) << endl;
    cout << endl;

    Matrix m3(m2);
    m3.set(2, 3, 233);
    m3.print();
    cout << endl;

    Matrix m4(3);
    double temp[] = {3, 4, 5, 6, 7, 8, 9, 1, 2};
    m4.set(temp);
    m4.print();
}

Matrix.hpp

#ifndef CPP_MATRIX_HPP
#define CPP_MATRIX_HPP
using namespace std;
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; // 矩阵列数
    double *p; // 指向存放矩阵数据的内存块的首地址
};

Matrix::Matrix(int n):lines(n), cols(n) {
    p = new double[lines*cols];
}

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) {
    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 {
    int k = 0;
    for(int i = 0;i < lines;++i){
        for(int j = 0;j < cols;++j, ++k)
            cout << p[k] << ", ";
        cout << "\b\b";
        cout << endl;
    }
}

#endif //CPP_MATRIX_HPP

运行结果截图


小结

  • 考虑到函数的复杂度,以一维数组的方式来保存数据。
  • 练习了深拷贝

标签:const,Matrix,int,lines,cols,C++,面向对象,Vector,实验报告
来源: https://www.cnblogs.com/xzajyjs/p/15518942.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有