1 1 #pragma once 2 2 #include<iostream> 3 3 4 4 using std::cout; 5 5 using std::endl; 6 6 7 7 class vectorInt { 8 8 public: 9 9 //构造函数和析构函数 10 10 vectorInt(int n); 11 11 vectorInt(int n, int value); 12 12 vectorInt(const vectorInt& X); 13 13 ~vectorInt(); //~vectorInt()=default; 默认合成的析构函数 14 14 15 15 //其它成员函数 16 16 int& at(int i); 17 17 int get_size(); 18 18 19 19 //友元函数 20 20 friend void output(const vectorInt& X); 21 21 22 22 private: 23 23 int size; 24 24 int* p; 25 25 }; 26 26 27 27 vectorInt::vectorInt(int n) :size{ n } { 28 28 cout << "constructor 1 called.\n"; 29 29 p = new int[size]; 30 30 } 31 31 32 32 vectorInt::vectorInt(int n, int value) :size{ n } 33 33 { 34 34 cout << "constuctot 2 called" << endl; 35 35 p = new int[size]; 36 36 for (int i = 0; i < n; ++i) 37 37 p[i] = value; 38 38 } 39 39 40 40 vectorInt::vectorInt(const vectorInt& X) :size{ X.size } 41 41 { 42 42 cout << "copy constuctot called" << endl; 43 43 p = new int[X.size]; 44 44 for (auto i = 0; i < X.size; ++i) 45 45 p[i] = X.p[i]; 46 46 } 47 47 48 48 vectorInt::~vectorInt() { 49 49 cout << "destructor called.\n"; 50 50 delete[]p; 51 51 } 52 52 53 53 int& vectorInt::at(int i) { 54 54 return p[i]; 55 55 } 56 56 57 57 int vectorInt::get_size() { 58 58 return size; 59 59 } 60 60 61 61 void output(const vectorInt& X) { 62 62 for (auto i = 0; i < X.size; i++) 63 63 cout << X.p[i] << " "; 64 64 cout << endl; 65 65 } 66 67 vectorInt.hpp
1 1 #pragma once 2 2 #include<iostream> 3 3 4 4 using std::cout; 5 5 using std::endl; 6 6 7 7 class vectorInt { 8 8 public: 9 9 //构造函数和析构函数 10 10 vectorInt(int n); 11 11 vectorInt(int n, int value); 12 12 vectorInt(const vectorInt& X); 13 13 ~vectorInt(); //~vectorInt()=default; 默认合成的析构函数 14 14 15 15 //其它成员函数 16 16 int& at(int i); 17 17 int get_size(); 18 18 19 19 //友元函数 20 20 friend void output(const vectorInt& X); 21 21 22 22 private: 23 23 int size; 24 24 int* p; 25 25 }; 26 26 27 27 vectorInt::vectorInt(int n) :size{ n } { 28 28 cout << "constructor 1 called.\n"; 29 29 p = new int[size]; 30 30 } 31 31 32 32 vectorInt::vectorInt(int n, int value) :size{ n } 33 33 { 34 34 cout << "constuctot 2 called" << endl; 35 35 p = new int[size]; 36 36 for (int i = 0; i < n; ++i) 37 37 p[i] = value; 38 38 } 39 39 40 40 vectorInt::vectorInt(const vectorInt& X) :size{ X.size } 41 41 { 42 42 cout << "copy constuctot called" << endl; 43 43 p = new int[X.size]; 44 44 for (auto i = 0; i < X.size; ++i) 45 45 p[i] = X.p[i]; 46 46 } 47 47 48 48 vectorInt::~vectorInt() { 49 49 cout << "destructor called.\n"; 50 50 delete[]p; 51 51 } 52 52 53 53 int& vectorInt::at(int i) { 54 54 return p[i]; 55 55 } 56 56 57 57 int vectorInt::get_size() { 58 58 return size; 59 59 } 60 60 61 61 void output(const vectorInt& X) { 62 62 for (auto i = 0; i < X.size; i++) 63 63 cout << X.p[i] << " "; 64 64 cout << endl; 65 65 } 66 67 vectorInt.hpp
测试截图:
Task 6
#pragma once #include <iostream> using std::cout; using std::endl; 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]; for(int i=0;i<lines*cols;i++) p[i]=X.p[i]; } Matrix::~Matrix() {delete[] p;} void Matrix::set(const double *pvalue) { int i=0,j=0; while(i<lines*cols) { p[i++]=pvalue[j++]; } } void Matrix::set(int i, int j, int value) { p[i*cols+j]=value; } double &Matrix::at(int i, int j) { return p[i*cols+j]; } int Matrix::get_lines() const { return lines; } int Matrix::get_cols() const { return cols; } void Matrix::print() const { int i=0; while(i<lines) { int j=0; while(j<cols) { cout<<p[i*cols+j]<<" "; j++; } cout<<endl; i++; } }
#include<iostream> #include"matrix.hpp" void test(){ using namespace std; double x[]={1,2,3,4,5,6}; Matrix m1(3,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(); } int main() { test(); }
测试截图:
标签:const,Matrix,四类,lines,cols,int,数组,vectorInt,指针 From: https://www.cnblogs.com/yy-L886/p/16870624.html