ICode9

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

实验3 类和对象Ⅱ

2021-11-10 20:33:23  阅读:127  来源: 互联网

标签:Matrix 对象 lines cols int 实验 && include


vector_int.hpp

#ifndef vector_int_hpp
#define vector_int_hpp
#include <cstdio>
#include <iostream>
using namespace std;
class Vector_int
{
private:
    int size;
    int *p;
public:
    
    int& at(int x){
        return p[x];
    }
    Vector_int(int size, int y = 0.0) : size(size)
    {
        cout << "Build a new class size = " << size << " , value = " << y << "\n";
        p = new int[size];
        for (int i = 0; i < size; ++i)
        {
            p[i] = y;
        }
    }
    Vector_int(const Vector_int &X)
    {
          cout << "Build a new class size = " << X.size << " , value = " << X.p[0] << "\n";
        p = new int[X.size];
        size = X.size;
        for (int i = 0; i < X.size; ++i)
        {
            p[i] = X.p[i];
        }
    }
    ~Vector_int()
    {
        cout << "Delete the class size = " << size << " , value = " << p[1] << "\n";
        delete[] p;
    }
};
#endif

task4.cpp

#include<iostream>
#include<cstdio>
#include"vector_int.hpp"
using namespace std;
int main()
{
    Vector_int x(10);
    puts("");
    Vector_int y(10,6);
    puts("");
    Vector_int z(y);
    puts("");
    y.at(0)=999;
    return 0;
}

image

Matrix.hpp

#ifndef Matrix_H
#define Matrix_H
#include <iostream>
using namespace std;
class Matrix
{
private:
    int lines, cols;
    double *p;

public:
    Matrix(int n)
    {
        lines = n, cols = n, p = new double[n * n];
    }
    Matrix(int n, int m)
    {
        lines = n, cols = m, p = new double[n * m];
    }
    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()
    {
        delete[] p;
    }
    void set(const double *X)
    {
        for (int i = 0; i < lines * cols; i++)
        {
            p[i] = X[i];
        }
    };
    void set(int i, int j, int value)
    {
        if (i >= 0 && i < lines && j >= 0 && j < cols)
            p[i * cols + j] = value;
    }
    double &at(int i, int j)
    {
        if (i >= 0 && i < lines && j >= 0 && j < cols)
            return p[i * cols + j];
    };
    double at(int i, int j) const
    {
        if (i >= 0 && i < lines && j >= 0 && j < cols)
            return p[i * cols + j];
    };
    int get_lines() const
    {
        return lines;
    };
    int get_cols() const
    {
        return cols;
    };
    void print() const
    {
        for (int i = 0; i < lines; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                cout << p[i * cols + j];
                if (j != cols - 1)
                {
                    cout << ", ";
                }
            }
            cout << "\n";
        }
    };
};
#endif

task5.cpp

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

int main()
{
    using namespace std;

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

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

image

标签:Matrix,对象,lines,cols,int,实验,&&,include
来源: https://www.cnblogs.com/nuist-wzy/p/15535312.html

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

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

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

ICode9版权所有