C++ 实验三 任务报告

📝实验三 任务报告

✨实验内容

本次实验比较仓促,有很多想写清楚没来得及,等班级考完再慢慢补充.

🕐任务一~三

🕓任务四

模拟实验任务2,不使用标准库模板类vector,自己动手设计并实现一个动态的整型数组类Vector_int
使其支持以下要求:

  • 支持在创建int型数组对象时,指定其大小
  • 支持在创建int型数组对象时,指定其大小,并将数组对象中每个数据项初始化到特定的值value
  • 支持用已经存在的int型数组对象x,来构造新的int型数组对象y(要求实现深复制)
  • 提供方法at()能够支持诸如x.at(i)这样通过索引访问动态int型数组对象中第i个数据项
  • 析构函数,释放占用的内存资源
  • 在构造和析构函数里,增加打印输出信息。运行程序时,帮助观察资源是否正确释放。
    即在测试代码中,支持形如下面的代码操作:
Vector_int x(n); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项
空间
Vector_int x(n, 6); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项
空间, 初始值均为6
Vector_int y(x); // 用已经存在的对象x构造新的对象y
y.at(0) = 999; // 通过at()方法访问对象y中索引为0的数据项

要求:
① 设计并实现动态整型数组类Vector_int,保存在文件vector_int.hpp
② 编写测试代码文件task4.cpp,测试其构造函数接口、复制构造函数、at()方法等,是否都正常使用,
是否实现了深复制。

📃代码:

task4.hpp

#ifndef EXP3_TASK4_HPP
#define EXP3_TASK4_HPP

#include <iostream>

using namespace std;

class vector_int
{
private:
    int *p_int;
    int capacity;
public:
    explicit vector_int(int n, int num = 0);//申请n个int的空间,并都赋值为num

    vector_int(const vector_int &obj);//复制构造函数

    vector_int(const int *arr, int len);//深拷贝

    ~vector_int()//析构函数
    {
        free(p_int);
        cout << "Memory is free" << endl;
    }

    int get_capacity() const { return capacity; }//返回容量

    int at(int index) const { return p_int[index]; }//读取某一位

    void assign(int n, int num);//给某一位赋值

    void push_back(int num);//在末尾插入一个数字

    void print();//输出数组

    void print_address() const { cout << "Address:" << &p_int << endl; }//输出数组首地址
};

vector_int::vector_int(int n, int num)
{
    capacity = n;
    p_int = new int[n];
    for (int i = 0; i < n; ++i)
        p_int[i] = num;
    cout << "Created an array with capacity:" << capacity << endl;
}

vector_int::vector_int(const vector_int &obj)
{
    capacity = obj.capacity;
    p_int = new int[capacity];
    for (int i = 0; i < capacity; ++i)
        p_int[i] = obj.at(i);
    cout << "Created an array with capacity:" << capacity << endl;
}

vector_int::vector_int(const int *arr, int len)
{
    capacity = len;
    p_int = new int[capacity];
    for (int i = 0; i < len; ++i)
        p_int[i] = arr[i];
    cout << "Created an array with capacity:" << capacity << endl;
}

void vector_int::assign(int n, int num)
{
    if (n < capacity)
        p_int[n] = num;
    else
    {
        int i = 0;
        int *temp = new int[n];
        for (i = 0; i < capacity; ++i)
            temp[i] = p_int[i];
        for (; i < n-1; ++i)
            temp[i] = 0;
        temp[i] = num;
        free(p_int);
        capacity = n;
        p_int = temp;
    }
}

void vector_int::push_back(int num)
{
    int *temp = new int[capacity + 1];
    for (int i = 0; i < capacity; ++i)
        temp[i] = p_int[i];
    temp[capacity] = num;
    free(p_int);
    p_int = temp;
    capacity++;
}

void vector_int::print()
{
    for (int i = 0; i < capacity; ++i)
        cout << p_int[i] << " ";
    cout << endl;
}

#endif //EXP3_TASK4_HPP

🎨输出:

