实验3 类和对象Ⅱ

#ifndef VECTOR_INT_HPP
#define VECTOR_INT_HPP

#include <iostream>
#include <cassert>

using namespace std;

class Vector_int
{
public:
    Vector_int(){};
    Vector_int(int n);
    Vector_int(int n, int x);
    Vector_int(const Vector_int &p);
    ~Vector_int();
    int &at(int index);

private:
    int size;
    int *p;
};

Vector_int::Vector_int(int n) : size(n)
{
    p = new int[n];
    for(int i = 0;i<n;i++)
    {
        p[i] = 0;
    }
}
Vector_int::Vector_int(int n, int x) : size(n)
{
    p = new int[n];
    for (int i = 0; i < n; i++)
    {
        p[i] = x;
    }
}
Vector_int::Vector_int(const Vector_int &p) : size(p.size)
{
    this->p = new int[size];
    for (int i = 0; i < size; i++)
    {
        this->p[i] = p.p[i];
    }
}
Vector_int::~Vector_int()
{
    delete[] p;
}

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

#endif

 

#include "Vector_int.hpp"

using namespace std;

int main()
{
    Vector_int x(3);
    cout << "x[2]=" << x.at(2)<< endl;

    Vector_int y(4, 6);
    cout << "y[2]=" << y.at(2)<< endl;

    Vector_int z(y);
    cout << "z[2]=" << z.at(2) << endl;

    y.at(0) = 999;
    cout << "y[0]=" << y.at(0) << endl;
}

 

#ifndef MATRIX_HPP
#define MATRIX_HPP

#include <iostream>
 
using namespace std;

class Matrix
{
public:
    Matrix(){}
    Matrix(int n);
    Matrix(int n,int m);
    Matrix(const Matrix &p);
    ~Matrix();
    void set(const double *pvalue);
    void set(int i,int j, double valude);
    double &at(int i,int j) const;
    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[n*n];
}
Matrix::Matrix(int n,int m)
{
    lines = n;
    cols = m;
    p = new double[n*m];
}

Matrix::Matrix(const Matrix &p):lines(p.lines),cols(p.cols)
{
    this->p = new double[lines*cols];
    for(int i = 0;i<lines*cols;i++)
    {
        this->p[i] =p.p[i] ;
    }
}

Matrix::~Matrix()
{
    delete[] p;
}

void Matrix::set(const double *pvalue)
{
    for(int i= 0;i<lines*cols;i++)
    {
        if(pvalue[i])
        {
            p[i]=pvalue[i];
        }
    }
}

void Matrix::set(int i,int j, double valude)
{
    p[cols*i+j] = valude;
}

double &Matrix::at (int i,int j)const
{
    return p[cols*i+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;i++)
    {
        for(int j = 0 ; j<cols;j++)
        {
            cout << p[cols*i+j];
            if(j<cols-1) cout << ",";
        }
        cout << endl;
    }
}


#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;
    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, 999);
    m3.print();
}

 

posted @ 2021-11-08 20:11  HUYEZ  阅读(28)  评论(3编辑  收藏  举报