实验三(类与对象2)

Vector_int.hpp

#include<iostream>
using namespace std;
class Vector_int {
public:
    Vector_int(int len) {
        size = len;
        a = new int[size];
        cout << "Constructor called." << endl;
    }
    Vector_int(int len,int value) {
        size = len;
        a = new int[size];
        for (int i = 0;i < size;i++) {
            a[i] = value;
        }
        cout << "Constructor called." << endl;
    }
    Vector_int(const Vector_int& p) {
        size = p.size;
        a = new int[size];
        for (int i = 0;i < size;i++) {
            a[i] = p.a[i];
        }
    }
    int &at(int i) {
        return a[i];
    }
    void print() {
        for (int i = 0;i < size;i++) {
            cout << a[i] << " ";
        }
        cout << endl;
    }
    ~Vector_int() {
        delete []a;
        cout << "Destructor called." << endl;
    }
private:
    int size;
    int *a;
};

task4.cpp

#include<iostream>
#include<cassert>
#include"Vector_int.hpp"
int main() {
    using namespace std;
    Vector_int a(4);
    a.print();
    Vector_int x(4,6);
    x.print();
    Vector_int y(x);
    y.at(0) = 999;
    y.print();
}

Matrix.hpp

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <cassert>
using namespace std;
class Matrix
{
public:
    Matrix(int n);
    Matrix(int n, int m);
    Matrix(const Matrix& X);
    ~Matrix();
    void set(const double* pvalue);
    void set(int i, int j, int value);
    double& at(int i, int j);
    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[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 < X.lines * X.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 {
    for (int i = 0;i < lines;i++) {
        for (int j = 0;j < cols-1;j++) {
            cout << p[i * cols + j] << ",";
        }
        cout << p[i * cols + cols-1];
        cout << endl;
    }
}
#endif

task5.hpp

#include <iostream>
#include "Matrix.hpp"

int main()
{
    using namespace std;

    double x[] = { 1, 2, 3, 4, 5, 6, 7, 8 };

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

    Matrix m2(2, 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) << endl;
    cout << endl;

    Matrix m3(m2);
    m3.set(0, 0, 999);
    m3.print();
}

 

posted @ 2021-11-08 16:23  玖馆不打烊  阅读(31)  评论(3)    收藏  举报