第二题:
#include<iostream> using namespace std; class vehicle{ public: void run(){cout<<"run"<<endl;}; void stop(){cout<<"stop"<<endl;}; vehicle(int x,int y):maxspeed(x),weight(y){ cout<<"maxspeed="<<maxspeed<<",weight="<<weight<<endl; }; friend class bicycle; friend class motorcar; ~vehicle(){}; private: int maxspeed,weight; }; class bicycle:virtual public vehicle{ public: bicycle(int x,int y,int z):vehicle(x,y){ int height; height=z; cout<<"height="<<height<<endl; }; ~bicycle(){}; }; class motorcar:virtual public vehicle{ public: motorcar(int x,int y,int z):vehicle(x,y){ int setnum; setnum=z; cout<<"setnum="<<setnum<<endl; }; ~motorcar(){}; }; #include <iostream> #include"vehicle.h" using namespace std; int main() { int a,b,c,d; cout<<"输入maxspeed,weight:"; cin>>a>>b; cout<<"输入自行车的height:"; cin>>c; bicycle newB(a,b,c); newB.run(); newB.stop(); cout<<"输入摩托车的setnum:"; cin>>d; motorcar newM(a,b,d); newM.run(); newM.stop(); return 0; }转载于:https://www.cnblogs.com/a18851962010/p/9151298.html