实验三 类与对象Ⅱ

实验任务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 inital_val=0);
10         Vector_int(const Vector_int &v);
11         ~Vector_int();
12         int& at(int index);
13         void show()const;
14     private:
15         int size;
16         int *a;
17 };
18 
19 Vector_int::Vector_int(int n,int initial_val):size{n}{
20     a=new int[size];
21     for(auto i=0;i<size;++i)
22         a[i]=initial_val;
23     std::cout<<"dynamic create array..."<<std::endl;
24 }
25 Vector_int::Vector_int(const Vector_int &v):size{v.size}{
26     a=new int[size];
27     for(auto i=0;i<size;++i)
28         a[i]=v.a[i];
29 }
30 Vector_int::~Vector_int(){
31     std::cout<<"deleting..."<<std::endl;
32     delete [] a;
33 }
34 int& Vector_int::at(int index){
35     assert(index>=0&&index<size);
36     return a[index];
37 }
38 void Vector_int::show()const{
39     for(auto i=0;i<size;i++)
40         std::cout<<a[i]<<", ";
41     std::cout<<"\b\b \n";
42 }
43 #endif 

task4.cpp

 1 #include"vector_int.hpp"
 2 #include<iostream>
 3 
 4 int main(){
 5     using namespace std;
 6     int n;
 7     cin>>n;
 8     
 9     Vector_int v1(n);
10     v1.show();
11     
12     Vector_int v2(n,7);
13     v2.show();
14     
15     Vector_int v3(v1);
16     v3.at(0)=777;
17     v3.show();
18     
19     Vector_int v4(v2);
20     v4.at(0)=111;
21     v4.show();
22 }

运行截图:

实验三 类与对象Ⅱ

 

 

 实验任务5

matrix.hpp

 1 #ifndef MATRIX_HPP
 2 #define MATRIX_HPP
 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 Matrix::Matrix(int n):lines{n},cols{n}{
25     p=new double[lines*cols];
26 }
27 Matrix::Matrix(int n,int m):lines{n},cols{m}{
28     p=new double[lines*cols];
29 }
30 Matrix::Matrix(const Matrix &X):lines{X.lines},cols{X.cols}{
31     p=new double[lines*cols];
32     for(auto i=0;i<lines*cols;++i)
33         p[i]=X.p[i];
34 }
35 Matrix::~Matrix(){
36     std::cout<<"deleting..."<<std::endl;
37     delete [] p;
38 }
39 void Matrix::set(const double *pvalue){
40     for(auto i=0;i<lines;++i)
41         for(auto j=0;j<cols;++j)
42             p[i*cols+j]=pvalue[i*cols+j];
43 }
44 void Matrix::set(int i,int j,int value){
45     p[i*cols+j]=value;
46 }
47 double& Matrix::at(int i,int j){
48     return p[i*cols+j];
49 }
50 double Matrix::at(int i,int j)const{
51     return p[i*cols+j];
52 }
53 int Matrix::get_lines()const{
54     return lines;
55 }
56 int Matrix::get_cols()const{
57     return cols;
58 }
59 void Matrix::print()const{
60     for(auto i=0;i<lines;++i){
61         for(auto j=0;j<cols;++j)
62             std::cout<<p[i*cols+j]<<", ";
63         std::cout<<"\b\b \n";
64     }
65 }
66 #endif

task5.cpp

 1 #include "matrix.hpp"
 2 #include <iostream>
 3 
 4 int main(){
 5     using namespace std;
 6 
 7     double x[] = {1, 2, 3, 4, 5, 6};
 8 
 9     Matrix m1(3, 2);    // 创建一个3×2的矩阵
10     m1.set(x);          // 用一维数组x的值按行为矩阵m1赋值
11     m1.print();         // 打印矩阵m1的值
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, 999);
25     m3.print();
26     return 0;
27 }

运行截图:

实验三 类与对象Ⅱ

 

 总结:

1.本次实验我熟悉使用了动态内存分配new以及delete的用法;

2.了解了assert的条件表达值的判断;

上一篇:Python实现简单的Web完整版(一)


下一篇:JavaScript 属性类型(数据属性和访问器属性)