实验三 类与数组,指针

发布时间 2023-11-05 21:12:09作者: 懿里

任务1:

1.代码:
point.hpp:

 1 #pragma once 
 2 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 class Point {
 7 public:
 8 Point(int x0 = 0, int y0 = 0);
 9 ~Point() = default;
10 int get_x() const;
11 int get_y() const;
12 void show() const;
13 void move(int new_x, int new_y);
14 private:
15 int x, y;
16 };
17 Point::Point(int x0, int y0): x{x0}, y{y0} {
18 }
19 int Point::get_x() const {
20 return x;
21 }
22 int Point::get_y() const {
23 return y;
24 }
25 void Point::show() const {
26 cout << "(" << x << ", " << y << ")" << endl;
27 }
28 void Point::move(int new_x, int new_y) {
29 x = new_x;
30 y = new_y;
31 }
View Code

task1.cpp:

 1 #include <iostream>
 2 #include "point.hpp"
 3 #include <vector>
 4 using std::vector;
 5 using std::cin;
 6 // 输出vector<Point>对象内所有点的坐标
 7 void output(const vector<Point> &v) {
 8 for(auto &t: v)
 9 t.show();
10 }
11 void test() {
12 int n;
13 cin >> n;
14 vector<Point> x(n);
15 cout << "x对象中所有点坐标信息: " << endl;
16 output(x);
17 vector<Point> y(x);
18 cout << "\nx对象中所有点坐标信息: " << endl;
19 output(y);
20 cout << "\n更新x对象: " << endl;
21 x.at(0).move(30, 50); 
22 x.push_back(Point(2, 2)); 
23 cout << "\nx对象中所有点坐标信息: " << endl;
24 output(x);
25 cout << "\ny对象中所有点坐标信息: " << endl;
26 output(y);
27 }
28 int main() {
29 test();
30 }
View Code

2.图片:

对x对象进行更新时,基于 vector 对象x创建的对象y不发生变化。

标准库模板类vector在复制一个动态数组对象时,实现的是深复制。

 

任务2:

1.代码:

point.hpp:

 1 #pragma once 
 2 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 class Point {
 7 public:
 8 Point(int x0 = 0, int y0 = 0);
 9 ~Point() = default;
10 int get_x() const;
11 int get_y() const;
12 void show() const;
13 void move(int new_x, int new_y);
14 private:
15 int x, y;
16 };
17 Point::Point(int x0, int y0): x{x0}, y{y0} {
18 }
19 int Point::get_x() const {
20 return x;
21 }
22 int Point::get_y() const {
23 return y;
24 }
25 void Point::show() const {
26 cout << "(" << x << ", " << y << ")" << endl;
27 }
28 void Point::move(int new_x, int new_y) {
29 x = new_x;
30 y = new_y;
31 }
View Code

vectorpoint.hpp:

 1 #pragma once
 2 #include "point.hpp"
 3 #include <cassert>
 4 #include <iostream>
 5 class vectorPoint{
 6 public:
 7 vectorPoint(int n);
 8 ~vectorPoint();
 9 
10 int get_size() const;
11 Point &at(int index);
12 Point& at(int index) const; 
13 
14 private:
15 int size;
16 Point* ptr;
17 };
18 
19 vectorPoint::vectorPoint(int n):size{n}
20 {
21     ptr=new Point[n];
22 }
23 
24 vectorPoint::~vectorPoint() {
25 delete[] ptr;
26 }
27 int vectorPoint::get_size() const {
28 return size;
29 }
30 Point& vectorPoint:: at(int index){
31 assert(index >= 0 && index < size); 
32 
33 return ptr[index];
34 }
35 Point& vectorPoint::at(int index) const {
36 assert(index >= 0 && index < size);
37 return ptr[index];
38 }
View Code

task2.cpp:

 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 int n;
12 cout << "输入vectorPoint对象中元素个数: ";
13 cin >> n;
14 vectorPoint x(n);
15 cout << "x对象中所有点坐标信息: " << endl;
16 output(x);
17 vectorPoint y(x);
18 cout << "\ny对象中所有点坐标信息: " << endl;
19 output(y);
20 cout << "\n更新x对象中点坐标信息......" << endl;
21 x.at(0).move(30, 50);
22 x.at(1).move(-1, -1);
23 cout << "x对象中所有点坐标信息: " << endl;
24 output(x);
25 cout << "\ny对象中所有点坐标信息: " << endl;
26 output(y);
27 }
28 int main() {
29 test();
30 }
View Code

