实验三_OOP_张文瑞_202213260018

发布时间 2023-11-07 22:11:44作者: 张文瑞

任务1

源代码:

 1 1 #pragma once
 2  2 
 3  3 #include <iostream>
 4  4 using std::cout;
 5  5 using std::endl;
 6  6 
 7  7 class Point {
 8  8 public:
 9  9     Point(int x0 = 0, int y0 = 0);
10 10     ~Point() = default;
11 11 
12 12     int get_x() const;
13 13     int get_y() const;
14 14     void show() const;
15 15     void move(int new_x, int new_y);
16 16 
17 17 private:
18 18     int x, y;
19 19 };
20 20 
21 21 Point::Point(int x0, int y0) : x{ x0 }, y{ y0 } {
22 22 }
23 23 
24 24 int Point::get_x() const {
25 25     return x;
26 26 }
27 27 
28 28 int Point::get_y() const {
29 29     return y;
30 30 }
31 31 
32 32 void Point::show() const {
33 33     cout << "(" << x << "," << y << ")" << endl;
34 34 }
35 35 
36 36 void Point::move(int new_x, int new_y) {
37 37     x = new_x;
38 38     y = new_y;
39 39 }
40 point.hpp
View Code
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include<iostream>
 3 #include<vector>
 4 #include"t.hpp"
 5 using std::vector;
 6 using std::cin;
 7 // 输出vector<Point>对象内所有点的坐标
 8 void output(const vector<Point>& v)
 9 {
10     for (auto& t : v)
11         t.show();
12 }
13 void test() {
14     int n;
15     cin >> n;
16     vector<Point> x(n);
17     cout << "x对象中所有点坐标信息: " << endl;
18     output(x);
19     vector<Point> y(x);// 基于vector<Point>对象x构建对象y
20 
21     cout << "\nx对象中所有点坐标信息: " << endl;
22     output(y);
23 
24     cout << "\n更新x对象: " << endl;
25     x.at(0).move(30, 50);// 更新对象x内索引为0的点对象坐标
26 
27     x.push_back(Point(2, 2)); // 向x对象末尾添加一个点对象
28 
29     cout << "\nx对象中所有点坐标信息: " << endl;
30     output(x);
31     cout << "\ny对象中所有点坐标信息: " << endl;
32     output(y);
33 }
34 
35 int main() {
36     test();
37 }
View Code

运行截图:

 

任务2

源代码:

 1 cout << "\n更新x对象中点坐标信息......" << endl;
 2     x.at(0).move(30, 50);
 3     x.at(1).move(-1, -1);
 4     cout << "x对象中所有点坐标信息: " << endl;
 5     output(x); 
 6     cout << "\ny对象中所有点坐标信息: " << endl;
 7     output(y); 
 8 }
 9 
10 int main() {
11     test();
12 }
View Code
 1 #pragma once
 2 #include "point.hpp"
 3 #include <cassert>
 4 #include <iostream>
 5 
 6 class vectorPoint {
 7 
 8 public:
 9     vectorPoint(int n);
10     ~vectorPoint();
11 
12     int get_size() const;// 获得当前动态数组内元素个数
13 
14     Point& at(int index);// 返回下标为index的元素引用
15 
16     Point& at(int index) const; // 返回下标为index的元素const引用
17 private:
18     int size; // 动态数组的大小
19 
20     Point* ptr;
21 };
22 
23 vectorPoint::vectorPoint(int n) : size{ n } {
24     ptr = new Point[n];
25 }
26 
27 vectorPoint::~vectorPoint() {
28     delete[] ptr;
29 }
30 
31 int vectorPoint::get_size() const {
32     return size;
33 }
34 
35 Point& vectorPoint::at(int index) {
36     assert(index >= 0 && index < size); // 宏,在测试模式下工作。如果不满足条件,则程序终止
37 
38         return ptr[index];
39 }
40 
41 Point& vectorPoint::at(int index) const {
42     assert(index >= 0 && index < size);
43     return ptr[index];
44 }
View Code
 1 #include "vectorPoint.hpp"
 2 #include <iostream>
 3 
 4 void output(const vectorPoint &v) {
 5     for(auto i = 0; i < v.get_size(); ++i)
 6         v.at(i).show();
 7 }
 8 
 9 void test() {
10     using namespace std;
11 
12     int n;
13     cout << "输入vectorPoint对象中元素个数: ";
14     cin >> n;
15 
16     vectorPoint x(n);
17     cout << "x对象中所有点坐标信息: " << endl;
18     output(x); 
19 
20     vectorPoint y(x);
21     cout << "\ny对象中所有点坐标信息: " << endl;
22     output(y); 
23 
24     cout << "\n更新x对象中点坐标信息......" << endl;
25     x.at(0).move(30, 50);
26     x.at(1).move(-1, -1);
27 
28     cout << "x对象中所有点坐标信息: " << endl;
29     output(x); 
30 
31     cout << "\ny对象中所有点坐标信息: " << endl;
32     output(y); 
33 }
34 
35 int main() {
36     test();
37 }
View Code

 

