1. 程式人生 > 其它 >實驗三 類和物件Ⅱ

實驗三 類和物件Ⅱ

實驗任務4

vector_int.hpp

 1 #ifndef VECTOR_INT_HPP
 2 #define VECTOR_INT_HPP
 3 
 4 #include<iostream>
 5 #include<cassert>
 6 
 7 class Vector_int{
 8     public:
 9         Vector_int(int n,int value=0);
10         Vector_int(const Vector_int &v);
11         int& at(int index);
12         ~Vector_int();
13 void show() const; 14 private: 15 int size; 16 int *p; 17 }; 18 19 Vector_int::Vector_int(int n,int value):size{n}{ 20 p=new int[size]; 21 for(auto i=0;i<size;i++) 22 p[i]=value; 23 std::cout<<"dynamic create array..."<<std::endl; 24 }
25 26 Vector_int::Vector_int(const Vector_int &v):size(v.size){ 27 p=new int[size]; 28 for(auto i=0;i<size;i++) 29 p[i]=v.p[i]; 30 } 31 32 int& Vector_int::at(int index){ 33 assert(index>=0&&index<size); 34 return p[index]; 35 } 36 37 Vector_int::~Vector_int(){
38 delete [] p; 39 std::cout<<"deleting..."<<std::endl; 40 } 41 42 void Vector_int::show() const{ 43 for(auto i=0;i<size;i++) 44 std::cout<<p[i]<<", "; 45 std::cout<<"\b\b \n"; 46 } 47 48 #endif

task4.cpp

 1 #include"vector_int.hpp"
 2 #include<iostream>
 3 
 4 int main(){
 5     using namespace std;
 6     
 7     int n;
 8     cin>>n; 
 9     
10     Vector_int x1(n);
11     x1.show();
12     
13     Vector_int x2(n,6);
14     x2.show();
15     
16     Vector_int y(x1);
17     y.show();
18     
19     y.at(0)=999;
20     y.show();
21     
22     Vector_int y1(x2);
23     y1.show();
24     
25     y1.at(0)=999;
26     y1.show();
27 }

執行測試結果截圖

實驗任務5

Matrix.hpp

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

task5.cpp

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

執行測試結果截圖