2.图片:

更新对象x后,基于 vectorPoint 对象x创建的对象y发生变化。

编译器为vectorPoint类创建的默认复制构造函数,在复制一个动态数组对象时,实现是浅复制。

在类vectorPoint内部,手动增加的以下复制构造函数声明和定义,实现的是是浅复制。

 

任务3:

1.代码:

point,hpp:

 1 #pragma once
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 class Point {
 6 public:
 7 Point(int x0 = 0, int y0 = 0);
 8 ~Point() = default;
 9 int get_x() const;
10 int get_y() const;
11 void show() const;
12 void move(int new_x, int new_y);
13 private:
14 int x, y;
15 };
16 Point::Point(int x0, int y0): x{x0}, y{y0} {
17 }
18 int Point::get_x() const {
19 return x;
20 }
21 int Point::get_y() const {
22 return y;
23 }
24 void Point::show() const {
25 cout << "(" << x << ", " << y << ")" << endl;
26 }
27 void Point::move(int new_x, int new_y) {
28 x = new_x;
29 y = new_y;
30 }
View Code

vectorpoint.hpp:

 1 #pragma once
 2 #include "point.hpp"
 3 #include <cassert>
 4 #include <iostream>
 5 class vectorPoint{
 6 public:
 7 vectorPoint(int n);
 8 vectorPoint(const vectorPoint &vp);
 9 ~vectorPoint();
10 int get_size() const; // 获得当前动态数组内元素个数
11 Point& at(int index); // 返回下标为index的元素引用
12 Point& at(int index) const; // 返回下标为index的元素const引用
13 private:
14 int size; // 动态数组的大小
15 Point *ptr;
16 };
17 vectorPoint::vectorPoint(int n) : size{n} {
18 ptr = new Point[n];
19 }
20 vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new
21 Point[size]} {
22 for(auto i = 0; i < size; ++i)
23 ptr[i] = vp.ptr[i];
24 }
25 vectorPoint::~vectorPoint() {
26 delete[] ptr;
27 }
28 int vectorPoint::get_size() const {
29 return size;
30 }
31 Point& vectorPoint::at(int index) {
32 assert(index >= 0 && index < size); // 宏,在测试模式下工作。如果不满足条件,则程序终止
33 
34 return ptr[index];
35 }
36 Point& vectorPoint::at(int index) const {
37 assert(index >= 0 && index < size);
38 return ptr[index];
39 }
View Code

task3.cpp:

 1 #include "vectorPoint.hpp"
 2 #include <iostream>
 3 // 输出vectorPoint对象内的所有数据
 4 void output(const vectorPoint &v) {
 5 for(auto i = 0; i < v.get_size(); ++i)
 6 v.at(i).show();
 7 }
 8 // 测试vectorPoint类:构造对象、复制构造对象
 9 void test() {
10 using namespace std;
11 int n;
12 cout << "输入vectorPoint对象中元素个数: ";
13 cin >> n;
14 vectorPoint x(n);
15 cout << "x对象中所有点坐标信息: " << endl;
16 output(x);
17 vectorPoint y(x);
18 cout << "\ny对象中所有点坐标信息: " << endl;
19 output(y);
20 cout << "\n更新x对象中点坐标信息......" << endl;
21 x.at(0).move(30, 50);
22 x.at(1).move(-1, -1);
23 cout << "x对象中所有点坐标信息: " << endl;
24 output(x);
25 cout << "\ny对象中所有点坐标信息: " << endl;
26 output(y);
27 }
28 int main() {
29 test();
30 }
View Code

2.图片:

更新对象x后,基于 vectorPoint 对象x创建的对象y不发生变化。

这个vectorPoint 类的实现中,复制构造函数实现的是深复制。

深复制时重新为一个节点分配空间,并将数值复制过去,两个指向的是不同地址;浅复制两个指针指向的是同一个地址。


任务4:

