一。实验结论:
1.(1)类的声明:
#include<iostream> using namespace std; class A { public: A(int m,int n); void plus(); void minus(); protected: int a; int b; }; class B:public A { public: B(int m,int n):A(m,n){} void multi(); }; class C:public A { public: C(int m,int n):A(m,n){} void divi(); };
(2)类的实现:
#include"math.h" #include<iostream> using namespace std; A::A(int m,int n) { a=m; b=n; } void A::plus() { int p; p=a+b; cout<<"The result is:"<<p<<endl; } void A::minus() { int p; p=a-b; cout<<"The result is:"<<p<<endl; } void B::multi() { int p; p=a*b; cout<<"The result is:"<<p<<endl; } void C::divi() { double p; if(b==0) cout<<"Error!"<<endl; else { p=(double)a/(double)b; cout<<"The result is:"<<p<<endl; } }
(3)主函数:
#include<iostream> #include"math.h" using namespace std; int main() { int m,n; char p; while(1) { cout<<"Please input an equation, input 000 to exit:"; cin>>m>>p>>n; if(m==0&&p=='0'&&n==0) break; else { if(p=='+') { A a(m,n); B b(m,n); C c(m,n); a.plus(); b.plus(); c.plus(); } else if(p=='-') { A a(m,n); a.minus(); } else if(p=='*') { B a(m,n); a.multi(); } else if(p=='/') { C a(m,n); a.divi(); } else { cout<<"Error!"<<endl; continue; } } } return 0; }
(4)运行结果:
2.(1)代码:
#include<iostream> using namespace std; class vehicle { public: vehicle(int m,int w):maxspeed(m),weight(w) { cout<<"Vehicle is constructing..."<<endl; cout<<"The max speed is:"<<maxspeed<<endl; cout<<"The weight is:"<<weight<<endl; } void run() { cout<<"Running..."<<endl; } void stop() { cout<<"Stopped."<<endl; } ~vehicle() { cout<<"Vehicle has been destructed."<<endl; } protected: int maxspeed; int weight; }; class bicycle:virtual public vehicle { public: bicycle(int m,int w,int h):vehicle(m,w),height(h) { cout<<"Bicycle is constructing..."<<endl; cout<<"The height is:"<<height<<endl; } ~bicycle() { cout<<"Bicycle has been destructed."<<endl; } protected: int height; }; class motorcar:virtual public vehicle { public: motorcar(int m,int w,int s):vehicle(m,w),seatnum(s) { cout<<"Motorcar is constructing..."<<endl; cout<<"The seatnum is:"<<seatnum<<endl; } ~motorcar() { cout<<"Motorcar has been destructed."<<endl; } protected: int seatnum; }; class motorbike:public bicycle,public motorcar { public: motorbike(int m,int w,int h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){} ~motorbike() { cout<<"Motorbike has been destructed."<<endl; } }; int main() { int a,b,c,d; cout<<"Please input the maxspeed,weight,height,seatnum of the motorbike:"; cin>>a>>b>>c>>d; motorbike M(a,b,c,d); M.run(); M.stop(); return 0; }
(2)运行结果:
3.(1)类的声明:
//#include"Fraction.h" #include<iostream> using namespace std; class iFraction:public Fraction { public: iFraction(int p=0,int q=1,int n=0):Fraction(p,q),number(n){} void show(); private: int itop; int ibottom; int number; };
(2)类的实现:
扫描二维码关注公众号,回复:
1476738 查看本文章
#include"iFraction.h" #include<iostream> using namespace std; void iFraction::show() { if(bottom==0) cout<<"Error!"<<endl; else if(top==0) cout<<number<<endl; else if(top%bottom==0) { number=number+top/bottom; cout<<number<<endl; } else if(top>bottom) { number=number+top/bottom; top=top%bottom; cout<<" "<<top<<endl; cout<<number<<"------"<<endl; cout<<" "<<bottom<<endl; } else if(number==0) { cout<<" "<<top<<endl; cout<<"------"<<endl; cout<<" "<<bottom<<endl; } else { cout<<" "<<top<<endl; cout<<number<<"------"<<endl; cout<<" "<<bottom<<endl; } }
(3)主函数
#include"Fraction.h" #include"iFraction.h" #include<iostream> #include<stdlib.h> using namespace std; int main() { int x,y,z; Fraction a;a.output(); Fraction b(3,6);b.output(); Fraction c(5);c.output(); b.compare(c); cout<<"Please input the top and the bottom:"; cin>>x>>y; Fraction d(x,y);d.opera(x,y);d.output(); cout<<"Please input a number:"; cin>>z; iFraction e(x,y,z);e.opera(x,y);e.show(); return 0; }
(4)运行结果:
二。实验分析与讨论
实验第三题类的声明中,有时取消第一行的注释后编译成功,而有时注释第一行后编译成功,至于什么原因我也没有搞明白,麻烦大佬们在评论时帮我解答一下。。。。
通过这次实验,我初步掌握了最近的新知识继承与多态的大致概念以及使用方法,也发现了自己在所学知识的掌握方面的诸多不足,在以后的时间里,我还需要多加练习。