实验六
Posted cppnewcomer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验六相关的知识,希望对你有一定的参考价值。
第一题
//bass.h //基类 //point class point{ public: void setm( double numbm) { m=numbm; } void setn( double numbn) { n=numbn; } double displayadd() { cout<<"m+n="<<m+n; } protected: double m; double n; }; //派生类A A芯片:计算两位整数的加法(m+n)、计算两位整数的减法(m-n) class A: public point{ public: double displaysub() { cout<<"m-n="<<m-n; } } ; //派生类B B 芯片:计算两位整数的加法(m+n)、计算两位整数的乘法(m*n) class B: public point{ public: double displaymul() { cout<<"m*n="<<m*n; } }; //派生类C C 芯片:计算两位整数的加法(m+n)、计算两位整数的除法(m/n) class C: public point{ public: double displaydiv() { cout<<"m/n="<<m/n; } }; //main #include<iostream> #include"bass.h" using namespace std; int main() { double m, n; cout<<"m= "; cin>>m; cout<<"n= "; cin>>n; cout<<endl; //A cout<<"A芯片"<<endl; A calculateA; calculateA.setm(m); calculateA.setn(n); calculateA.displayadd(); cout<<endl; calculateA.displaysub(); cout<<endl<<endl; //B cout<<"B芯片"<<endl; B calculateB; calculateB.setm(m); calculateB.setn(n); calculateB.displayadd(); cout<<endl; calculateB.displaymul(); cout<<endl<<endl; //C cout<<"C芯片"<<endl; C calculateC; calculateC.setm(m); calculateC.setn(n); calculateC.displayadd(); cout<<endl; calculateC.displaydiv(); return 0; }
第二题
//vehicle.h class vehicle{ public: vehicle(int setmaxs, int setwei) { maxspeed = setmaxs; weight = setwei; } void run(); void stop(); protected: int maxspeed; int weight; }; //bicycle类 新增数据成员高度(height) class bicycle : virtual public vehicle{ public: bicycle(int setmaxs, int setwei, int sethei):vehicle(setmaxs, setwei), height(sethei){ } protected: int height; }; //motorcar类 新增数据成员座位数(seatnum) class motorcar : virtual public vehicle{ public: motorcar(int setmaxs, int setwei, int setsea):vehicle(setmaxs, setwei), seatnum(setsea){ } protected: int seatnum; }; //从 bicycle 和 motorcar 派生出摩托车(motorcycle)类 class motorcycle : virtual public bicycle, virtual public motorcar{ public: motorcycle(int setmaxs, int setwei, int sethei, int setsea): motorcar(setmaxs, setwei, setsea), bicycle(setmaxs, setwei, sethei), vehicle(setmaxs, setwei) {} }; //show.cpp #include<iostream> #include"vehicle.h" using namespace std; void vehicle::run() { cout<<"run"<<endl; } void vehicle::stop() { cout<<"stop"<<endl; } //main #include<iostream> #include"show.cpp" using namespace std; int main() { motorcycle motor(1,1,1,1); motor.run(); motor.stop(); return 0; }
运行后很糟糕的失败了,这种错误我还检查不出来
由多个类派生出来的motorcycle对值的操作不是很懂,在问过舍友后也还是不怎么懂,希望有更简单清晰的告解。
以上是关于实验六的主要内容,如果未能解决你的问题,请参考以下文章