1. 程式人生 > 其它 >實驗三 類和物件

實驗三 類和物件

vector_int.hpp

#include <iostream>
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 n);
private:
    int *p; //陣列的首地址
    int n;  //陣列元素的個數
};
Vector_int::Vector_int(int n):n(n)
{
    p = new
int[n]();//分配n個空間,用0初始化 } Vector_int::Vector_int(int n, int m):n(n) { p = new int[n](); for (int i = 0; i < n; ++i) { p[i] = m; } } Vector_int::Vector_int(const Vector_int &x) { n = x.n; p = new int[n](); for (int i = 0; i < n; i++) { p[i] = x.p[i]; } } Vector_int::
~Vector_int() { delete[] p; } int Vector_int::at(int n) { return p[n]; }

task4.cpp:

#include <iostream>
#include "Vector_int.hpp"
int main()
{
    Vector_int x(5);
    Vector_int y(5, 1);
    Vector_int z(y);
    std::cout << y.at(1) << std::endl;
    for (int i = 0; i < 5
; i++) { std::cout << x.at(i) << " "; } std::cout<<std::endl; for (int i = 0; i < 5; i++) { std::cout << y.at(i) << " "; } std::cout<<std::endl; for (int i = 0; i < 5; i++) { std::cout << z.at(i) << " "; } std::cout<<std::endl; }

執行測試截圖:

martrix_cpp:

#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>
#include <cassert>

class Matrix
{
public:
    Matrix(int n);                     // 建構函式,構造一個n*n的矩陣
    Matrix(int n, int m);              // 建構函式,構造一個n*m的矩陣
    Matrix(const Matrix &X);           // 複製建構函式,使用已有的矩陣X構造
    ~Matrix();                         //解構函式
    void set(const double *pvalue);    // 用pvalue指向的連續記憶體塊資料為矩陣賦值
    void set(int i, int j, int value); //設定矩陣第i行第j列元素值為value
    double &at(int i, int j);          //返回矩陣第i行第j列元素的引用
    double at(int i, int j) const;     // 返回矩陣第i行第j列元素的值
    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 temp = 0;
    for (int i = 0; i < lines; ++i)
        for (int j = 0; j < cols; ++j)
        {
            p[temp++] = X.at(i, j);
        }
}
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)
{
    p[i * cols + j] = value;
}
double &Matrix::at(int i, int j)
{
    double &a = p[i * cols + j];
    return a;
}
double Matrix::at(int i, int j) const
{
    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)
        {
            std::cout << p[i * cols + j] << " ";
        }
        std::cout << std::endl;
    }
}
#endif

tassk5.cpp

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

int main()
{
    using namespace std;

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

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

執行測試截圖: