实验4.5
#pragma once #include<iostream> using namespace std; class vectorInt { friend void output(vectorInt&v); public: vectorInt(int n); vectorInt(int n, int value); vectorInt(vectorInt& v); ~vectorInt() { cout << "destructor called \n"; delete[]vec; } int &at(int i); void get_size()const; private: int size; int* vec; }; //构造函数接口 vectorInt::vectorInt(int n){ size = n; cout << "constructor called \n"; vec = new int[size](); } //构造函数接口 vectorInt::vectorInt(int n, int value) { cout << "constructor called"<<endl; size = n; vec = new int[size]; for (int i = 0; i < size; i++) { vec[i] = value; } } //复制构造函数接口 vectorInt::vectorInt(vectorInt& v) { cout << "copyconstructor called \n"; size = v.size; vec = new int[size]; for (int i = 0; i < size; i++) vec[i] = v.vec[i]; } //获取数组大小 void vectorInt::get_size()const{ cout << size<<endl; } //返回特定位置数组元素 int &vectorInt::at(int i){ return vec[i]; } //输出数组元素 函数接口 void output(vectorInt& v) { for (int i = 0; i < v.size; i++) cout << v.vec[i]<<" "; cout << endl; } #include <iostream> #include "vectorInt.hpp" void test() { using namespace std; int n; cin >> n; vectorInt x1(n); for (auto i = 0; i < n; ++i) x1.at(i) = i * i; output(x1); vectorInt x2(n, 42); vectorInt x3(x2); output(x2); output(x3); x2.at(0) = 77; output(x2); output(x3); } int main() { test(); }
实验4.6
#pragma once #include <iostream> using namespace std; class Matrix { public: Matrix(int n); Matrix(int n, int m); Matrix(const Matrix& X); ~Matrix(); void set(const double* pvalue); void set(int i, int j, int value); double& at(int i, int j); double at(int i, int j) const; int get_lines() const; int get_cols() const; void print() const; private: int lines; int cols; double * p; }; //构造函数接口 Matrix::Matrix(int n) { lines = n, cols = n; p = new double[n * n]; } //构造函数接口 Matrix::Matrix(int n, int m){ lines = n, cols = m; p = new double[n * m]; } //复制构造函数接口 Matrix::Matrix(const Matrix& X){ lines = X.lines, cols = X.cols; p = new double[lines*cols]; for (int i = 0; i < lines; i++) for (int j = 0; j < cols; j++) p[i*cols+j] = X.p[i*cols+j]; } //析构函数接口 inline Matrix::~Matrix(){ cout << "default constructor called\n"; delete[]p; } //用pvalue指向的连续内存块数据按行为矩阵赋值 void Matrix::set(const double* pvalue){ p = new double[lines * cols]; for (int i = 0; i < lines; i++) for (int j = 0; j < cols; j++) p[i*cols+j] = *(pvalue++); } //设置矩阵第i行第j列元素值为value void Matrix::set(int i, int j, int value){ p[i*cols+j] = value; } //返回矩阵第i行第j列元素的引用 double& Matrix::at(int i, int j) { return p[i*cols+j]; } //返回矩阵第i行第j列元素的值 double Matrix::at(int i, int j) const{ return p[i*cols+j]; } //返回矩阵行数 int Matrix::get_lines() const{ return lines; } //返回矩列数 int Matrix::get_cols() const{ return cols; } // 按行打印输出矩阵 void Matrix::print() const{ for (int i = 0; i < lines; i++) { for (int j = 0; j < cols; j++) { cout << p[i*cols+j] << " "; } cout << endl; } } #include <iostream> #include "Matrix.hpp" using namespace std; void test() { double x[9] = {1,4,5}; Matrix m1(3, 3); m1.set(x); m1.print(); cout << "the first line is: " << endl; cout << m1.at(0, 0) << " " << m1.at(0, 1) <<" "<<m1.at(0, 2) << endl; cout << endl; Matrix m2(3, 3); m2.set(x); m2.print(); cout << "the first line is: " << endl; cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; cout << endl; Matrix m3(m2); m3.set(1, 2, 999); m3.print(); } int main() { test(); }