Feb address: 0x61fd20
x1:
Created an array with capacity:7
1 1 2 3 5 8 13
Address:0x61fd10
x2:
Created a array with capacity:7
1 1 2 3 5 8 13
Address:0x61fd00
1 1 2 3 5 8 13 21
Address:0x61fd00
x3:
Created a array with capacity:8
8 8 8 8 8 8 8 8
99 8 8 8 8 8 8 8 0 10
Memory is free
Memory is free
Memory is free

进程已结束,退出代码为 0

🤓思考:

深拷贝,浅拷贝主要用于指针操作,其他的情况可以不用考虑.

也可以看看这篇文章

https://blog.csdn.net/u010700335/article/details/39830425

🕔任务五

实现一个动态矩阵类Matrix,类Matrix的声明见文件Matrix.hpp

  • 实现类Matrix的定义
  • 使用task5.cpp测试矩阵类Matrix。

📃代码:

task5.hpp

#ifndef task5
#define task5

#include <iostream>

using namespace std;

class Matrix
{
public:
    Matrix(int n) : lines(n), cols(n) { p = new double[n * n]; }; // 构造函数,构造一个n*n的矩阵
    Matrix(int n, int m) : lines(n), cols(m) { p = new double[n * m]; }; // 构造函数,构造一个n*m的矩阵
    Matrix(const Matrix &X) : lines(X.lines), cols(X.cols)// 复制构造函数,使用已有的矩阵X构造
    {
        p = new double[X.lines * X.cols];
        for (int i = 0; i < X.lines * X.cols; ++i)
            p[i] = X.p[i];
    }

    ~Matrix() { free(p); }; //析构函数
//  ~Matrix()=default; //析构函数
    void set(const double *pvalue)
    {
        p = new double[lines * cols];
        for (int i = 0; i < lines * cols; ++i)
            p[i] = pvalue[i];
    }; // 用pvalue指向的连续内存块数据按行为矩赋值
    void set(int i, int j, int value){p[i * cols + j] = value;}//设置矩阵第i行第j列元素值为value
    double &at(int i, int j) { return p[i * cols + j]; } //返回矩阵第i行第j列元素的引用
    double at(int i, int j) const { return p[i * cols + j]; }// 返回矩阵第i行第j列元素的值
    int get_lines() const { return lines; } //返回矩阵行数
    int get_cols() const { return cols; } //返回矩列数
    void print() const
    {
        for (int i = 0; i < lines; ++i)
        {
            for (int j = 0; j < cols; ++j)
                cout << p[i * cols + j] << " ";
            cout << endl;
        }
    } // 按行打印输出矩阵
private:
    int lines; // 矩阵行数
    int cols; // 矩阵列数
    double *p; // 指向存放矩阵数据的内存块的首地址
};
#endif

task5.cpp

#include <iostream>
#include "task5.hpp"
int main()
{
    using namespace std;
    double x[] = {0,2,4,8,1,3,5,7};
    Matrix m1(2, 4); // 创建一个2×4的矩阵
    m1.set(x); // 用一维数组x的值按行为矩阵m1赋值
    cout<<"m1:"<<endl;
    m1.print(); // 打印矩阵m1的值
    cout << "the first line is: " << endl;
    cout << m1.at(0, 0) << " " << m1.at(0, 1)<< " " << m1.at(0, 2)<< " " << m1.at(0, 3) << endl; // 输出矩阵m1第1行两元素的值
    cout << endl;
    Matrix m2(4, 2);
    m2.set(x);
    cout<<"m2:"<<endl;
    m2.print();
    cout << "the first line is: " << endl;
    cout << m2.at(0, 0) << " " << m2.at(0, 1) << endl;
    cout << endl;
    Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
    m3.set(0, 0, 9); // 将矩阵m3第0行第0列元素值设为9
    cout<<"m3:"<<endl;
    m3.print();
    return 0;
}

🎨输出:

m1:
0 2 4 8
1 3 5 7
the first line is:
0 2 4 8

m2:
0 2
4 8
1 3
5 7
the first line is:
0 2

m3:
9 2
4 8
1 3
5 7

进程已结束,退出代码为 0

🤓思考:

析构函数很重要,但是我现在一般都是直接交给编译器来完成,也完成的很完美.但是想要再进一步还需要多去尝试自己写.

posted @ 2021-11-09 17:17  李柳星  阅读(66)  评论(5编辑  收藏  举报