• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
KochiyaSanae
博客园    首页    新随笔    联系   管理    订阅  订阅
实验三 类和对象Ⅱ

实验任务4

vector_int.hpp

#ifndef VECTOR_HPP
#define VECTOR_HPP

#include<iostream>
#include<cassert>
using namespace std;

class Vector_int
{
public:
    Vector_int(int x);
    Vector_int(int x, int y);
    Vector_int(const Vector_int& test);
    ~Vector_int();
    int& at(int num);
private:
    int size;
    int* p;
};
Vector_int::Vector_int(int x) :size(x)//一参数初始化
{
    cout << "dynamic create arry..." << endl;
    p = new int[x];
    for (auto i = 0; i < size; i++)
    {
        p[i] = 0;
    }
}
Vector_int::Vector_int(int x, int y) :size(x)//二参数初始化
{
    cout << "dynamic create arry..." << endl;
    p = new int[x];
    for (auto i = 0; i < size; i++)
    {
        p[i] = y;
    }
}
Vector_int::Vector_int(const Vector_int& test) :size(test.size)//深拷贝复制构造函数
{
    p = new int[size];
    for (auto i = 0; i < size; i++)
    {
        p[i] = test.p[i];
    }
}
Vector_int::~Vector_int()//析构函数
{
    cout << "deleting..." << endl;
    delete[] p;
}
int& Vector_int::at(int num)
{
    assert(num >= 0 && num < size);
    return p[num];
}
#endif

源文件

#include<iostream>
#include"Vector_int.hpp"
using namespace std;
int main()
{
    int a;
    cin >> a;
    Vector_int x(a);
    for (auto i = 0; i < a; i++)
    {
        cout << x.at(i);
    }
    cout << endl;
    Vector_int y(a, 8);
    for (auto i = 0; i < a; i++)
    {
        cout << y.at(i);
    }
    cout << endl;
    Vector_int z(y);
    for (auto i = 0; i < a; i++)
    {
        cout << z.at(i);
    }
    cout << endl;
    z.at(0) = 2333;
    cout << z.at(0) << endl;
    return 0;
}

结果

 实验任务5

头文件

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
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 = cols = n;
    p = new double[n * n];
}

Matrix::Matrix(int n, int m)//两个参数构造参数
{
    lines = n;
    cols = m;
    p = new double[n * m];
}

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++)
    {
        if (pvalue == NULL)
        {
            break;
        }
        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)
{
    double& res = p[i * lines + j];
    return res;
}
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
{
    for (int i = 0; i < lines * cols; i++)
    {
        cout << p[i];
        if ((i + 1) % cols == 0)
        {
            cout << endl;
        }
        else cout << ", ";
    }
}
#endif

源文件

#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();
}

实验结果

 

posted on 2021-11-08 23:27  KochiyaSanae  阅读(16)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3