实验三 类与对象II

task4: 模拟实验任务2,不使用标准库模板类vector,自己动手设计并实现一个动态的整型数组类Vector_int

vector_int.hpp

 1 #ifndef VECTOR_INT_HPP
 2 #define VECTOR_INT_HPP
 3 #include<iostream>
 4 #include<cassert>
 5 using namespace std;
 6 class Vector_int
 7 {
 8 private:
 9     int *p;
10     int size;
11 public:
12     Vector_int(int n,int a);
13     Vector_int(const Vector_int &a);
14     int &at(int index);
15     ~Vector_int();
16     friend void print(const Vector_int a);
17 };
18 
19 Vector_int::Vector_int(int n,int a=0):size(n)
20 {
21     p=new int[size](); 
22     for (int i = 0; i < size; i++)
23         *(p+i)=a;
24     cout<<"Constructor called."<<endl;    
25 }
26 Vector_int::Vector_int(const Vector_int &a):size(a.size)
27 {
28     p=new int[size]();
29     for (int i = 0; i < size; i++)
30         *(p+i)=*(a.p+i);
31     cout<<"Copy Constructor called."<<endl;
32 }
33 Vector_int::~Vector_int()
34 { 
35     delete []p;
36     cout<<"Destructor called."<<endl; 
37 }
38 int &Vector_int::at(int index)
39 {
40     assert(index>=0&&index<size);
41     return p[index];
42 }
43 #endif

task4.cpp

 1 #include<iostream>
 2 #include"vector_int.hpp"
 3 void print(const Vector_int a)
 4 {
 5     for (int i = 0; i < a.size; i++)
 6         cout<<a.p[i]<<ends;
 7     cout<<endl;
 8 }
 9 
10 int main()
11 {
12     int n;
13     cout<<"请输入数组大小:"; 
14     cin>>n;
15     Vector_int x(n); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间
16     Vector_int y(n, 5); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间, 初始值均为6
17     Vector_int z(x); // 用已经存在的对象x构造新的对象y
18     z.at(0) = 999; // 通过at()方法访问对象y中索引为0的数据项
19     print(x);
20     print(y);
21     print(z);
22 }

实验三 类与对象II

 

 task5:实现一个动态矩阵类Matrix

Matrix.cpp

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

task5.cpp

 1 #include<iostream>
 2 #include"Matrix.hpp"
 3 int main(){
 4     using namespace std;
 5 
 6     double x[] = {3, 6, 3, 4, 5, 6};
 7 
 8     Matrix m1(3, 2);    // 创建一个3×2的矩阵
 9     m1.set(x);          // 用一维数组x的值按行为矩阵m1赋值
10     m1.print();         // 打印矩阵m1的值
11     cout << "the first line is: " << endl;
12     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;
13     cout << endl;
14 
15     Matrix m2(2, 3);
16     m2.set(x);
17     m2.print();
18     cout << "the first line is: " << endl;
19     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
20     cout << endl;
21 
22     Matrix m3(m2);
23     m3.set(1, 1, 888);
24     m3.print();
25     return 0;
26 }

实验三 类与对象II

 

 总结:

1.学到了动态创建数组的方法,即new和delete的使用

2.学习到了标准c++头文件cassert中assert的使用

 

上一篇:CS229 6.13 Neurons Networks Implements of stack autoencoder


下一篇:实验3 类和对象II