1 #pragma once 2 #include<iostream> 3 using namespace std; 4 class vectorInt 5 { 6 public: 7 vectorInt(int n); 8 vectorInt(int n, int num); 9 vectorInt(vectorInt& other); 10 int get_size(); 11 int &at(int i) const; 12 ~vectorInt(); 13 void friend output(vectorInt& s); 14 private: 15 int size; 16 int* p; 17 }; 18 19 static int x = 0; 20 vectorInt::vectorInt(int n) 21 { 22 ++x; 23 size = n; 24 p = new int[size]; 25 cout << "constructor " << x << " called" << endl; 26 } 27 vectorInt::vectorInt(int n, int num) 28 { 29 ++x; 30 size = n; 31 p = new int[size]; 32 for (int i = 0;i < size;i++) 33 { 34 p[i] = num; 35 } 36 cout << "constructor " << x << " called" << endl; 37 38 } 39 vectorInt::vectorInt(vectorInt& other) 40 { 41 size = other.size; 42 p = new int[size]; 43 for (int i = 0;i < size;i++) 44 { 45 p[i] = other.p[i]; 46 } 47 cout << "copy constructor called." << endl; 48 } 49 int &vectorInt::at(int i) const 50 { 51 return p[i]; 52 53 } 54 int vectorInt::get_size() 55 { 56 return size; 57 58 } 59 vectorInt::~vectorInt() 60 { 61 cout << "destructor called." << endl; 62 delete[] p; 63 } 64 void output(vectorInt& s) 65 { 66 for (int i = 0;i < s.size;i++) 67 { 68 cout << s.p[i] << " "; 69 } 70 cout << endl; 71 }
hpp
1 #include"vectorInt.hpp" 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6 int n; 7 cin >> n; 8 vectorInt x1(n); 9 for (auto i = 0;i < n;i++) 10 { 11 x1.at(i) = i * i; 12 13 } 14 output(x1); 15 vectorInt x2(n, 42); 16 vectorInt x3(x2); 17 output(x2); 18 output(x3); 19 x2.at(0) = 77; 20 output(x2); 21 output(x3); 22 }
cpp
实验6
1 #pragma once 2 3 #include <iostream> 4 5 6 using std::cout; 7 using std::endl; 8 9 class Matrix { 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 16 void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 17 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 18 double& at(int i, int j); //返回矩阵第i行第j列元素的引用 19 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 20 int get_lines() const; //返回矩阵行数 21 int get_cols() const; //返回矩列数 22 void print() const; // 按行打印输出矩阵 23 24 private: 25 int lines; // 矩阵行数 26 int cols; // 矩阵列数 27 double* p; // 指向存放矩阵数据的内存块的首地址 28 }; 29 30 // 类Matrix的实现:待补足 31 // ××× 32 Matrix::Matrix(int n) 33 { 34 p = new double[n * n]; 35 lines = n; 36 cols = n; 37 } 38 Matrix::Matrix(int n, int m) 39 { 40 p = new double[n * m]; 41 lines = n; 42 cols = m; 43 } 44 Matrix::Matrix(const Matrix& X) 45 { 46 lines = X.lines; 47 cols = X.cols; 48 p = new double[lines * cols]; 49 for (int i = 0;i < lines*cols;i++) 50 { 51 p[i] = X.p[i]; 52 } 53 54 } 55 Matrix::~Matrix() 56 57 { 58 delete[] p; 59 } 60 void Matrix::set(const double* pvalue) 61 { 62 int i = 0; 63 while (i<cols*lines) 64 { 65 p[i++] = *(pvalue++); 66 } 67 } 68 void Matrix::set(int i, int j, int value) 69 { 70 p[i * cols + j ] = value; 71 } 72 double& Matrix::at(int i, int j) 73 { 74 return p[i * cols + j]; 75 } 76 double Matrix::at(int i, int j) const 77 { 78 return p[i * cols + j ]; 79 } 80 int Matrix::get_lines() const 81 { 82 return lines; 83 } 84 int Matrix::get_cols() const 85 { 86 return cols; 87 }//返回矩列数 88 void Matrix::print() const 89 { 90 int k = 0; 91 for (int i = 0;i < lines;i++) 92 { 93 cout << p[k++]; 94 for (int j = 1;j < cols;j++) 95 { 96 cout<<"," << p[k++]; 97 } 98 cout << endl; 99 } 100 }
hpp
1 #include <iostream> 2 #include "matrix.hpp" 3 4 void test() { 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; // 输出矩阵m1第1行两个元素的值 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); // 用矩阵m2构造新的矩阵m3 24 m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999 25 m3.print(); 26 } 27 28 int main() { 29 test(); 30 }
cpp
标签:const,Matrix,int,double,矩阵,实验,vectorInt,第四次 From: https://www.cnblogs.com/pdywsf/p/16867526.html