实验4

//vectorInt.hpp
#include<iostream> using namespace std; class vectorInt{ private: int size; int *data; public: vectorInt(int n) { size=n; cout<<"constructor 1 called"<<endl; data=new int[n]; for(int i=0;i<size;i++) data[i]=0; } vectorInt(int n,int value) { size=n; cout<<"constructor 2 called"<<endl; data=new int[n]; for(int i=0;i<size;i++) data[i]=value; } ~vectorInt(){ cout<<"destructor called"<<endl; }; vectorInt(const vectorInt &p) { cout<<"copy constructor called"<<endl; size=p.size; data=new int[p.size]; for(int i=0;i<size;i++) data[i]=p.data[i]; } int &at(int i) { return data[i]; } int get_size() { return size; } friend void output(vectorInt &p); }; void output(vectorInt &p) { for(int i=0;i<p.size;i++) cout<<p.data[i]<<" "; cout<<endl; }

task.cpp:

#include <iostream>
#include "vectorInt.hpp"
  void test() {
using namespace std;
     int n;
     cin >> n;
     vectorInt x1(n);
     for(auto i = 0; i < n; ++i)
     x1.at(i) = i*i;
     output(x1);
     vectorInt x2(n, 42);
     vectorInt x3(x2);
     output(x2);
     output(x3);
     x2.at(0) = 77;
     output(x2);
     output(x3);
}
int main() {
     test();
}

运行结果:

 

 

 

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

task.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     Matrix m1(3, 2); 
 7     m1.set(x); 
 8     m1.print();
 9     cout << "the first line is: " << endl;
10     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; 
11     cout << endl;
12     Matrix m2(2, 3);
13     m2.set(x);
14     m2.print();
15     cout << "the first line is: " << endl;
16     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
17     cout << endl;
18     Matrix m3(m2); 
19     m3.set(0, 0, 999); 
20     m3.print();
21 }
22 int main() {
23    test();
24 }

运行结果

 

 

 
 
posted @ 2022-11-05 21:04  宇宙无敌纯爱暴龙战士  阅读(38)  评论(0)    收藏  举报