实验三

发布时间 2023-11-05 20:46:23作者: 汤谷雨
1.Point.hpp
 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 class Point{
 5     public:
 6         Point(int x0=0,int y0=0);
 7         ~Point()=default;
 8         int get_x() const;
 9         int get_y() const;
10         void show() const;
11         void move(int new_x,int new_y);
12         private:
13             int x,y;
14 };
15 Point::Point(int x0,int y0):x{x0},y{y0}{
16 }
17 int Point::get_x() const{
18 return x;}
19 int Point::get_y() const{
20 return y;}
21 void Point::show() const{
22 cout<<"("<<x<<","<<y<<")"<<endl;}
23 void Point::move(int new_x,int new_y){
24     x=new_x;
25     y=new_y;
26 }
View Code

  task1.cpp

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

 无变化

深复制

 

 

2.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 int Point::get_y() const{
21 return y;}
22 void Point::show() const{
23 cout<<"("<<x<<","<<y<<")"<<endl;}
24 void Point::move(int new_x,int new_y){
25     x=new_x;
26     y=new_y;
27 }
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         int get_size() const;
10         Point& at(int index);
11         Point& at(int index) const;
12         private:
13             int size;
14             Point *ptr;
15 };
16 vectorPoint::vectorPoint(int n):size{n}{
17 ptr=new Point[n];}
18 vectorPoint::~vectorPoint(){
19     delete[] ptr;
20 }
21 int vectorPoint::get_size() const{
22 return size;}
23 Point& vectorPoint::at(int index){
24     assert(index>=0&&index<size);
25     return ptr[index];
26 }
27 Point& vectorPoint::at(int index) const{
28     assert(index>=0&&index<size);
29     return ptr[index];
30 }
View Code

task2.cpp

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

 变化

浅复制

浅复制

 

 

3.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 int Point::get_y() const{
21 return y;}
22 void Point::show() const{
23 cout<<"("<<x<<","<<y<<")"<<endl;}
24 void Point::move(int new_x,int new_y){
25     x=new_x;
26     y=new_y;
27 }
View Code

task3.cpp

 1 #include"vectorPoint.hpp"
 2 #include<iostream>
 3 void output(const vectorPoint &v){
 4     for(auto i=0;i<v.get_size();++i)
 5     v.at(i).show();
 6 }
 7 void test(){
 8     using namespace std;
 9     int n;
10     cout<<"输入vectorPoint对象元素中元素个数:";
11     cin>>n;
12     vectorPoint x(n);
13     cout<<"x对象中所有点坐标信息:"<<endl;
14     output(x);
15     vectorPoint y(x);
16     cout<<"\ny对象中所有点坐标信息:"<<endl;
17     output(y);
18     cout<<"\n更新x对象中点坐标信息........."<<endl;
19     x.at(0).move(30,50);
20     x.at(1).move(-1,-1);
21     cout<<"x对象中所有点坐标信息........"<<endl;
22     output(x);
23     cout<<"\ny对象中所有点坐标信息:"<<endl;
24     output(y);
25      
26 }
27 int main(){
28     test();
29 }
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         int get_size() const;
10         Point& at(int index);
11         Point& at(int index) const;
12         private:
13             int size;
14             Point *ptr;
15 };
16 vectorPoint::vectorPoint(int n):size{n}{
17 ptr=new Point[n];}
18 vectorPoint::~vectorPoint(){
19     delete[] ptr;
20 }
21 int vectorPoint::get_size() const{
22 return size;}
23 Point& vectorPoint::at(int index){
24     assert(index>=0&&index<size);
25     return ptr[index];
26 }
27 Point& vectorPoint::at(int index) const{
28     assert(index>=0&&index<size);
29     return ptr[index];
30 }
View Code

 无变化

深复制

当类的成员中包含指针域成员时,默认复制构造函数进行浅复制。浅复制仅复制指针,新指针与原指针指向同一内存区域;深复制复制数据并为之分配单独的内存区域,新指针指向新的内存区域

 

 

4.task4_1.cpp

 1 #include<iostream>
 2 using namespace std;
 3 
 4 
 5 void swap1(int &rx,int &ry);
 6 void swap2(int *px,int *py);
 7 void print(int x,int y);
 8 
 9 