task1.cpp:

 1 #include <iostream>
 2 using namespace std;
 3 // 函数声明
 4 void swap1(int &rx, int &ry); // 引用作为形参
 5 void swap2(int *px, int *py); // 指针作为形参
 6 void print(int x, int y); // 普通变量作为形参
 7 // 测试代码
 8 void test() {
 9 int x = 3, y = 4;
10 print(x, y);
11 swap1(x, y); // 函数调用,注意:引用作为形参时,实参形式
12 print(x, y);
13 cout << endl;
14 x = 3, y = 4;
15 print(x, y);
16 swap2(&x, &y); // 函数调用,注意:指针作为形参时,实参形式
17 print(x, y);
18 }
19 int main() {
20 test();
21 }
22 // 函数定义:交换两个变量(引用变量作为形参)
23 void swap1(int &rx, int &ry) {
24 int t;
25 t = rx; rx = ry; ry = t;
26 }
27 // 函数定义:交换两个变量(指针变量作为形参)
28 void swap2(int *px, int *py) {
29 int t;
30 t = *px; *px = *py; *py = t;
31 }
32 // 函数定义:输出两个变量(普通变量作为形参)
33 void print(int x, int y) {
34 std::cout << "x = " << x << ", y = " << y << "\n";
35 }
View Code

task2.cpp:

 1 #include <iostream>
 2 #include <typeinfo>
 3 using namespace std;
 4 int main() {
 5 int a;
 6 int &ra = a;
 7 ra = 4;
 8 int *pa = &a;
 9 *pa = 5;
10 // 以十六进制形式输出普通变量a, 引用变量ra,指针变量pa的地址
11 cout << "&a = " << hex << &a << endl;
12 cout << "&ra = " << hex << &ra << endl;
13 cout << "&pa = " << hex << &pa << "\n\n";
14 // 输出普通变量a, 引用变量ra,指针变量pa的值
15 cout << "a = " << a << endl;
16 cout << "ra = " << a << endl;
17 cout << "pa = " << hex << pa << endl;
18 // 输出指针变量pa指向的变量的值
19 cout << "*pa = " << *pa << "\n\n";
20 // 输出普通变量a,引用变量ra, 指针变量pa的类型信息
21 cout << "type a: " << typeid(a).name() << endl;
22 cout << "type ra: " << typeid(ra).name() << endl;
23 cout << "type pa: " << typeid(pa).name() << endl;
24 }
View Code

task3.cpp:

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 template<typename T>
 5 void output(const T &x) {
 6 for(auto i: x)
 7 std::cout << i << ", ";
 8 std::cout << "\b\b \n";
 9 }
10 template<typename T>
11 void square1(T &x) {
12 for(auto i: x) // i是普通类型
13 i *= i;
14 }
15 template<typename T>
16 void square2(T &x) {
17 for(auto &i: x) // i是引用类型
18 i *= i;
19 }
20 void test1() {
21 vector<int> x {1, 2, 3, 4, 5};
22 cout << "动态int型数组对象x内的元素值: ";
23 output(x);
24 cout << "调用函数square1()......" << endl;
25 square1(x);
26 cout << "动态int型数组对象x内的元素值: ";
27 output(x);
28 }
29 void test2() {
30 vector<int> x {1, 2, 3, 4, 5};
31 cout << "动态int型数组对象x内的元素值: ";
32 output(x);
33 cout << "调用函数square2()......" << endl;
34 square2(x);
35 cout << "动态int型数组对象x内的元素值: ";
36 output(x);
37 }
38 int main() {
39 cout << "测试1: " << endl;
40 test1();
41 cout << "\n测试2: " << endl;
42 test2();
43 }
View Code

1.在C++中,引用在声明时必须被初始化,并且一旦一个引用被绑定到一个对象,就不能再被重新绑定到另一个对象。引用总是代表同一个对象。另一方面,指针可以在任何时候被重新指向     另一个对象,或者被设为nullptr。所以引用与对象是同一个地址,指针则不是。

2.引用是直接引用的,即引用的对象可以直接访问。指针则是间接引用的,如果想要访问指针指向的对象需要用"*"符号。

3.函数中不使用引用或者指针的话原本的对象的数值是不会改变的。


任务5:

