vectorInt.hpp
#pragma once #include<iostream> #include<cassert> using namespace std; class vectorInt{ public: vectorInt(int n); vectorInt(int n,int value); vectorInt(const vectorInt &vp); ~vectorInt(); int &at(int index); int get_size(); friend void output(vectorInt &T); private: int size; int *p; }; vectorInt::vectorInt(int n) :size{n} { cout<<"constructor 1 called.\n"; p = new int[n]; } vectorInt::vectorInt(const vectorInt &vp) :size{vp.size} { cout<<"copy constructor called.\n"; p=new int[size]; for(int i =0;i<size ;i++) p[i]=vp.p[i]; } vectorInt::vectorInt(int n,int value) :size{n} { cout<<"cosntructor 2 called.\n"; p = new int[size]; } vectorInt::~vectorInt() { cout<<"destructor called.\n"; delete[] p; } int &vectorInt::at(int index) { assert(index >=0 && index <size); return p[index]; } int vectorInt::get_size() { return size; } void output(vectorInt &T){ for(int i=0;i<T.get_size();i++) cout<<T.p[i]<<" "; cout<<endl; }
main.cpp
#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(); }
Matrix.hpp
#include <iostream> #include "matrix.hpp" using namespace std; void test() { 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(); }
main.cpp
#include <iostream> #include "matrix.hpp" using namespace std; void test() { 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; couy<<endl; Matix m3(m2); m3.set(0,0,999); m3.print(); } int main(){ test(); }
标签:m1,int,hpp,实验,output,vectorInt,include From: https://www.cnblogs.com/wzw252/p/16861724.html