实验四 类与数组、指针

任务五

代码:

vectorInt:

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

class VectorInt{
    public:
        VectorInt(int s);
        VectorInt(int s,int value);
        VectorInt(const VectorInt &v);
        ~VectorInt()  {delete[]p; cout << "析构函数调用" << endl;}
        
        int& at(int i);
        int get_size() const {return size;}
        friend void output(VectorInt &v);
    private:
        int size,*p;
        
};

VectorInt::VectorInt(int s):size{s}
{
    p=new int[size];
    cout << "默认构造函数调用" << endl;
}

VectorInt::VectorInt(int s,int value):size{s}
{
    p=new int[size];
    for(int i=0;i<size;i++)
        p[i]=value;
    cout << "构造函数调用" << endl;
}

VectorInt::VectorInt(const VectorInt &v):size{v.size}
{
    p=new int[size];
    for(int i=0;i<size;i++)
        p[i]=v.p[i];
    cout << "复制构造函数调用" << endl; 
}

int& VectorInt::at(int i)
{
    if(i>=0 && i<size)
        return p[i];
}

void output(VectorInt &v)
{
    for(int i=0;i<v.size;i++)
        cout << v.p[i] << " ";
    cout << endl;        
}

 

task5:

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

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

 

运行结果截图:

 

 

 

 

任务六

代码:

Matrix.hpp:

#pragma once

#include<iostream>
using namespace std;

class Matrix{
    
    public:
        Matrix(int n);
        Matrix(int n, int m);
        Matrix(const Matrix &X);
        ~Matrix()  {delete []p;}
        
        void set(const double *pvalue);
        void set(int i, int j, int value) { p[i*cols+j]=value; }
        double &at(int i, int j)  { return p[i*cols+j]; }
        double at(int i, int j) const  { return p[i*cols+j]; }
        int get_lines() const  { return lines; }
        int get_cols() const { return cols; }
        void print() const;
        
    private:
        int lines,cols;
        double *p;

};

Matrix::Matrix(int n):lines{n},cols{n}
{
    p=new double[lines*cols];
    cout << "默认构造函数调用" << endl;
}

Matrix::Matrix(int n,int m):lines{n},cols{m}
{
    p=new double[lines*cols];
    cout << "构造函数调用" << endl;
}

Matrix::Matrix(const Matrix &X):lines{X.lines},cols{X.cols}
{
    p=new double[lines*cols];
    for(auto i=0;i<lines*cols;i++)
        p[i]=X.p[i];
    cout << "复制构造函数调用" << endl; 
}

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

void Matrix::print() const
{
    for(auto i=0;i<lines;i++)
    {
        for(auto j=0;j<cols;j++)
            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();
}

 

运行结果截图:

 

posted @ 2022-11-08 23:28  lovessr  阅读(6)  评论(0编辑  收藏  举报