ICode9

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

实验3 类与对象II

2021-11-07 19:33:44  阅读:103  来源: 互联网

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


#ifndef VECTOR_INT
#define VECTOR_INT
#include<iostream>
#include<cassert>
using namespace std;
class Vector_int {
public:
    Vector_int(int n);
    Vector_int(int n, int m);
    Vector_int(const Vector_int& x);
    ~Vector_int();
    int& at(int i);
    void show() const;
private:
    int size;
    int* p;
};
Vector_int::Vector_int(int n) : size(n) {
    cout << "Default constructor called." << endl;
    p = new int[n];
    for(int i=0;i<n;i++)
    p[i]=0;
}

Vector_int::Vector_int(int n,int m) : size(m) {
    cout << "constructor called." << endl;
    p = new int[n];
    for (int i = 0; i < m; i++) {
    p[i]=m;                      
    }
}

Vector_int::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::~Vector_int()
{
    cout<<"deleting"<<endl;
    delete[] p;
}
int &Vector_int::at(int i) {
    assert( i>= 0 && i < size);
    return p[i];
}
void Vector_int::show() const {
    for (int i = 0; i < size; i++) {
        cout << p[i] <<endl;
    }
}

#endif
#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.show();
    y.at(0) = 999;
    y.show();
    return 0;
}

 

 

 

#include<iostream>
#pragma once
#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP
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[lines * cols];
}
Matrix::Matrix(int n, int m) : lines(n), cols(m)
{
    p = new double[lines * cols];
}
Matrix::Matrix(const Matrix& x) : lines(x.lines), cols(x.cols)
{
    p = new double[lines * cols];
    int length = lines * cols;
    for (int i = 0; i < length; ++i)
        p[i] = x.p[i];
}
Matrix::~Matrix()
{
    delete[] p;
}
void Matrix::set(const double* pvalue)
{
    int length = lines * cols;
    for (int i = 0; i < length; ++i)
        p[i] = pvalue[i];
}
void Matrix::set(int i, int j, int value)
{
    p[(i + 1) * (j + 1) - 1] = value;
}
double& Matrix::at(int i, int j)
{
    return p[(i + 1) * (j + 1) - 1];
}
double Matrix::at(int i, int j)const
{
    return p[(i + 1) * (j + 1) - 1];
}
int Matrix::get_lines()const
{
    return lines;
}
int Matrix::get_cols()const
{
    return cols;
}
void Matrix::print()const
{
    int t = -1;
    for (int i = 1; i <= lines; i++)
    {
        for (int j = 1; j <= cols; j++)
            cout << p[++t] << " ";
        cout << "\n";
    }
}
#endif

int main()
{
    double x[] = { 1,2,3,4,5,6,7,8 };
    Matrix m1(4, 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();
}

 

标签:const,Matrix,对象,lines,cols,II,int,Vector,实验
来源: https://www.cnblogs.com/yangliu-1219/p/15521318.html

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

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

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

ICode9版权所有