Three types and objects of cpp experiment II

Posted by cloudnyn3 on Sat, 06 Nov 2021 00:09:21 +0100

1, Experimental task 4

Simulation experiment task 2, do not use the standard library template class vector, design and implement a dynamic integer array class vector by yourself_ int,
Enable it to support the following requirements:
Supports specifying the size of an int array object when it is created
When creating an int array object, specify its size and initialize each data item in the array object to a specific value value
Support the construction of new int array object y with existing int array object x (deep copy is required)
The method at() provided can support accessing the ith data item in a dynamic int array object by index, such as x.at(i)
Destructor to release the occupied memory resources
Add printout information in constructors and destructors. When running the program, it helps to observe whether the resources are released correctly.
That is, in the test code, the following code operations are supported:
requirement:
① Design and implement the dynamic integer array class Vector_int, saved in the file vector_ In int.hpp
② Write the test code file task4.cpp to test whether its constructor interface, copy constructor, at() method, etc. are used normally,
Whether deep replication is implemented.
After analysis, the code is as follows:
Vector_int.hpp is as follows:
 1 #include<iostream> 
 2 #include<cassert>
 3 using namespace std;
 4 
 5 class Vector_int
 6 {
 7 private:
 8     int n,x;//n Is the array length, x Is the default initial value 
 9     int *p;//array 
10 public:
11     Vector_int(int n0):n(n0),x(0)
12     {
13         p=new int[n];
14         for(int i=0;i<n0;i++)
15         p[i]=x; 
16     };//Constructor
17     Vector_int(int n0,int x0):n(n0),x(x0)
18     {
19         p=new int[n];
20         for(int i=0;i<n0;i++)
21         p[i]=x;
22     };//Constructor
23     Vector_int(const Vector_int &y)//copy constructor  
24     {
25         n=y.n;
26         x=y.x;
27         p=new int[n];
28         for(int i=0;i<n;i++)
29         p[i]=y.p[i];
30     } 
31     
32     void print() const;//output 
33     ~Vector_int()//Destructor
34     {
35     delete[] p;
36     cout<<"destructor called"<<endl;
37     } 
38     
39     int &at(int index);//returnSubscript is index Data reference for 
40     
41     
42 };
43 
44 void Vector_int::print() const{
45     for(int i=0;i<n;i++){
46         cout<<p[i]<<"  ";
47     }
48     cout<<endl;
49     
50 }
51 
52 int &Vector_int::at(int index)
53 {
54     assert(index >= 0 && index < n);
55     return p[index];
56  } 

task4.cpp is as follows:

 1 #include<iostream>
 2 #include"Vector_int.hpp"
 3 using namespace std;
 4 int main()
 5 {
 6     int n;
 7     cout<<"Please enter the array length:\n";
 8     cin>>n;
 9     Vector_int x0(n); 
10     cout<<"x0=";
11     x0.print();
12     int n1,x;
13     cout<<"Please enter the array length and initial value:\n"; 
14     cin>>n1>>x;
15     Vector_int x1(n1, x);
16     cout<<"x1=";
17     x1.print();
18     Vector_int y(x1);
19     cout<<"y=";
20     y.print();
21     y.at(0) = 999;
22     cout<<"y=";
23     y.print();
24     return 0;
25 }

The operation results are as follows:

 

 

2, Experiment task 5

Implement a dynamic Matrix class Matrix. See the file Matrix.hpp for the declaration of class Matrix.
① Definition of implementation class Matrix
② Use task5.cpp to test the Matrix class Matrix.
After analysis, Matrix.hpp is as follows:
 1 #ifndef MATRIX_H
 2 #define MATRIX_H
 3 
 4 #include <iostream>
 5 #include <cassert>
 6 using namespace std;
 7 class Matrix
 8 {
 9 public:
10     Matrix(int n);                     // Constructor, construct a n*n Matrix of
11     Matrix(int n, int m);              // Constructor, construct a n*m Matrix of
12     Matrix(const Matrix &X);           // Copy constructor, using existing matrix X structure
13     ~Matrix() //Destructor
14     {
15         delete[] p;
16         cout<<"destructor called\n";
17     }
18     void set(const double *pvalue);     // use pvalue The continuous memory block data pointed to assigns a value to the matrix
19     void set(int i, int j, int value); //Set matrix No i Line number j Column element value is value
20     double &at(int i, int j);          //Return to matrix No i Line number j Reference to column element
21     double at(int i, int j) const;     // Return to matrix No i Line number j The value of the column element
22     int get_lines() const;             //Returns the number of rows in the matrix
23     int get_cols() const;              //Returns the number of moment columns
24     void print() const;                // Printout matrix by row
25 private:
26     int lines; // Number of matrix rows
27     int cols;  // Number of matrix columns
28     double *p; // Refers to the first address of the memory block where the matrix data is stored
29 };
30 
31 Matrix::Matrix(int n):lines(n),cols(n)
32 {
33     p=new double[n*n];
34 }
35 
36 Matrix::Matrix(int n,int m):lines(n),cols(m)
37 {
38     p=new double[n*m];
39 }
40 
41 Matrix::Matrix(const Matrix &X ):lines(X.lines),cols(X.cols)
42 {
43     p=new double[lines*cols];
44     for(int i=0;i<lines*cols;i++)
45         p[i]=X.p[i];
46 }
47 
48 void Matrix::set(const double *pvalue)
49 {
50     for(int i=0; i<lines*cols; i++)
51         p[i]=*(pvalue+i);
52 }
53 
54 void Matrix::set(int i,int j,int value)
55 {
56     assert((i >= 0 && i < lines) && (j >= 0 && j < cols));
57     p[i*cols+j]=value;    
58 }
59 
60 double Matrix::at(int i, int j) const {
61     assert((i >= 0 && i < lines) && (j >= 0 && j < cols));
62     return p[i*cols+j];
63 }
64 double & Matrix::at(int i, int j) {
65     return p[i*cols+j];
66 }
67 
68 int Matrix::get_lines() const {
69     return lines;
70 }
71 int Matrix::get_cols() const {
72     return cols;
73 }
74 
75 void Matrix::print() const {
76     for(int i=0; i<lines; i++) {
77         for(int j=0; j<cols; j++) {
78             cout<<p[i*cols+j]<<" ";
79         }
80 
81         cout<<endl;
82     }
83 }
84 
85 
86 
87 
88 
89 #endif

task5.cpp is as follows:

 1 #include <iostream>
 2 #include "Matrix.hpp"
 3 
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     double x[] = {1, 2, 3, 4, 5, 6};
 9 
10     Matrix m1(3, 2);    // Create a 3×2 Matrix of
11     m1.set(x);          // Using a one-dimensional array x Values by behavior matrix m1 assignment
12     m1.print();         // Print matrix m1 Value of
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 }

The operation results are shown in the figure below:

 

 

3, Experimental conclusion

1. The reference does not have its own address, and the pointer has its own address

2. When a reference is used as a formal parameter, such as & x, x is used as an argument;

    When a pointer is used as an argument, such as * x, use & X

3.*pa=a, & pa is your own address and pa is the address of A

   & When ra = a, & ra is the address of a and ra is the value of A

4. If you need a group of arrays in a class, you can use pointers to help solve this problem. For example, define * p, p=new double []

5.assert is a macro defined in the standard c + + header file cassert, which is used to judge whether the value of a conditional expression is true.

If false, it will terminate.
6. The reference type should also be used in the implementation of functions with reference type, such as double & matrix:: at (int i, int j)