ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验三 类与对象2

2021-11-09 23:01:47  阅读:106  来源: 互联网

标签:const Matrix 对象 double lines cols int 实验


vector_int.hpp:

#ifndef VECTOR_INT_H
#define VECTOR_INT_H
#include<iostream>
#include<cassert>

using namespace std;

class vector_int {
public:
    vector_int(int n) :size{ n } {
        cout << "Default constructor called." << endl;
        p = new int[size];
        for (int i = 0; i < size; i++)
            p[i] = 0;
    }
    vector_int(int n, int m) :size{ n }, value{ m }{
        cout << "Constructor called." << endl;
        p = new int[size];
        for (int i = 0; i < size; i++)
            p[i] = value;
    }
    vector_int(const vector_int& x) :size{ x.size } {
        cout << "Copy constructor called." << endl;
        p = new int[size];
        for (int i = 0; i < size; i++)
            p[i] = x.p[i];
    }
    ~vector_int()
    {
        cout << "Destructor called" << endl;
        delete[] p;
    }
    int& at(int i) {
        assert(i >= 0 && i < size);
        return p[i];
    }
    void show() const {
        for (int i = 0; i < size; i++)
            cout << p[i] << " ";
        cout << "\b\n";
    }
private:
    int size, value;
    int* p;
};
#endif // !VECTOR_INT_H

 

 

task4.cpp:

#include<iostream>
#include"vector_int.hpp"
using namespace std;
int main() {
    int n;
    cin >> n;
    vector_int x1(n);
    x1.show();
    vector_int x2(n, 6);
    x2.show();
    vector_int y(x2);
    y.at(0) = 999;
    y.show();
}

 

结果截图:

 

 

 

Matrix.hpp:

#ifndef MATRIX_HPP
#define MATRIX_HPP
#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[n * n];
}

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

Matrix::Matrix(const Matrix& X) : lines{ X.lines }, cols{ X.cols }{
    p = new double[lines * cols];
    for (int i = 0; i < lines * 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)
{
    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 (int i = 0; i < lines; i++) {
        for (int j = 0; j < cols; j++)
            cout << p[i * cols + j] << " ";
        cout << "\b\n";
    }
}
#endif // !MATRIX_HPP

 

 

task5.cpp:

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

int main()
{
    using namespace std;

    double x[] = { 10, 20, 30, 40, 50, 60 };

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

 

 

结果截图:

 

 

标签:const,Matrix,对象,double,lines,cols,int,实验
来源: https://www.cnblogs.com/Hershel-Hjh/p/15531395.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有