标签:const Matrix 对象 lines cols II int Vector 实验
实验任务四
//vector_int.hpp
#ifndef VECTOR_INT #define VECTOR_INT #include<iostream> #include<cassert> using namespace std; class Vector_int { public: Vector_int(int n); Vector_int(int m, int n); Vector_int(const Vector_int& x); ~Vector_int(); int& at(int n); void show() const; private: int size; int* p; }; Vector_int::Vector_int(int n) : size(n) { cout << "Default constructor called." << endl; p = new int[n](); } Vector_int::Vector_int(int m, int n) : size(m) { cout << "constructor called." << endl; p = new int[m]; for (int i = 0; i < m; i++) { *(p + i) = n; } } Vector_int::Vector_int(const Vector_int& x) : size(x.size) { cout << "copy constructor called." << endl; p = new int[size]; for (auto i = 0; i < size; ++i) p[i] = x.p[i]; } int& Vector_int::at(int n) { assert(n >= 0 && n < size); return p[n]; } void Vector_int::show() const { for (auto i = 0; i < size; ++i) { cout << *(p + i) <<endl; } } Vector_int::~Vector_int() { cout << "destory the constructor" << endl; delete[] p; } #endif
//task 4 #include<iostream> #include"vector_int.hpp" using namespace std; int main() { int n; cin >> n; Vector_int x1(n); x1.show(); Vector_int x2(n, 6); x2.show(); Vector_int y(x2); y.show(); y.at(0) = 999; y.show(); return 0; }
实验任务五
//Matrix.hpp #pragma once #ifndef TEXTCODER_HPP #define TEXTCODER_HPP #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[lines * cols]; } Matrix::Matrix(int n, int m) : lines(n), cols(m) { p = new double[lines * cols]; } Matrix::Matrix(const Matrix& x) : lines(x.lines), cols(x.cols) { p = new double[lines * cols]; int length = lines * cols; for (int i = 0; i < length; ++i) p[i] = x.p[i]; } Matrix::~Matrix() { delete[] p; } void Matrix::set(const double* pvalue) { int length = lines * cols; for (int i = 0; i < length; ++i) p[i] = pvalue[i]; } void Matrix::set(int i, int j, int value) { p[(i + 1) * (j + 1) - 1] = value; } double& Matrix::at(int i, int j) { return p[(i + 1) * (j + 1) - 1]; } double Matrix::at(int i, int j)const { return p[(i + 1) * (j + 1) - 1]; } int Matrix::get_lines()const { return lines; } int Matrix::get_cols()const { return cols; } void Matrix::print()const { int t = -1; for (int i = 1; i <= lines; i++) { for (int j = 1; j <= cols; j++) cout << p[++t] << " "; cout << "\n"; } } #endif
//task 5.cpp #include<iostream> #include"Matrix.hpp" using namespace std; int main() { double x[] = { 1,2,3,4,5,6,7,8 }; Matrix m1(4, 2); m1.set(x); m1.print(); cout << "the first line is:" << endl; cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; cout << endl; Matrix m2(2, 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(0, 0, 999); m3.print(); }
标签:const,Matrix,对象,lines,cols,II,int,Vector,实验 来源: https://www.cnblogs.com/mzd211/p/15519399.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。