运行截图:

 

任务3

源代码:

 1 #pragma once
 2 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 
 7 class Point {
 8 public:
 9     Point(int x0 = 0, int y0 = 0);
10     ~Point() = default;
11 
12     int get_x() const;
13     int get_y() const;
14     void show() const;
15     void move(int new_x, int new_y);
16 
17 private:
18     int x, y;
19 };
20 
21 Point::Point(int x0, int y0) : x{ x0 }, y{ y0 } {
22 }
23 
24 int Point::get_x() const {
25     return x;
26 }
27 
28 int Point::get_y() const {
29     return y;
30 }
31 
32 void Point::show() const {
33     cout << "(" << x << ", " << y << ")" << endl;
34 }
35 
36 void Point::move(int new_x, int new_y) {
37     x = new_x;
38     y = new_y;
39 }
View Code
 1 #pragma once
 2 
 3 #include "point.hpp"
 4 #include <cassert>
 5 #include <iostream>
 6 
 7 class vectorPoint {
 8 public:
 9     vectorPoint(int n);
10     vectorPoint(const vectorPoint& vp);
11     ~vectorPoint();
12 
13     int get_size() const;           // 获得当前动态数组内元素个数
14     Point& at(int index);           // 返回下标为index的元素引用
15     Point& at(int index) const;     // 返回下标为index的元素const引用
16 
17 private:
18     int size; // 动态数组的大小
19     Point* ptr;
20 };
21 
22 vectorPoint::vectorPoint(int n) : size{ n } {
23     ptr = new Point[n];
24 }
25 
26 vectorPoint::vectorPoint(const vectorPoint& vp) : size{ vp.size }, ptr{ new Point[size] } {
27     for (auto i = 0; i < size; ++i)
28         ptr[i] = vp.ptr[i];
29 }
30 
31 vectorPoint::~vectorPoint() {
32     delete[] ptr;
33 }
34 
35 int vectorPoint::get_size() const {
36     return size;
37 }
38 
39 Point& vectorPoint::at(int index) {
40     assert(index >= 0 && index < size);  // 宏,在测试模式下工作。如果不满足条件,则程序终止
41     return ptr[index];
42 }
43 
44 Point& vectorPoint::at(int index) const {
45     assert(index >= 0 && index < size);
46     return ptr[index];
47 }
View Code
 1 #include "vectorPoint.hpp"
 2 #include <iostream>
 3 
 4 // 输出vectorPoint对象内的所有数据
 5 void output(const vectorPoint& v) {
 6     for (auto i = 0; i < v.get_size(); ++i)
 7         v.at(i).show();
 8 }
 9 
10 // 测试vectorPoint类:构造对象、复制构造对象
11 void test() {
12     using namespace std;
13 
14     int n;
15     cout << "输入vectorPoint对象中元素个数: ";
16     cin >> n;
17 
18     vectorPoint x(n);
19     cout << "x对象中所有点坐标信息: " << endl;
20     output(x);
21 
22     vectorPoint y(x);
23     cout << "\ny对象中所有点坐标信息: " << endl;
24     output(y);
25 
26     cout << "\n更新x对象中点坐标信息......" << endl;
27     x.at(0).move(30, 50);
28     x.at(1).move(-1, -1);
29 
30     cout << "x对象中所有点坐标信息: " << endl;
31     output(x);
32 
33     cout << "\ny对象中所有点坐标信息: " << endl;
34     output(y);
35 }
36 
37 int main() {
38     test();
39 }
View Code

运行截图:

 

任务4

源代码:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 // 函数声明
 6 
 7 void swap1(int& rx, int& ry); // 引用作为形参
 8 
 9 void swap2(int* px, int* py); // 指针作为形参
