实验3

1.实现动态的整型数组类

vector.hpp:

#pragma once
#ifndef VECTOR_INT_HPP
#define VECTOR_INT_HPP
#include<iostream>

class vector_int
{
private:
    int* p;
    int size;
public:
    vector_int(int numble, int val);
    vector_int(const vector_int& a1);
    ~vector_int();
    void show();
    int& at(int index);
};

vector_int::vector_int(int numble,int val=0) {

    using namespace std;
    size = numble;
    p = new int[size];
    for (int i = 0; i < size; i++) {
        p[i] = val;
    }
    cout << "calling the constructor" << endl;
}

vector_int::vector_int(const vector_int& a1) {
    using namespace std;
    size = a1.size;
    p = new int[size];
    for (int i = 0; i < size; i++) {
        p[i] = a1.p[i];
    }
    cout << "calling the copy constructor" << endl;
}

vector_int::~vector_int() {
    using namespace std;
    delete[] p;
    cout << "destroy the constructor" << endl;
}

void vector_int::show() {

    using namespace std;
    for (int i = 0; i < size; i++) {
           cout << p[i] << ", ";
    }
    cout << endl;
    
}

int& vector_int::at(int index) {
    if (index > 0 && index < size)
        return p[index];

}

#endif

task.cpp;

#include "vector_int.hpp"

int main() {
    using namespace std;
    int n=10;
    vector_int x(n);
    x.show();
    vector_int z(n, 6);
    z.show();
    vector_int y(x);
    y.show();
    y.at(0) = 999;
    y.show();
}

运行图:

 2.实现动态矩阵:

matrix.hpp:

pragma once
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include<iostream>

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 { return lines; };
    int get_cols() const { return cols; };
    void print() const; 
private:
    int lines; 
    int cols; 
    double* p; 
};

Matrix::Matrix(int n) {
    p = new double[n * n];
    lines = n;
    cols = n;

}

Matrix::Matrix(int n,int m) {
    p = new double[n * m];
    lines = n;
    cols = m;
}

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

Matrix::~Matrix() {
    using namespace std;
    delete[] p;
    cout << "destroy the constructor" << endl;

}

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

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

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];
}

void Matrix::print()const {
    using namespace std;
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << p[i * cols + j] << ",";
            
        }
        cout << endl;
    }
    

}


#endif

task.cpp:

#include <iostream>
#include "matrix.hpp"
 int main()
{
    using namespace std;
    double x[] = { 1, 2, 3, 4, 5, 6 };
    Matrix m1(3, 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, 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();
}

运行图:

 总结:

1.注意引用时是否改变值。

2.深层复制是值的复制,浅层复制是地址值的复制。

3.任务4和5都是练习动态存储空间和对学过知识的复习。

posted @ 2021-11-07 10:49  社交废物  阅读(24)  评论(2)    收藏  举报