10 void test(){
11     int x=3,y=4;
12     
13     print(x,y);
14     swap1(x,y);
15     print(x,y);
16     
17     cout<<endl;
18     
19     x=3,y=4;
20     print(x,y);
21     swap2(&x,&y);
22     print(x,y);
23 }
24 
25 int main(){
26     test();
27 }
28 
29 
30 void swap1(int &rx,int &ry){
31     int t;
32     t=rx;rx=ry;ry=t;
33 }
34 
35 
36 
37 void swap2(int *px,int *py){
38     int t;
39     
40     t=*px;*px=*py;*py=t;
41 }
42 
43 
44 void print(int x,int y){
45     std::cout<<"x= "<<x<<",y= "<<y<<"\n";
46 }
View Code

  task4_2.cpp

 1 #include<iostream>
 2 #include<typeinfo>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int a;
 8     
 9     int &ra=a;
10     ra=4;
11     
12     int *pa=&a;
13     *pa=5;
14     
15     cout<<"&a="<<hex<<&a<<endl;
16     cout<<"&ra="<<hex<<&ra<<endl;
17     cout<<"&pa="<<hex<<&pa<<"\n\n";
18     
19     cout<<"a="<<a<<endl;
20     cout<<"ra="<<a<<endl;
21     cout<<"pa="<<hex<<pa<<endl;
22     
23     cout<<"*pa="<<*pa<<"\n\n";
24     
25     cout<<"type a:"<<typeid(a).name()<<endl;
26     cout<<"type ra:"<<typeid(ra).name()<<endl;
27     cout<<"type pa:"<<typeid(pa).name()<<endl;
28 }
View Code

task4_3.cpp

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

 

 

 

指针类型:本质是地址,可以独立存在,用于存放数据的地址。可以对指针本身进行操作,也可以通过指针去间接操作数据。引用类型:本质是别名,不能独立存在,依赖于源数据。所有操作同步反馈到源数据上。

 

 

5.task5.cpp

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

vectorint.hpp

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class vectorInt {
 5 public:
 6     // 构造函数
 7     vectorInt(int size = 0, int value = 0);
 8     // 复制构造函数
 9     vectorInt(const vectorInt& other);
10     // 析构函数
11     ~vectorInt();
12     // 返回vectorInt中数据项个数
13     int get_size() const;
14     // 访问vectorInt中第i个数据项,并返回引用
15     int& at(int i);
16 private:
17     int* data;
18     int size;
19 };
20 
21 
22 // 构造函数
23 vectorInt::vectorInt(int size, int value) {
24     this->size = size;
25     if (size > 0) {
26         data = new int [size];
27         for (int i = 0; i < size; i++) {
28             data[i] = value;
29         }
30     }
31     else {
32         data = nullptr;
33     }
34     cout << "vectorInt constructed, size = " << size << endl;
35 }
36 
37 // 复制构造函数
38 vectorInt::vectorInt(const vectorInt& other) {
39     size = other.size;
40     if (size > 0) {
41         data = new int [size];
42         for (int i = 0; i < size; i++) {
43             data[i] = other.data[i];
44         }
45     }
46     else {
47         data = nullptr;
48     }
49     std::cout << "vectorInt copied, size = " << size << std::endl;
50 }
51 
52 // 析构函数
53 vectorInt::~vectorInt() {
54     if (data != nullptr) {
55         delete[] data;
56         std::cout << "vectorInt destructed, size = " << size << std::endl;
57     }
58 }
59 
60 // 返回vectorInt中数据项个数
61 int vectorInt::get_size() const {
62     return size;
63 }
64 
65 // 访问vectorInt中第i个数据项,并返回引用
66 int& vectorInt::at(int i) {
67     if (i < 0 || i >= size) {
68         std::cerr << "Out of range!" << std::endl;
69         exit(EXIT_FAILURE); // 或者抛出一个异常
70     }
71     return data[i];
72 }
View Code

 

6.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

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 void set(int i, int j, double value); // 设置矩阵对象索引(i,j)的元素值为value
15 double& at(int i, int j) const; // 返回矩阵对象索引(i,j)的元素引用
16 double& at(int i, int j); // 返回矩阵对象索引(i,j)的元素引用
17 int get_lines() const; // 返回矩阵对象行数
18 int get_cols() const; // 返回矩阵对象列数
19 void print() const; // 按行打印输出矩阵对象元素值
20 private:
21 int lines; // 矩阵对象内元素行数
22 int cols; // 矩阵对象内元素列数
23 double *ptr;
24 };
25 // 类Matrix的实现:待补足
26 // xxx
View Code