欢迎使用皮肤 Geek|
2021-11-03 10:27阅读: 32评论: 3推荐: 0

实验三 类和对象Ⅱ

实验任务4

Vector_int.cpp源码

复制代码
#ifndef VECTOR_INT_HPP
#define VECTOR_INT_HPP

#include <iostream>
#include <cassert>
using std ::cout;
using std::endl;

// Vector_int类的声明
class Vector_int
{
private:
    int size;
    int *array = nullptr;

public:
    Vector_int(int n, int value = 0);
    Vector_int(const Vector_int &p);
    ~Vector_int();

    int capacity(); //返回数组大小
    int &at(int index);
};

// Vector_int类的定义
Vector_int::Vector_int(int n, int value) : size{n}
{
    array = new int[size];
    for (int i = 0; i < size; i++)
    {
        array[i] = value;
    }
}

Vector_int::Vector_int(const Vector_int &p)
{
    size = p.size;
    array = new int[size];

    for (int i = 0; i < size; i++)
    {
        array[i] = p.array[i];
    }
}

Vector_int::~Vector_int()
{
    delete[] array;
}

int Vector_int::capacity()
{
    return size;
}

int &Vector_int::at(int index)
{
    assert(index >= 0 && index < size);
    return array[index];
}

#endif
复制代码

task4.cpp源码

复制代码
#include <iostream>
#include "Vector_int.hpp"
int main()
{
    int n = 10;
    Vector_int x(n); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间

    Vector_int z(n, 6); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间, 初始值均为6

    Vector_int y(x); // 用已经存在的对象x构造新的对象y
    y.at(0) = 999;   // 通过at()方法访问对象y中索引为0的数据项

    for (int i = 0; i < x.capacity(); i++)
        cout << x.at(i) << " ";
    cout << "\b" << endl;

    for (int i = 0; i < z.capacity(); i++)
        cout << z.at(i) << " ";
    cout << "\b" << endl;

    for (int i = 0; i < y.capacity(); i++)
        cout << y.at(i) << " ";
    cout << "\b" << endl;

    return 0;
}
复制代码

实验结果截图

 


 

实验任务5

Matrox.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, 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::Matrix(int n) : lines(n), cols(n)
{
    double **a = new double *[n];
    for (int i = 0; i < n; i++)
        a[i] = new double[n];

    p = *a;
}

Matrix::Matrix(int n, int m) : lines(n), cols(m)
{
    double **a = new double *[n];
    for (int i = 0; i < n; i++)
        a[i] = new double[m];

    p = *a;
}

Matrix::Matrix(const Matrix &X)
{
    lines = X.lines;
    cols = X.cols;

    double **a = new double *[lines];
    for (int i = 0; i < lines; i++)
        a[i] = new double[cols];
    p = *a;

    for (int i = 0; i < lines * cols; i++)
        p[i] = X.p[i];
}

Matrix::~Matrix()
{
    delete[] p;
    std::cout << "Matrix is being destroyed... \n";
}

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 * lines + j] = value;
}

double &Matrix::at(int i, int j)
{
    return p[i * lines + j];
}

double Matrix::at(int i, int j) const
{
    return p[i * lines + j];
}

int Matrix::get_lines() const
{
    return lines;
}

int Matrix::get_cols() const
{
    return cols;
}

void Matrix::print() const
{
    using std::cout;
    int j = 0;
    for (int i = 0; i < lines * cols; i++)
    {
        cout << p[i] << " ";
        j++;
        if (j >= cols)
        {
            cout << "\b\n";
            j = 0;
        }
    }
}
#endif
复制代码

task5.cpp源码

复制代码
#include <iostream>
#include "matrix.hpp"

int main()
{
    using namespace std;

    double x[] = {10, 20, 30, 40, 50, 60};

    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, 90);
    m3.print();
}
复制代码

实验结果截图

 


 

实验总结

 

本文作者:请去看诡秘之主

本文链接:https://www.cnblogs.com/xjy881/p/15502523.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   请去看诡秘之主  阅读(32)  评论(3编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起