实验三: 类和对象Ⅱ

<vector_int.hpp>

#ifndef VEXTOR_INT_HPP
#define VEXTOR_INT_HPP
#include<iostream>
using namespace std;
class Vector_int{
public:
    Vector_int(int n) :size(n)
    {
        p = new int[size];
        cout << "创建成功,v"<<++t<<"值为空"<<endl;
    }
    Vector_int(int n, int value) :size(n)
    {
        p = new int[size];
        cout << "创建成功,v" << ++t << "值为:";
        for (int i = 0; i < size; i++)
        {
            p[i] = value;
            cout << p[i] << ",";
        }
        cout << "\b \n";
    }
    Vector_int(const Vector_int &x):size(x.size)
    {
        p = new int[size];
        cout << "创建成功,v" << ++t << "值为:";
        for (int i = 0; i < size; i++)
        {
            p[i] = x.p[i];
            cout << p[i] << ",";
        }
        cout << "\b \n";
    }
    ~Vector_int()
    {
        cout << "v" << t-- << "清空的值为:";
            for (int i = 0; i < size; i++)
                cout << p[i] << ",";
            cout << "\b \n";
            delete[] p;
    }
    int& at(int n)
    {
        return p[n];
    }
private:
    int* p;
    int size;
    static int t;
};
int Vector_int::t = 0;
#endif

 

<task4.cpp>

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

运行结果:

实验三: 类和对象Ⅱ

 

 

 <Matrix.hpp>

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP
#include<iostream>
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

<task 5.cpp>

#include<iostream>
#include"textcoder.hpp"
using namespace std;
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, 4);
    m2.set(x);
    m2.print();
    cout << "the first line is:" << endl;
    cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << " " << m2.at(0, 3) << endl;
    cout << endl;
    Matrix m3(m2);
    m3.set(0, 0, 999);
    m3.print();
}

运行结果:

实验三: 类和对象Ⅱ

 

上一篇:时间戳批量修改为时间


下一篇:05 RDD练习:词频统计