10 
11 void print(int x, int y); // 普通变量作为形参
12 
13 // 测试代码
14 
15 void test() {
16     int x = 3, y = 4;
17     print(x, y);
18     swap1(x, y); // 函数调用,注意:引用作为形参时,实参形式
19 
20     print(x, y);
21     cout << endl;
22     x = 3, y = 4;
23     print(x, y);
24     swap2(&x, &y); // 函数调用,注意:指针作为形参时,实参形式
25 
26     print(x, y);
27 }
28 
29 int main() {
30     test();
31 }
32 
33 // 函数定义:交换两个变量(引用变量作为形参)
34 
35 void swap1(int& rx, int& ry) {
36     int t;
37     t = rx; rx = ry; ry = t;
38 }
39 
40 // 函数定义:交换两个变量(指针变量作为形参)
41 
42 void swap2(int* px, int* py) {
43     int t;
44     t = *px;  *px = *py;  *py = t;
45 }
46 
47 // 函数定义:输出两个变量(普通变量作为形参)
48 
49 void print(int x, int y) {
50     std::cout << "x = " << x << ", y = " << y << "\n";
51 }
View Code
 1 #include <iostream>
 2 #include <typeinfo>
 3 
 4 using namespace std;
 5 
 6 int main() {
 7     int a;
 8     
 9     int &ra = a;
10     ra = 4;
11     int *pa = &a;
12     *pa = 5;
13     // 以十六进制形式输出普通变量a, 引用变量ra,指针变量pa的地址
14 
15     cout << "&a = " << hex << &a << endl;
16     cout << "&ra = " << hex << &ra << endl;
17     cout << "&pa = " << hex << &pa << "\n\n";
18     
19     // 输出普通变量a, 引用变量ra,指针变量pa的值
20 
21     cout << "a = " << a << endl;
22     cout << "ra = " << a << endl;
23     cout << "pa = " << hex << pa << endl;
24     
25     // 输出指针变量pa指向的变量的值
26 
27     cout << "*pa = " << *pa << "\n\n";
28     // 输出普通变量a,引用变量ra, 指针变量pa的类型信息
29 
30     cout << "type a: " << typeid(a).name() << endl;
31     cout << "type ra: " << typeid(ra).name() << endl;
32     cout << "type pa: " << typeid(pa).name() << endl;
33 }
View Code

运行截图:

 

 

 

实验5

运行测试:

源代码:

 1 #include<iostream>
 2 #include<cassert>
 3 using namespace std;
 4 //类的定义
 5 class vectorInt {
 6 public:
 7     vectorInt(int n);
 8     vectorInt(int n, int value);
 9     vectorInt(const vectorInt& vi);
10     ~vectorInt();
11 
12     int get_size() const;
13     int& at(int index);
14     int& at(int index)const;
15 
16 private:
17     int size;
18     int* p;
19 };
20 
21 //类的实现
22 
23 vectorInt::vectorInt(int n) : size{ n }, p{ new int[n] } {
24     cout << "constructor vectorInt was called." << endl;
25 }
26 
27 vectorInt::vectorInt(int n, int value) :size{ n }, p{ new int[n] } {
28     for (int i = 0; i < n; ++i)
29         p[i] = value;
30     cout << "constructor vectorInt was called." << endl;
31 }
32 
33 vectorInt::vectorInt(const vectorInt& vi) : size{ vi.size }, p{ new int[size] } {
34     for (int i = 0; i < size; ++i)
35         p[i] = vi.p[i];
36     cout << "copy constructor called." << endl;
37 }
38 
39 vectorInt::~vectorInt() {
40     delete[] p;
41     cout << "destructor called." << endl;
42 }
43 
44 int vectorInt::get_size() const {
45     return size;
46 }
47 
48 int& vectorInt::at(int index) {
49     assert(index >= 0 && index < size);
50     return p[index];
51 }
52 
53 int& vectorInt::at(int index) const {
54     assert(index >= 0 && index < size);
55     return p[index];
56 }
de
#include "vectorInt.hpp"
#include <iostream>

// 函数output()的定义:遍历输出vectorInt对象内的所有元素

void output(const vectorInt& vi) {
    // 待补足。。。
    for (auto i = 0; i < vi.get_size(); ++i)
        cout << vi.at(i) << ", ";
    cout << "\b\b \n";

}

void test() {
    int n;
    cout << "输入vectorInt对象中元素个数: ";
    cin >> n;
    vectorInt x1(n);// 构造动态int数组对象x1,包含n个元素,不对元素初始化

    for (auto i = 0; i < n; ++i)
        x1.at(i) = i * i;
    cout << "vectorInt对象x1: ";
    output(x1);
    vectorInt x2(n, 42); // 构造动态int数组对象x1,包含n个元素,每个元素初始值为42
    cout << "vectorInt对象x2: ";
    output(x2);
    vectorInt x3(x2);// 使用x2构造x3

    cout << "vectorInt对象x3: ";
    output(x3);
    cout << "更新vectorInt对象x2......\n";
    x2.at(0) = 77;
    x2.at(1) = -999;
    cout << "vectorInt对象x2: ";
    output(x2);
    cout << "vectorInt对象x3: ";
    output(x3);
}

int main() {
    test();
}
View Code

实验六

