实验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 [ ]释放
  • 委托构造函数的使用,减少代码冗余
  • 用一维数组来处理二维空间
posted @ 2021-11-08 21:07  dd摇摆  阅读(32)  评论(1编辑  收藏  举报