实验三 类与对象II

实验任务四
//vector_int.hpp
#ifndef VECTOR_INT #define VECTOR_INT #include<iostream> #include<cassert> using namespace std; class Vector_int { public: Vector_int(int n); Vector_int(int m, int n); Vector_int(const Vector_int& x); ~Vector_int(); int& at(int n); 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](); } Vector_int::Vector_int(int m, int n) : size(m) { cout << "constructor called." << endl; p = new int[m]; for (int i = 0; i < m; i++) { *(p + i) = n; } } Vector_int::Vector_int(const Vector_int& x) : size(x.size) { cout << "copy constructor called." << endl; p = new int[size]; for (auto i = 0; i < size; ++i) p[i] = x.p[i]; } int& Vector_int::at(int n) { assert(n >= 0 && n < size); return p[n]; } void Vector_int::show() const { for (auto i = 0; i < size; ++i) { cout << *(p + i) <<endl; } } Vector_int::~Vector_int() { cout << "destory the constructor" << endl; delete[] p; } #endif
//task 4
#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;
}

实验三 类与对象II

 

实验任务五

//Matrix.hpp
#pragma once
#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"Matrix.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, 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();
}

实验三 类与对象II

 

上一篇:面向对象->实验报告三(C++)


下一篇:CSS3新特性(阴影、动画、渐变、变形、伪元素等) CSS3与页面布局学习总结——CSS3新特性(阴影、动画、渐变、变形、伪元素等)