源代码:

 1 #pragma once
 2 #include <iostream>
 3 #include <cassert>
 4 
 5 using std::cout;
 6 
 7 using std::endl;
 8 
 9 // 类Matrix的声明
10 
11 class Matrix {
12 
13 public:
14     Matrix(int n, int m);                                // 构造函数,构造一个n*m的矩阵
15 
16     Matrix(int n);                                        // 构造函数,构造一个n*n的矩阵
17 
18     Matrix(const Matrix& x);                            // 复制构造函数, 使用已有的矩阵X构造
19 
20     ~Matrix();
21     void set(const double* pvalue);                        // 用pvalue指向的连续内存块数据按行为矩阵赋值
22     void set(int i, int j, double value);                // 设置矩阵对象索引(i,j)的元素值为value
23     double& at(int i, int j) const;                        // 返回矩阵对象索引(i,j)的元素引用
24     double& at(int i, int j);                            // 返回矩阵对象索引(i,j)的元素引用
25     int get_lines() const;                                // 返回矩阵对象行数
26     int get_cols() const;                                // 返回矩阵对象列数
27     void print() const;                                    // 按行打印输出矩阵对象元素值
28 
29 private:
30     int lines;// 矩阵对象内元素行数
31 
32     int cols;// 矩阵对象内元素列数
33 
34     double* ptr;
35 };
36 
37 // 类Matrix的实现:待补足
38 Matrix :: Matrix(int n,int m){ 
39     ptr = new double[n * m];
40     lines = n;
41     cols = m;
42 }
43 Matrix :: Matrix(int n){
44     ptr = new double[n * n];
45     lines = cols = n;
46 }
47 Matrix::Matrix(const Matrix& x) : lines(x.lines), cols(x.cols), ptr(new double[lines * cols]) {
48     for (int i = 0; i < lines; ++i) {
49         for (int j = 0; j < cols; ++j) {
50             ptr[i * cols + j] = x.ptr[i * cols + j];
51         }
52     }
53 }
54 Matrix::~Matrix() {
55     delete[]ptr;
56 }
57 
58 void Matrix::set(const double* pvalue) {
59     for (auto i = 0; i < lines * cols; ++i)
60         ptr[i] = pvalue[i];
61 }
62 
63 void Matrix::set(int i, int j, double value) {
64     ptr[i * cols + j] = value;
65 }
66 
67 double& Matrix::at(int i, int j)const {
68     return ptr[i * cols + j];
69 }
70 
71 double& Matrix::at(int i, int j) {
72     return ptr[i * cols + j];
73 }
74 
75 int Matrix::get_cols()const {
76     return cols;
77 }
78 
79 int Matrix::get_lines()const {
80     return lines; 
81 }
82 
83 void Matrix::print()const {
84     for (auto i = 0; i < lines; ++i)
85     {
86         for (auto j = 0; j < cols; ++j)
87         {
88             cout << ptr[i * cols + j] << ",";
89         }
90         cout << "\b \b\n";
91     }
92 }
View Code
 1 #include <iostream>
 2 #include "matrix.hpp"
 3 
 4 using namespace std;
 5 
 6 const int N1 = 3;
 7 
 8 const int N2 = 2;
 9 
10 // 输出一个矩阵对象中索引为index对应的行的所有元素值
11 
12 void output(const Matrix& m, int index) {
13 for (auto j = 0; j < m.get_cols(); ++j)
14 cout << m.at(index, j) << ", ";
15 cout << "\b\b \n";
16 }
17 
18 void test() {
19 double x[N1 * N2] = { 1, 2, 3, 4, 5, 6 };
20 Matrix m1(N1, N2); // 创建一个N1×N2矩阵
21 
22 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值
23 
24 cout << "矩阵对象m1: " << endl;
25 m1.print(); // 打印矩阵m1的值
26 
27 cout << "矩阵对象m1第0行是: " << endl;
28 output(m1, 0); cout << endl;
29 Matrix m2(N2, N1);
30 m2.set(x);
31 cout << "矩阵对象m2: " << endl;
32 m2.print();
33 cout << "矩阵对象m2第0行是: " << endl;
34 output(m2, 0);
35 cout << endl;
36 Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
37 
38 m3.set(0, 0, 999); // 讲矩阵对象m2索引(0,0)元素设为999
39 
40 cout << "矩阵对象m3:" << endl;
41 m3.print();
42 cout << endl;
43 Matrix m4(2); // 创建一个2*2矩阵对象
44 
45 m4.set(x); // 用一维数组x的值按行为矩阵m4赋值
46 
47 cout << "矩阵对象m4:" << endl;
48 m4.print();
49 }
50 
51 int main() {
52 test();
53 }
View Code

 

实验截图: