实验4

实验4.5:

vectorInt.hpp

 1 #include <iostream>
 2 using namespace std; 
 3 class vectorInt{
 4     public:
 5     vectorInt(int n);
 6     vectorInt(int n,int value);
 7     vectorInt( vectorInt& T);
 8     ~vectorInt();
 9     int &at(int i);
10     int get_size();
11     friend void output(vectorInt &T);
12     private:
13     int* p;    int size;
14 }; 
15 vectorInt::vectorInt(int n):size{n}{
16     p=new int[n];
17     for(auto i=0:n){
18         p[i]=0;
19     }
20     cout<<"constructor 1 called.\n";
21 }
22 vectorInt::vectorInt(int n,int value):size{n}{
23         p=new int[n];
24         
25     for(int i=0;i<size;++i){
26         p[i]=value;
27     }
28     cout<<"constructor 2 called.\n";
29 }
30 vectorInt::vectorInt( vectorInt& T){
31     this->p=new int[T.size];
32     this->size=T.size;
33     for(int i=0;i<size;++i){
34         this->p[i]=T.at(i);
35     }
36 cout<<"copy constructor called.\n";
37 }
38 int &vectorInt::at(int i){
39     return p[i];
40 }
41 int vectorInt::get_size(){
42     return size;
43 }
44 void output(vectorInt &T){
45     for(int i=0;i<T.get_size();++i){
46     cout<<T.at(i)<<", ";
47     }
48     cout<<"\b\b \n";
49 }
50 vectorInt::~vectorInt(){
51     delete [] p;
52     cout<<"destructor called.\n";
53 }

task5.cpp

 1 #include <iostream>
 2 #include "vectorInt.hpp"
 3 void test(){
 4     using namespace std;
 5     int n;
 6     cin>>n;
 7     vectorInt x1(n);
 8     for(auto i=0;i<n;i++){
 9         x1.at(i)=i*i;
10     }
11     output(x1);
12     vectorInt x2(n,42);
13     vectorInt x3(x2);
14     
15     output(x2);
16     output(x3);
17     
18     x2.at(0)=77;
19     output(x2);
20     output(x3);
21 }
22 int main(){
23     test();}

 

 

实验4.6

matrix.hpp:

 1 #pragma once
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 class Matrix{
 6     public:
 7         Matrix(int n);
 8         Matrix(int n,int m);
 9         Matrix(const Matrix &X);
10         ~Matrix();
11         
12         void set(const double* pvalue);
13         void set(int i,int j,int value);
14         double &at(int i,int j)const;
15         int get_lines()const;
16         int get_cols()const;
17         void print()const;
18     private:
19         int lines;
20         int cols;
21         double* p;
22 };
23 int Matrix::get_lines()const{
24     return lines;
25 }
26 int Matrix::get_cols()const{
27     return cols;
28 }
29 Matrix::Matrix(int n):lines{n},cols{n}{
30     p=new double[n*n];
31 }
32 Matrix::Matrix(int n,int m):lines{n},cols{m}{
33     p=new double[n*m];
34 }
35 Matrix::Matrix(const Matrix& X){
36     this->lines=X.get_lines();
37     this->cols=X.get_cols();
38     this->p=X.p;
39 }
40 void Matrix::set(const double*pvalue){
41     int k=0;
42     for(k=0;k<lines*cols;k++){
43     p[k]=pvalue[k];
44     }
45 }
46 void Matrix::set(int i,int j,int value){
47     int k=i*cols+j;
48     p[k]=value;
49 }
50 double &Matrix::at(int i,int j)const{
51         return p[i*cols+j];
52 }
53 void Matrix:: print()const{
54     for(int i=0;i<lines;++i){
55         for(int j=0;j<cols;++j){
56             if(j==cols-1){
57                 cout<<p[i*cols+j]<<endl;
58             }
59             else{
60                 cout<<p[i*cols+j]<<", ";
61             }
62         }
63     }
64 }
65 Matrix::~Matrix(){
66     delete [] p;
67 }

task6.cpp:

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 void test(){
 4     using namespace std;
 5     double x[]={1,2,3,4,5,6};
 6     
 7     Matrix m1(3,2);
 8     m1.set(x);
 9     m1.print();
10     cout<<"the first line is: "<<endl;
11     cout<<m1.at(0,0)<<" "<<m1.at(0,1)<<endl;
12     cout<<endl;
13     
14     Matrix m2(2,3);
15     m2.set(x);
16     m2.print();
17     cout<<"the first line is: "<<endl;
18     cout<<m2.at(0,0)<<" "<<m2.at(0,1)<<" "<<m2.at(0,2)<<endl;
19     cout<<endl;
20     
21     Matrix m3(m2);
22     m3.set(0,0,999);
23     m3.print();
24     
25 }
26 int main(){
27     test();
28 }

 

posted @ 2022-11-05 18:02  ~GZJ  阅读(22)  评论(0)    收藏  举报