1.代码:
vectorInt.hpp:

 1 #pragma once
 2 #include<cassert>
 3 #include<iostream>
 4 
 5 class vectorInt{
 6 public:
 7     vectorInt(int n);
 8     vectorInt(int n,int value);
 9     vectorInt(const vectorInt &vp);
10     ~vectorInt();
11     
12     int get_size()const;
13     int& at(int index);
14     int& at(int index)const;
15     void show()const;
16     
17 private:
18     int size;
19     int* ptr;
20 
21 };
22 
23 vectorInt::vectorInt(int n):size{n}{
24 ptr=new int[n];
25 std::cout << "constructor vectorInt(int n) called. \n";
26 }
27 
28 vectorInt::vectorInt(int n,int value):size{n}{
29 ptr=new int[n];
30 for(int i=0;i<n;i++)
31 {
32     ptr[i]=value;
33 }
34  std::cout << "constructor vectorInt(int n, int value) called. \n";
35 }
36 
37 vectorInt::vectorInt(const vectorInt &vp):size{vp.size},ptr{new int[size]}
38 {
39     for(auto i = 0; i < size; ++i)
40     ptr[i] = vp.ptr[i];
41     std::cout << "copy constructor called. \n";
42 }
43 
44 vectorInt::~vectorInt() {
45     delete[] ptr;
46     std::cout << "destructor called" << std::endl;
47 }
48 
49 int vectorInt::get_size() const {
50     return size;
51 }
52 
53 int& vectorInt::at(int index)
54 {
55     return ptr[index];
56 }
57 int& vectorInt::at(int index)const
58 {
59     return ptr[index];
60 }
View Code

task5.cpp:

 1 #include "vectorInt.hpp"
 2 #include <iostream>
 3 using std::cout;
 4 using std::cin;
 5 using std::endl;
 6 // 函数output()的定义:遍历输出vectorInt对象内的所有元素
 7 void output(const vectorInt &vi) {
 8     for(auto i = 0; i < vi.get_size(); ++i)
 9     cout<<vi.at(i)<<", ";
10     cout<<"\b\b\n";
11 
12 }
13 void test() {
14 int n;
15 cout << "输入vectorInt对象中元素个数: ";
16 cin >> n;
17 vectorInt x1(n); // 构造动态int数组对象x1,包含n个元素,不对元素初始化
18 for(auto i = 0; i < n; ++i)
19 x1.at(i) = i*i;
20 cout << "vectorInt对象x1: ";
21 output(x1);
22 vectorInt x2(n, 42); // 构造动态int数组对象x1,包含n个元素,每个元素初始值为42
23 cout << "vectorInt对象x2: ";
24 output(x2);
25 vectorInt x3(x2); // 使用x2构造x3
26 cout << "vectorInt对象x3: ";
27 output(x3);
28 cout << "更新vectorInt对象x2......\n";
29 x2.at(0) = 77;
30 x2.at(1) = -999;
31 cout << "vectorInt对象x2: ";
32 output(x2);
33 cout << "vectorInt对象x3: ";
34 output(x3);
35 }
36 int main() {
37 test();
38 }
View Code

2.图片:

 

任务6:

1.代码:

matrix.hpp:

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

task6.cpp:

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 using namespace std;
 4 const int N1 = 3;
 5 const int N2 = 2;
 6 // 输出一个矩阵对象中索引为index对应的行的所有元素值
 7 void output(const Matrix &m, int index) {
 8 for(auto j = 0; j < m.get_cols(); ++j)
 9 cout << m.at(index, j) << ", ";
10 cout << "\b\b \n";
11 }
12 void test() {
13 double x[N1*N2] = {1, 2, 3, 4, 5, 6};
14 Matrix m1(N1, N2); // 创建一个N1×N2矩阵
15 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值
16 cout << "矩阵对象m1: " << endl;
17 m1.print(); // 打印矩阵m1的值
18 cout << "矩阵对象m1第0行是: " << endl;
19 output(m1, 0);
20 cout << endl;
21 Matrix m2(N2, N1);
22 m2.set(x);
23 cout << "矩阵对象m2: " << endl;
24 m2.print();
25 cout << "矩阵对象m2第0行是: " << endl;
26 output(m2, 0);
27 cout << endl;
28 Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
29 m3.set(0, 0, 999); // 讲矩阵对象m2索引(0,0)元素设为999
30 cout << "矩阵对象m3:" << endl;
31 m3.print();
32 cout << endl;
33 Matrix m4(2); // 创建一个2*2矩阵对象
34 m4.set(x); // 用一维数组x的值按行为矩阵m4赋值
35 cout << "矩阵对象m4:" << endl;
36 m4.print();
37 }
38 int main() {
39 test();
40 }
View Code

2.图片: