实验六
#include <iostream> using namespace std; class A{ public: A(int n,int m); ~A(){} void add() { cout<<n+m<<endl; } void subtraction() { cout<<n-m<<endl; } int getn() { return n; } int getm() { return m; } private: int n; int m; }; A::A(int n,int m):n(n),m(m){} class B:public A{ public: B(int n,int m):A(n,m){} ~B(){} void multiplication() { cout<<getn()*getm()<<endl; } private: int n,m; }; class C:public A{ public: C(int n,int m):A(n,m){} ~C(){} void divition() { cout<<getn()/getm()<<endl; } }; int main(){ A a(3,2); B b(5,4); C c(4,2); a.add(); a.subtraction(); b.add(); b.multiplication(); c.divition(); return 0; };
1 #include <iostream> 2 3 using namespace std; 4 5 class vehicle{ 6 public: 7 void run(){cout<<"maxspeed is"<<maxspeed<<endl;} 8 void stop(){cout<<"weight is"<<weight<<endl;} 9 vehicle(int n,int m):maxspeed(n),weight(m){} 10 int getx(){return maxspeed;} 11 int gety(){return weight;} 12 13 ~vehicle(){} 14 private: 15 int maxspeed; 16 int weight; 17 }; 18 class bicycle:virtual public vehicle{ 19 public: 20 void Height(){cout<<"height is"<<height<<endl;} 21 bicycle(int n,int m,int k):vehicle(n,m),height(k){} 22 ~bicycle(){} 23 int getz(){return height;} 24 void aa(){cout<<getx()<<endl;} 25 void bb(){cout<<gety()<<endl;} 26 private: 27 int height; 28 29 }; 30 class motorcar:virtual public vehicle{ 31 public: 32 void Seatnum(){cout<<"seatnum is"<<seatnum<<endl;} 33 motorcar(int n,int m,int k):vehicle(n,m),seatnum(k){} 34 ~motorcar(){} 35 void cc(){cout<<getx()<<endl;} 36 void dd(){cout<<gety()<<endl;} 37 int geth(){return seatnum;} 38 private: 39 int seatnum; 40 }; 41 class motorcycle:public bicycle,public motorcar{ 42 public: 43 motorcycle(int n,int m,int k,int h):vehicle(n,m),bicycle(n,m,k),motorcar(n,m,k){} 44 ~motorcycle(){} 45 46 }; 47 int main(){ 48 vehicle a(120,1000); 49 bicycle b(40,200,110); 50 motorcar c(200,1500,6); 51 motorcycle d(120,800,110,2); 52 b.aa(); 53 b.bb(); 54 c.cc(); 55 c.dd(); 56 return 0; 57 }
#include <iostream> #include"Fraction.h" #include"iFraction.h" using namespace std; int main(){ Fraction a(3,5),b(2,5); iFraction c(5,3); c.ishow(); a-b; a+b; a*b; a/b; return 0; }
Fraction。h
#ifndef FRACTION_H_INCLUDED #define FRACTION_H_INCLUDED class Fraction{ public: Fraction(){} Fraction(int t0,int b0):top(t0),bottom(b0){} Fraction(int t0):top(t0),bottom(t0){} ~Fraction(){} void show(); void add(Fraction &p1,Fraction &p2); void sub(Fraction &p1,Fraction &p2); void mul(Fraction &p1,Fraction &p2); void div(Fraction &p1,Fraction &p2); void compare(Fraction &p1,Fraction &p2); void operator+(const Fraction &a); void operator-(const Fraction &a); void operator*(const Fraction &a); void operator/(const Fraction &a); int getx() { return top; } int gety() { return bottom; } private: int top; int bottom; }; #endif // FRACTION_H_INCLUDED
Fraction.cpp
#include<iostream> #include"Fraction.h" using namespace std; void Fraction::show() { cout<<top<<'/'<<bottom<<endl; } void Fraction::add(Fraction &a,Fraction &b) { a.top=top*b.bottom+b.top*bottom; a.bottom=bottom*b.bottom; a.show(); } void Fraction::sub(Fraction &a,Fraction &b) { a.top=top*b.bottom-b.top*bottom; a.bottom=bottom*b.bottom; a.show(); } void Fraction::mul(Fraction &a,Fraction &b) { a.top=top*b.top; a.bottom=bottom*b.bottom; a.show(); } void Fraction::div(Fraction &a,Fraction &b) { a.top=top*b.bottom; a.bottom=bottom*b.top; a.show(); } void Fraction::compare(Fraction &b,Fraction &c) { double m,n; m=b.top/b.bottom; n=c.top/c.bottom; if(m<n) { cout <<b.top<<'/'<<b.bottom<<'<';c.show();cout<<endl; } else if(m==n) { cout <<b.top<<'/'<<b.bottom<<'=';c.show();cout<<endl; } else { cout <<b.top<<'/'<<b.bottom<<'>';c.show();cout<<endl; } } void Fraction::operator+(const Fraction &f) { Fraction result; result.top=top*f.bottom+f.top*bottom; result.bottom=bottom*f.bottom; result.show(); } void Fraction::operator-(const Fraction &f) { Fraction result; result.top=top*f.bottom-f.top*bottom; result.bottom=bottom*f.bottom; result.show(); } void Fraction::operator*(const Fraction &f) { Fraction result; result.top=top*f.top; result.bottom=bottom*f.bottom; result.show(); } void Fraction::operator/(const Fraction &f) { Fraction result; result.top=top*f.bottom; result.bottom=bottom*f.top; result.show(); }
iFraction.h
#ifndef IFRACTION_H_INCLUDED #define IFRACTION_H_INCLUDED #include"Fraction.h" class iFraction:public Fraction { public: iFraction(int a,int b):Fraction(a,b){} void ishow(); friend class Fraction; }; #endif // IFRACTION_H_INCLUDED
iFraction.h
#include<iostream> #include"iFraction.h" using namespace std; void iFraction::ishow() { int m,n; if(getx()>gety()) { m=getx()/gety(); n=getx()%gety(); cout<<" "<<n<<endl<<m<<'-'<<endl<<" "<<gety()<<endl; } else cout<<getx()<<'/'<<gety()<<endl; }