實驗三 類與物件2
阿新 • • 發佈:2021-11-10
vector_int.hpp:
#ifndef VECTOR_INT_H #define VECTOR_INT_H #include<iostream> #include<cassert> using namespace std; class vector_int { public: vector_int(int n) :size{ n } { cout << "Default constructor called." << endl; p = new int[size]; for (inti = 0; i < size; i++) p[i] = 0; } vector_int(int n, int m) :size{ n }, value{ m }{ cout << "Constructor called." << endl; p = new int[size]; for (int i = 0; i < size; i++) p[i] = value; } 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() { cout << "Destructor called" << endl; delete[] p; } int& at(int i) { assert(i>= 0 && i < size); return p[i]; } void show() const { for (int i = 0; i < size; i++) cout << p[i] << " "; cout << "\b\n"; } private: int size, value; int* p; }; #endif // !VECTOR_INT_H
task4.cpp:
#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.at(0) = 999; y.show(); }
結果截圖:
Matrix.hpp:
#ifndef MATRIX_HPP #define MATRIX_HPP #include <iostream> #include <cassert> 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[n * n]; } Matrix::Matrix(int n,int m ): lines{ n }, cols{ m }{ p = new double[n * m]; } Matrix::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::~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) { assert(i >= 0 && i < lines&& j >= 0 && j < cols); p[i * cols + j] = value; } double& Matrix::at(int i, int j) { assert(i >= 0 && i < lines&& j >= 0 && j < cols); return p[i * cols + j]; } double Matrix::at(int i, int j)const { assert(i >= 0 && i < lines&& j >= 0 && j < cols); 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++) cout << p[i * cols + j] << " "; cout << "\b\n"; } } #endif // !MATRIX_HPP
task5.cpp:
#include <iostream> #include "Matrix.hpp" int main() { using namespace std; double x[] = { 10, 20, 30, 40, 50, 60 }; 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, 666); m3.print(); }
結果截圖: