实验3 类和对象Ⅱ
实验任务4
vector_int.hpp
1 #ifndef VECTOR_INT_HPP 2 #define VECTOR_INT_HPP 3 4 #include<iostream> 5 #include <cassert> 6 using std::cout; 7 using std::endl; 8 9 class Vector_int{ 10 11 public: 12 Vector_int(int size0); 13 Vector_int(int size0, int value); 14 Vector_int(const Vector_int &v); 15 int &at(int index); 16 ~Vector_int(); 17 void show() const; 18 void show_Vector_Address() const; 19 20 private: 21 int size; 22 int *p; 23 }; 24 25 Vector_int::Vector_int(int size0): size(size0) { 26 cout << "dynamic create array..." << endl; 27 p = new int[size]; 28 for (auto i = 0; i < size; i++) { 29 p[i] = 0; 30 } 31 } 32 33 Vector_int::Vector_int(int size0, int value): size(size0) { 34 cout << "dynamic create array...initialize value" << endl; 35 p = new int[size]; 36 for (auto i = 0; i < size; i++) { 37 p[i] = value; 38 } 39 } 40 41 Vector_int::Vector_int(const Vector_int &v): size(v.size), p(v.p) { 42 43 } 44 45 int &Vector_int::at(int index) { 46 assert(index >= 0 && index < size); 47 //assert是标准c++头文件cassert中定义的一个宏,用来判断一个条件表达式的值是否为true。 48 //如果是false,会终止。 49 return p[index]; 50 } 51 52 Vector_int::~Vector_int() { 53 cout << "deleting..." << endl; 54 delete[] p; 55 } 56 57 void Vector_int::show() const { 58 for (auto i = 0; i < size; i++) { 59 cout << p[i] << ", "; 60 } 61 cout << "\b\b \n"; 62 } 63 64 void Vector_int::show_Vector_Address() const { 65 cout << p << endl; 66 } 67 68 #endif
task4.cpp
1 #include "vector_int.hpp" 2 #include<iostream> 3 using namespace std; 4 5 int main() { 6 7 Vector_int x(8); // 创建一个动态大小的int型数组对象x,向系统申请8个int型数据项空间 8 cout << "数组x: "; 9 x.show(); 10 cout << "数组x的地址: "; 11 x.show_Vector_Address(); 12 13 Vector_int y(8, 6); // 创建一个动态大小的int型数组对象y,向系统申请8个int型数据项空间, 初始值均为6 14 cout << "数组y: "; 15 y.show(); 16 17 Vector_int z(x); // 用已经存在的对象x构造新的对象z 18 cout << "数组z: "; 19 z.show(); 20 cout << "数组z的地址: "; 21 z.show_Vector_Address(); 22 23 z.at(0) = 999; // 通过at()方法访问对象z中索引为0的数据项 24 cout << "修改过后的数组z: "; 25 z.show(); 26 }
实验任务5
Matrix.hpp
1 #ifndef MATRIX_H 2 #define MATRIX_H 3 4 #include <iostream> 5 #include <cassert> 6 using std::cout; 7 8 class Matrix 9 { 10 public: 11 Matrix(int n); // 构造函数,构造一个n*n的矩阵 12 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 13 Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 14 ~Matrix(); //析构函数 15 void set(const double *pvalue); // 用pvalue指向的连续内存块数据为矩阵赋值 16 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 17 double &at(int i, int j); //返回矩阵第i行第j列元素的引用 18 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 19 int get_lines() const; //返回矩阵行数 20 int get_cols() const; //返回矩列数 21 void print() const; // 按行打印输出矩阵 22 private: 23 int lines; // 矩阵行数 24 int cols; // 矩阵列数 25 double *p; // 指向存放矩阵数据的内存块的首地址 26 }; 27 28 Matrix::Matrix(int n): lines(n), cols(n) { 29 p = new double[n * n]; 30 } 31 32 Matrix::Matrix(int n, int m) : lines(n), cols(m) { 33 p = new double[n * m]; 34 } 35 36 Matrix::Matrix(const Matrix &X): lines(X.lines), cols(X.cols) { 37 p = new double[lines * cols]; 38 for (int i = 0; i < lines * cols; i++){ 39 p[i] = X.p[i]; 40 } 41 } 42 43 Matrix::~Matrix() { 44 delete[] p; 45 } 46 47 void Matrix::set(const double *pvalue) { 48 int num = lines * cols; 49 for (int i = 0; i < num; i++) { 50 p[i] = pvalue[i]; 51 } 52 } 53 54 void Matrix::set(int i, int j, int value) { 55 assert((i >= 0 && i < lines) && (j >= 0 && j < cols)); 56 p[i*cols+j] = value; 57 } 58 59 double &Matrix::at(int i, int j) { 60 assert((i >= 0 && i < lines) && (j >= 0 && j < cols)); 61 return p[i*cols+j]; 62 } 63 64 double Matrix::at(int i, int j) const { 65 assert((i >= 0 && i < lines) && (j >= 0 && j < cols)); 66 return p[i*cols+j]; 67 } 68 69 int Matrix::get_lines() const { 70 return lines; 71 } 72 73 int Matrix::get_cols() const { 74 return cols; 75 } 76 77 void Matrix::print() const { 78 int num = 0; 79 for (int i = 0; i < lines; i++){ 80 for (int j = 0; j < cols; j++){ 81 cout << p[num] << ", "; 82 num++; 83 } 84 cout << "\b\b \n"; 85 } 86 } 87 #endif
task5.cpp
1 #include <iostream> 2 #include "matrix.hpp" 3 4 int main() 5 { 6 using namespace std; 7 8 double x[] = {1, 3, 5, 2, 4, 6}; 9 10 Matrix m1(3, 2); // 创建一个3×2的矩阵 11 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 12 m1.print(); // 打印矩阵m1的值 13 cout << "the first line is: " << endl; 14 cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; 15 cout << endl; 16 17 Matrix m2(2, 3); 18 m2.set(x); 19 m2.print(); 20 cout << "the first line is: " << endl; 21 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 22 cout << endl; 23 24 Matrix m3(m2); 25 m3.set(0, 0, 999); 26 m3.print(); 27 }