1. 程式人生 > 其它 >實驗3 類與物件Ⅱ

實驗3 類與物件Ⅱ

實驗任務3

粗糙小球運動

  • 把經常要複用的程式碼抽象出來寫成函式模組,減少程式碼冗餘
  • system("color+空格+背景色+前景色")。system()的引數必須是const char*型別
  • string類的成員函式c_str()。把string類的字串轉換為char*風格的字串

實驗任務4

實現動態整型陣列類Vector_int

<vector_int.hpp>

 1 #ifndef VECTOR_INT_HPP
 2 #define VECTOR_INT_HPP
 3 
 4 #include <iostream>
 5 #include <cassert>
 6
using namespace std; 7 class Vector_int 8 { 9 public: 10 Vector_int(int n); 11 Vector_int(int n, int init); 12 Vector_int(const Vector_int &vi); 13 ~Vector_int(); 14 int &at(int index); //返回下標為index的元素引用 15 16 private: 17 int size; //陣列大小 18 int *p; //該指標指向分配的記憶體空間 19
}; 20 21 Vector_int::Vector_int(int n) : size(n) 22 { 23 p = new int[n]; 24 cout << "dynamic int array creating..." << endl; 25 } 26 27 Vector_int::Vector_int(int n, int init) : size(n) 28 { 29 cout << "dynamic int array with initial value creating..." << endl;
30 p = new int[n]; 31 for (auto i = 0; i < n; i++) 32 p[i] = init; 33 } 34 35 Vector_int::Vector_int(const Vector_int &vi) : size(vi.size) 36 { 37 cout << "copy constructor is called" << endl; 38 p = new int[size]; 39 for (auto i = 0; i < size; i++) 40 p[i] = vi.p[i]; 41 } 42 43 Vector_int::~Vector_int() 44 { 45 cout << "destructor is called " << endl; 46 delete[] p; //使用new []申請的記憶體應該用delete []釋放 47 } 48 49 int &Vector_int::at(int index) 50 { 51 assert(index >= 0 && index < size); 52 return p[index]; 53 } 54 #endif

<task4.cpp>

 1 #include<iostream>
 2 #include"Vector_int.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int n;
 8     cin >> n;
 9     Vector_int x(n);
10     cout << x.at(0) << endl;
11 
12     Vector_int y(n,6);
13     cout << y.at(0) << endl;
14     
15     Vector_int z(y);
16     cout << z.at(0) << endl;
17 
18     y.at(0) = 4;
19     cout << y.at(0) << endl;
20     cout << z.at(0) <<endl;
21 }

測試結果:

可以看到,建構函式,複製建構函式,at()方法,解構函式正常執行,且複製構造實現了深複製。

實驗任務5

實現動態矩陣類Matrix

<Matrix.hpp>

 1 #ifndef MATRIX_H
 2 #define MATRIX_H
 3 
 4 #include <iostream>
 5 #include <cassert>
 6 
 7 using std::cout;
 8 using std::endl;
 9 
10 class Matrix
11 {
12 public:
13     Matrix(int n);                     // 建構函式,構造一個n*n的矩陣
14     Matrix(int n, int m);              // 建構函式,構造一個n*m的矩陣
15     Matrix(const Matrix &X);           // 複製建構函式,使用已有的矩陣X構造
16     ~Matrix();                         //解構函式
17     void set(const double *pvalue);    // 用pvalue指向的連續記憶體塊資料為矩陣賦值
18     void set(int i, int j, int value); //設定矩陣第i行第j列元素值為value, i,j從0開始
19     double &at(int i, int j);          //返回矩陣第i行第j列元素的引用
20     double at(int i, int j) const;     // 返回矩陣第i行第j列元素的值
21     int get_lines() const;             //返回矩陣行數
22     int get_cols() const;              //返回矩列數
23     void print() const;                // 按行列印輸出矩陣
24 private:
25     int lines; // 矩陣行數
26     int cols;  // 矩陣列數
27     double *p; // 指向存放矩陣資料的記憶體塊的首地址
28 };
29 
30 //委託構造
31 Matrix::Matrix(int n) : Matrix(n, n) {}
32 
33 Matrix::Matrix(int n, int m) : lines(n), cols(m)
34 {
35     p = new double[lines * cols];
36 }
37 
38 Matrix::Matrix(const Matrix &X) : lines(X.lines), cols(X.cols)
39 {
40     p = new double[lines * cols];
41     for (int i = 0; i < lines * cols; i++)
42     {
43         p[i] = X.p[i];
44     }
45 }
46 
47 Matrix::~Matrix()
48 {
49     delete[] p;
50 }
51 
52 void Matrix::set(const double *pvalue)
53 {
54     for (int i = 0; i < lines * cols; i++)
55     {
56         p[i] = pvalue[i];
57     }
58 }
59 
60 void Matrix::set(int i, int j, int value)
61 {
62     p[i * cols + j] = value;
63 }
64 
65 double &Matrix::at(int i, int j)
66 {
67     assert(i >= 0 && i < lines && j >= 0 && j < cols);
68     return p[i * cols + j];
69 }
70 
71 double Matrix::at(int i, int j) const
72 {
73     assert(i >= 0 && i < lines && j >= 0 && j < cols);
74     return p[i * cols + j];
75 }
76 
77 int Matrix::get_lines() const
78 {
79     return lines;
80 }
81 
82 int Matrix::get_cols() const
83 {
84     return cols;
85 }
86 
87 void Matrix::print() const
88 {
89     for (int i = 0; i < lines; i++)
90     {
91         for (int j = 0; j < cols; j++)
92         {
93             cout << p[i * cols + j] << ", ";
94         }
95         cout << "\b\b \n";
96     }
97 }
98 #endif

<task5.cpp>

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     double x[] = {6,5,4,3,2,1};
 9 
10     Matrix m1(3, 2);    // 建立一個3×2的矩陣
11     m1.set(x);          // 用一維陣列x的值按行為矩陣m1賦值
12     m1.print();         // 列印矩陣m1的值
13     cout << "the first line is: " << endl;
14     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;
15     cout << endl;
16 
17     Matrix m2(2, 3);
18     m2.set(x);
19     m2.print();
20     cout << "the first line is: " << endl;
21     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
22     cout << endl;
23 
24     Matrix m3(m2);
25     m3.set(0, 0, 999);
26     m3.print();
27 }

測試結果:

分析與總結:

  • 使用new [ ] 申請的記憶體應用 delete [ ]釋放
  • 委託建構函式的使用,減少程式碼冗餘
  • 用一維陣列來處理二維空間