实验4 类与数组、指针

实验任务5:

vectorInt.hpp:

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

class vectorInt {
public:
    vectorInt(int n);
    vectorInt(int n, int value);
    vectorInt(const vectorInt& vi);
    ~vectorInt();

    int &at(int index);
    int get_size() const { return size; }
    friend void output(vectorInt &v);
private:
    int size;
    int* p;
    static int count;
};

int vectorInt::count = 1;

vectorInt::vectorInt(int n):size{n}
{
    cout << "constructor " << count << " called.\n";
    count++;
    p = new int[n];
}

vectorInt::vectorInt(int n, int value) :size{ n }
{
    cout << "constructor " << count << " called.\n";
    count++;
    p = new int[n];
    for (auto i = 0;i < size;++i)
        p[i] = value;
}

vectorInt::vectorInt(const vectorInt& vi) :size{ vi.size }
{
    cout << "copy constructor called." << endl;
    p= new int[size];
    for (auto k = 0;k < size;++k)
        p[k] = vi.p[k];
}

vectorInt::~vectorInt()
{
    cout << "destructor called." << endl;
    delete[] p;
}

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

void output(vectorInt &v)
{
    for(auto i=0;i<v.size-1;++i)
    {
        cout << v.at(i) << ", ";
    }
    cout << v.at(v.size-1) << endl;
}

task5.cpp:

void test() {
    using namespace std;

    int n;
    cin >> n;

    vectorInt x1(n);
    for (auto i = 0; i < n; ++i)
        x1.at(i) = i * i;

    output(x1);

    vectorInt x2(n, 42);
    vectorInt x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;

    output(x2);
    output(x3);
}

int main() {
    test();
}

运行结果:

此代码经过修改运行出正确的结果,但错误代码中output函数的声明为friend void output(vectorInt v);错误代码运行的结果如下:

在错误代码中忽略每组数后多余的逗号的错误,最明显与答案结果不同的是output函数运行比正常的在执行时多了copy constructor called和destructor called.。在加了引用的

符号后改为viod output(vectorInt &v)后运行结果才和答案一致。由此可见,在声明中的形参如果不是引用,那么在执行时会在调用类函数完成初始化,相当于调用一次复制构造函

数,在output函数执行完后,它的实参的生命周期也就结束,所以调用了析构函数。完成的是深复制。

实验任务6:

matrix.hpp:

 

#pragma once

#include <iostream>
#include<cassert>

using std::cout;
using std::endl;

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[n*n];
}

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

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

Matrix::Matrix(const Matrix& x) :lines{ x.lines }, cols{ x.cols }
{
    p = new double[x.lines * x.cols];
    for (auto j = 0;j <lines*cols;j++)
        p[j] = x.p[j];
}

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

void Matrix::set(int i, int j, int value)
{
    assert((i >= 0 && i < lines) && (j >= 0 && j < cols));
    p[i * cols + j ] = value;
}

double& Matrix::at(int i, int j)
{
    assert((i >= 0 && i < lines) && (j >= 0 && j < cols));
    return p[i*cols+ j ];
}

double Matrix::at(int i, int j) const
{
    assert((i >= 0 && i < lines) && (j >= 0 && j < cols));
    return p[i*cols+ j ];
}

int Matrix::get_lines()const
{
    return lines;
}

int Matrix::get_cols() const
{
    return cols;
}

void Matrix::print() const
{
    for (auto i = 0;i < lines;i++)
    {
        for (auto j = 0;j < cols;j++)
        {
            if (j < cols - 1)
                cout << p[i * cols + j] << ", ";
            else
                cout << p[i * cols + j] ;
        }
        cout << endl;
    }
}

task6.cpp:

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

void test() {
    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();
}

int main() {
    test();
}

运行结果:

在函数定义中,遇到的问题是给二维数组分配空间,二维数组相当于是一维数组的数组,当作一维数组处理。空间数为lines*cols,第n行第m列(n,m)的数在p中分配的空间数顺序为

n*cols+m;即p[n*cols+m].

 更换数据后的task6.cpp:

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

void test() {
    using namespace std;

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

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

    Matrix m2(4, 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(1, 2, 9);
    m3.print();
}

int main() {
    test();
}

运行结果为:

 

posted @ 2022-11-03 15:40  orangelight  阅读(36)  评论(0)    收藏  举报