以下是第六次实验的内容,本次实验的主要目的是让我们进一步掌握继承和派生方面的内容。
第一个实验是编写一个简单的继承派生的程序,让其实现功能,代码及运行结果如下:
#include<iostream> using namespace std; class chip{ public: chip(int A,int B) { m=A;n=B; } int plus() { int C=m+n; return C; } protected: int m,n; }; class chipA:public chip { public: chipA(int A,int B); int minus() { int C=m-n; return C; } }; chipA::chipA(int A,int B):chip(A,B) { } class chipB:public chip { public: chipB(int A,int B); int mult() { int C=m*n; return C; } }; chipB::chipB(int A,int B):chip(A,B) { } class chipC:public chip { public: chipC(int A,int B); int div() { int C=m/n; return C; } }; chipC::chipC(int A,int B):chip(A,B) { } int main() { chipA a(3,6); chipB b(8,4); chipC c(15,5); int i=a.plus(); int j=a.minus(); int k=b.mult(); int l=c.div(); cout<<i<<endl<<j<<endl<<k<<endl<<l<<endl; }
第二个实验与第一个类似,只不过是有间接类和直接类的分别:
#include<iostream> using namespace std; class vehicle{ protected: int maxspeed,weight; public: vehicle(){ cout<<"vehicle constructed"<<endl; } vehicle(int max,int we):maxspeed(max),weight(we) { cout<<"vehicle constructed"<<endl; } ~vehicle() { cout<<"vehicle destructed"<<endl; } void run() { cout<<"run"<<endl; }; void stop() { cout<<"stop"<<endl; }; }; class bicycle:public virtual vehicle{ protected: int height; public: bicycle(int max,int we,int he):vehicle(max,we),height(he){ cout<<"bicycle constrcuted"<<endl; } ~bicycle() { cout<<"bicycle destructed"<<endl; } }; class motorcar:public virtual vehicle{ protected: int seatnum; public: motorcar(int max,int we,int seat):vehicle(max,we),seatnum(seat){ cout<<"motorcar constrcuted"<<endl; } ~motorcar() { cout<<"motorcar destructed"<<endl; } }; class motorcycle:public bicycle,public motorcar { public: motorcycle(int max,int we,int he,int seat):bicycle(max,we,he),motorcar(max,we,seat) { cout<<"motorcycle constructed"<<endl; } ~motorcycle(){ cout<<"motocycle destructed"<<endl; } }; int main(){ motorcycle ironman(1,2,3,4); ironman.run(); ironman.stop(); return 0; }
第三个实验是基于上次的实验,进行功能拓展,对Fraction函数派生,并添加功能:
Fraction.h
class Fraction{ public: Fraction(){ top=0; bottom=1; } Fraction(int i,int j){ top=i; bottom=j; } Fraction(int i) { top=i; bottom=1; } int skt(int i,int j); const Fraction build(); const Fraction show(); const Fraction convertF(); friend Fraction compare(const Fraction&c1,const Fraction&c2); friend Fraction operator+(const Fraction&c1,const Fraction&c2); friend Fraction operator-(const Fraction&c1,const Fraction&c2); friend Fraction operator*(const Fraction&c1,const Fraction&c2); friend Fraction operator/(const Fraction&c1,const Fraction&c2); protected: int top; int bottom; };
Fraction.cpp
#include "Fraction.h" #include<iostream> #include<iomanip> #include<cmath> using namespace std; int Fraction::skt(int i,int j){ if(j==0) return i; else return skt(j,i%j); } const Fraction Fraction::build(){ Fraction f; int n=skt(top,bottom); f.top=top/n; f.bottom=bottom/n; if(f.top<0) { if(f.bottom<0) f.bottom=-f.bottom; } else if(f.top>0){ if(f.bottom<0) { f.top=-f.top; f.bottom=-f.bottom; } } return f; } const Fraction Fraction::show() { cout<<"分数形式是"<<" "; if(top==0) { cout<<0<<'/'<<bottom<<endl; } else{ Fraction t; t=build(); cout<<t.top<<'/'<<t.bottom<<endl; } cout<<"小数形式是"<<" "; if(top==0) { cout<<0<<endl; } else{ Fraction t; t=build(); float p=(float)t.top; float q=(float)t.bottom; cout<<p/q<<endl; } } Fraction compare(const Fraction&c1,const Fraction&c2) { float p=(float)c1.top; float q=(float)c1.bottom; float x=(float)c2.top; float y=(float)c1.bottom; float s=p/q-x/y; if(s>0) cout<<"c1>c2"<<endl; else if(s<0) cout<<"c2>c1"<<endl; else cout<<"c1=c2"<<endl; } Fraction operator+(const Fraction&c1,const Fraction&c2) { Fraction b; b.top=c1.top*c2.bottom+c1.bottom*c2.top; b.bottom=c1.bottom*c2.bottom; return b.show(); } Fraction operator-(const Fraction&c1,const Fraction&c2) { Fraction b; b.top=c1.top*c2.bottom-c1.bottom*c2.top; b.bottom=c1.bottom*c2.bottom; return b.show(); } Fraction operator*(const Fraction&c1,const Fraction&c2) { Fraction b; b.top=c1.top*c2.top; b.bottom=c1.bottom*c2.bottom; } Fraction operator/(const Fraction&c1,const Fraction&c2){ Fraction b; b.top=c1.top*c2.bottom; b.bottom=c1.bottom*c2.top; return b.show(); } const Fraction Fraction::convertF() { Fraction t; t=build(); if (t.top=0) { cout<<0<<endl<<"-"<<bottom<<endl; } else{ int co=0; if(t.top<0) co=1; int n,m,x=abs(t.top),y=t.bottom; while(x>y) { n=x/y; m=x%y; } if(co==1) cout<<setw(4)<<"-"<<x<<endl; else cout<<setw(4)<<m<<endl; cout<<n<<setw(4)<<"-"<<endl; cout<<setw(4)<<y; } }
iFraction.h
#include"Fraction.h" class iFraction:public Fraction{ private: int add; public: iFraction(int i,int j,int k):Fraction(i,j),add(k){ }; iFraction(int i):Fraction(i){ }; void shownew(); };
iFraction,cpp
#include<iostream> #include"iFraction.h" using namespace std; void iFraction::shownew(){ cout<<"this is iFraction "<<add<<endl; }
main.cpp
#include<iostream> #include"iFraction.h" using namespace std; int main() { Fraction a; Fraction b(3,4); Fraction c(5); Fraction d(2,-3); Fraction e(15,21); Fraction f(-2,-6); iFraction g(8,16,2); Fraction h(45,6); a.show(); b.show(); c.show(); d.show(); e.show(); f.show(); compare(b,c); b+c; d+f; g.shownew(); h.convertF(); return 0; }
第四个实验实在是超出能力范围。。。。无能为力了啊QAQ。
以上就是本次实验的全部内容