标签:div clu cycle inf private 技术分享 com namespace .sh
1.xinpian.h
#include<iostream> using namespace std; class xinpian{ public: xinpian(float M,float N); void jia(); private: int m,n; }; class xinpianA:public xinpian{ public: xinpianA(float M,float N); void jian(); private: int m,n; }; class xinpianB:public xinpian{ public: xinpianB(float M,float N); void chen(); private: int m,n; }; class xinpianC:public xinpian{ public: xinpianC(float M,float N); void chu(); private: int m,n; };
xinpian.cpp
#include<iostream> #include"xinpian.h" using namespace std; xinpian::xinpian(int m0,int n0):m(m0),n(n0){ }; void xinpian::jia(){ cout<<m+n<<endl; }; xinpianA::xinpianA(int m0,int n0):xinpian(m0,n0),m(m0),n(n0){ }; void xinpianA::jian(){ cout<<m-n<<endl; }; xinpianB::xinpianB(int m0,int n0):xinpian(m0,n0),m(m0),n(n0){ }; void xinpianB::chen(){ cout<<m*n<<endl; }; xinpianC::xinpianC(int m0,int n0):xinpian(m0,n0),m(m0),n(n0){ }; void xinpianC::chu(){ cout<<m/n<<endl; }
main.cpp
#include"xinpian.h" #include<iostream> using namespace std; int main() { xinpianA A(5,2); A.jia(); A.jian(); xinpianB B(6,3); B.jia(); B.chen(); xinpianC C(7,4); C.jia(); C.chu(); return 0; }
2.vehicle.h
#include<iostream> using namespace std; class vehicle{ public: vehicle(int m,int n); void run(); void stop(); ~vehicle(){} private: int maxspeed,weight; }; class bicycle:virtual public vehicle{ public: bicycle(int m,int n,float s); ~bicycle(){} private: float height; }; class motorcar:virtual public vehicle{ public: motorcar(int m,int n,int t); ~motorcar(){} private: int seatnum; }; class motorcycle:public bicycle,public motorcar{ public: motorcycle(int m,int n,float s,int t); ~motorcycle(){} };
vehicle.cpp
#include"vehicle.h" #include<iostream> using namespace std; vehicle::vehicle(int m,int n):maxspeed(m),weight(n){ cout<<"maxspeed"<<maxspeed<<endl; cout<<"weight"<<weight<<endl; } void vehicle::run(){ cout<<"run"<<endl; } void vehicle::stop(){ cout<<"stop"<<endl; } bicycle::bicycle(int m,int n,float s):vehicle(m,n),height(s){ cout<<"height"<<height<<endl; } motorcar::motorcar(int m,int n,int t):vehicle(m,n),seatnum(t){ cout<<"seatnum"<<seatnum<<endl; } motorcycle::motorcycle(int m,int n,float s,int t):vehicle(m,n),bicycle(m,n,s),motorcar(m,n,t){}
#include <iostream> #include"vehicle.h" using namespace std; int main() { motorcycle A(1,2,3,4); A.run(); A.stop(); return 0; }
3,Fraction //fraction.h #include<iostream> using namespace std; class Fraction{ public: Fraction(); Fraction(int a ,int b); Fraction(int a); Fraction operator +(const Fraction &c2)const; Fraction operator -(const Fraction &c2)const; Fraction operator *(const Fraction &c2)const; Fraction operator /(const Fraction &c2)const; friend ostream & operator<<(ostream &out, const Fraction &c); void get(); private: int top; int bottom; }; class iFraction:public Fraction{ public: iFraction(int x,int y); void show(); int top1; int bottom1; friend void convertF(const iFraction &c); };
fraction.cpp
#include"fraction.h" #include<iostream> using namespace std; Fraction::Fraction() :top(0), bottom(1) { } Fraction::Fraction(int a) : top(1), bottom(a) { } Fraction::Fraction(int a, int b) : top(a), bottom(b) { } Fraction Fraction::operator +(const Fraction &c2)const{ return Fraction(top*c2.bottom+bottom*c2.top,bottom*c2.bottom); } Fraction Fraction::operator -(const Fraction &c2)const{ return Fraction(top*c2.bottom-bottom*c2.top,bottom*c2.bottom); } Fraction Fraction::operator *(const Fraction &c2)const{ return Fraction(top*c2.top,bottom*c2.bottom); } Fraction Fraction::operator /(const Fraction &c2)const{ return Fraction(top*c2.bottom,bottom*c2.top); } ostream & operator<<(ostream &out, const Fraction &c){ if(c.top==0) out<<"0"; else out<<c.top<<"/"<<c.bottom; return out; } void Fraction::get(){ int x,y; cout<<"get the top and bottom"<<endl; cin>>x>>y; top=x; bottom=y; } iFraction::iFraction(int x,int y):Fraction(x,y),top1(x),bottom1(y){ } void iFraction::show(){ cout<<top1<<"/"<<bottom1<<endl; } void convertF(const iFraction &c){ if(c.top1==c.bottom1) cout<<"1"<<endl; if(c.top1>c.bottom1) { int m,n; m=c.top1/c.bottom1; n=c.top1%c.bottom1; cout<<"("<<m<<")"<<n<<"/"<<c.bottom1<<endl; } }
main.cpp
#include"fraction.h" #include<iostream> using namespace std; int main() { Fraction a; Fraction b(5); Fraction c(2,3); iFraction d(4,5); iFraction e(7,3); cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl; d.get(); cout<<d<<endl; d.show(); c=a+b; cout<<c<<endl; c=a-b; cout<<c<<endl; c=b*d; cout<<c<<endl; c=b/d; cout<<c<<endl; convertF(e); return 0; }
标签:div clu cycle inf private 技术分享 com namespace .sh
原文地址:https://www.cnblogs.com/wudigongben/p/9152926.html