Q1:芯片问题
思路:basic做父类,ABC做子类
#include<iostream> using namespace std; class basic{ public: basic(int,int); int mm(){return m;} int nn(){return n;}//提供了m,n的外部接口 void add(); private: int m,n; }; //basic类为芯片ABC的最基本功能,即m+n basic::basic(int mx,int nx) {m=mx;n=nx;} //构造函数的实现 void basic::add() {cout<<"m+n="<<mm()+nn()<<endl;} //basic已经完成,开始芯片A class A:public basic{ public: A(int a,int b):basic(a,b){}; void sub(); }; void A::sub() {cout<<"m-n="<<mm()-nn()<<endl;} class B:public basic{ public: B(int a,int b):basic(a,b){}; void mul(); }; void B::mul() {cout<<"m*n="<<mm()*nn()<<endl;} class C:public basic{ public: C(int a,int b):basic(a,b){}; void div(); }; void C::div() {cout<<"m/n="<<(float)mm()/nn()<<endl;} //芯片ABC都已经设计好 int main() { basic ba(2,3); ba.add(); A a(3,4); a.sub(); a.add(); B b(2,1); b.add(); b.mul(); C c(7,8); c.div(); c.add(); return 0; }
运行截图:
Q2:车类
思路:合理的应用虚基类
#include<iostream> using namespace std; class vehicle { public: void run(); void stop(); vehicle(int a,int b) { maxspeed=a;weight=b; } ~vehicle(){} private: int maxspeed; int weight; }; void vehicle::run(){cout<<"vehicle is running"<<endl;} void vehicle::stop(){cout<<"vehicle has stopped"<<endl;} class bicycle:virtual public vehicle { public: void run(); void stop();//同名覆盖 bicycle(int a,int b,int c):vehicle(a,b) { height=c; } ~bicycle(){} private: int height; }; void bicycle::run(){cout<<"bicycle is running"<<endl;} void bicycle::stop(){cout<<"bicycle has stopped"<<endl;} class motorcar:virtual public vehicle { public: void run(); void stop(); motorcar(int a,int b,int c):vehicle(a,b) { seatnum=c; } ~motorcar(){} private: int seatnum; }; void motorcar::run(){cout<<"motorcar is running"<<endl;} void motorcar::stop(){cout<<"motorcar has stopped"<<endl;} class motorcycle:public motorcar,public bicycle { public: void run(); void stop(); motorcycle(int a,int b,int c,int d):vehicle(a,b),bicycle(a,b,c),motorcar(a,b,d) {} ~motorcycle(){} }; void motorcycle::run(){cout<<"motorcycle is running"<<endl;} void motorcycle::stop(){cout<<"motorcycle has stopped"<<endl;} int main() { vehicle v(1,2); v.run(); v.stop(); bicycle b(1,2,3); b.run(); b.stop(); motorcar m(1,2,3); m.run(); m.stop(); motorcycle mo(1,2,3,4); mo.run(); mo.stop(); return 0; }
运行截图:
Q3:fraction类
思路:友元函数,我这道题有问题...我的基础不扎实...这我得承认...
#include<iostream> using namespace std; class fraction { public: int tt(){return top;} int bb(){return bottom;} fraction operator+(fraction& f2); fraction operator-(fraction& f2); fraction operator*(fraction& f2); fraction operator/(fraction& f2); fraction(int a,int b):top(a),bottom(b){} friend void convertF(); private: int top,bottom; }; fraction fraction::operator+(fraction& f2) return fraction(top+f2.top,bottom+f2.bottom); fraction fraction::operator-(fraction& f2) return fraction(top-f2.top,bottom-f2.bottom); fraction fraction::operator*(fraction& f2) return fraction(top*f2.top,bottom*f2.bottom); fraction fraction::operator/(fraction& f2) return fraction(top*f2.bottom,bottom*f2.top); void convertF() { int k=top>bottom?top:bottom; for(int i=k;i>=2;i--) { if(top%i==0&&bottom%i==0) { top=top/i; bottom=bottom/i; } break; } cout<<top/bottom<<endl; } //构造完毕,yeah class ifraction:public fraction { public: ifraction(int a,int b,int c):fraction(b,c){num=c;} friend void display(); friend void convertf(); private: int num; }; void ifraction::convertf() { int t=tt(),b=bb(); int m=t%b; t=t-m*b; int k=t>b?t:b; for(int i=k;i>=2;i--) { if(t%i==0&&b%i==0) { t=t/i; b=b/i; } break; } cout<<m<<"+"<<t/b<<endl; } void display() {cout<<num<<"+"<<top/bottom<<endl;} int main() { fraction f(2,4),f1(1,2),f3(0,0); f3=f1+f2; cout<<f3.top<<"/"<<f3.bottom<<endl; f3=f1-f2; cout<<f3.top<<"/"<<f3.bottom<<endl; f3=f1*f2; cout<<f3.top<<"/"<<f3.bottom<<endl; f3=f1/f2; cout<<f3.top<<"/"<<f3.bottom<<endl; return 0; }