C++实验六继承生

//chip

#ifndef CHIP_H
#define CHIP_H
#include<iostream>
using namespace std;
class chip{
    public:
        chip(int m,int n):m(m),n(n){} 
        void add(){
        cout<< m <<"+"<<n<<"= "<<m+n<<endl;
        }
    protected:
      int m,n;    
}; 
#endif

//chipA.h
#include"chip.h" class chipA:public chip{ public : chipA(int m,int n):chip(m,n){} void subtract() { cout<<m<<"-"<<n<<"= "<<m-n<<endl; }

  chipB.h

#include"chip.h"
class chipB:public chip{
    public:
        chipB(int m,int n):chip(m,n){}
         void multiply()
       {
        cout<<m<<"*"<<n<<"= "<<m*n<<endl;
        }
    
       
    
};

chipC.h

#include"chip.h"
class chipC:public chip{
    public:
        chipC(int m,int n):chip(m,n){}
         void divide()
       {
           double a=m;
           double b=n;
        cout<<m<<"/"<<n<<"= "<< a/b<<endl;
        }
        
        
    
};

main.cpp

#include <iostream>
#include"chip.h"
#include"chipA.h"
#include"chipB.h"
#include"chipC.h"
using namespace std;
int main()
{
    cout<<"enter two integers"<<endl;
    int m,n;
    cin>>m>>n;
    chip init(m,n); 
    cout<<"the function of A"<<endl;
    chipA a(m,n);
    a.subtract();
    a.add();
    cout<<"the function of B"<<endl;
    chipB b(m,n);
    b.multiply();
    b.add();
    cout<<"the function of C"<<endl;
    chipC c(m,n);
    c.divide();
    c.add();

return 0;
    
}

vehicle.h

#include<iostream>
#ifndef VEHICLE_H
#define VEHICLE_H
using namespace std;
class vehicle{
    public:
        vehicle(int w,int ms):weight(w),maxspeed(ms){
                cout<<"constructing vehicle...."<<endl;
        }
        void run();
        void stop();
        ~vehicle(){cout<<"Destructing vehicle";}
    protected:
       int maxspeed;
       int weight;    
};
void vehicle:: run()
{
    cout<<"++++++run++++++"<<endl;
}
void vehicle::stop()
{
    cout<<" ___stop__"<<endl;
}
#endif

bicycle.h

#include"vehicle.h"
#ifndef BICYCLE_H
#define BICYCLE_H
class bicycle:virtual public vehicle{
    public:
        bicycle(int w,int ms,int h):vehicle(weight,maxspeed),height(h){
            cout<<"constructing bicycle..."<<endl;
        }
        ~bicycle(){cout<<"Destructing bicycle"<<endl;}
    protected:
        int height;
};
#endif

motorcar.h

#include"vehicle.h"
#ifndef MOTORCAR_H
#define MOTORCAR_H

class motorcar:virtual public vehicle{
    public:
        motorcar(int w,int ms,int sn):vehicle(weight,maxspeed),seatnum(sn){
            cout<<"constructing motorcar..."<<endl;
        }
        ~motorcar(){cout<<"Destructing motorcar"<<endl;}
    protected:
        int seatnum;
};
#endif

motorcycle.h

#include <iostream>
#include"vehicle.h"
#include"bicycle.h"
#include"motorcar.h"
#ifndef MOTORCYCLE_H
#define MOTORCYCLE_H
class motorcycle:public bicycle,public motorcar
{
    public:
        motorcycle(int w,int ms,int h ,int sn):vehicle(w,ms),bicycle(w,ms,h),motorcar(w,ms,sn){
            cout<<"constructing motorcycle...."<<endl;
        }
        void display(){
            cout<<"hight:  "<<height<<endl;
            cout<<"maxspeed:  "<<maxspeed<<endl;
            cout<<"height:  "<<height<<endl;
            cout<<"seatnum  :"<<seatnum<<endl;
        }
        ~motorcycle(){
            cout<<"destructing motorcycle..."<<endl;
        }
};
#endif
#include <iostream>
#include"vehicle.h"
#include"bicycle.h"
#include"motorcar.h"
#include"motorcycle.h" 
using namespace std;
int main()
{
    motorcycle car(100,200,1.5,5);
    car.run();
    car.stop();
    car.display();
    return 0;
}

Fraction.h

#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
#include<iostream>
using namespace std; 
class Fraction{
    public:
        Fraction(int t=0,int b=1):top(t),bottom(b){};
        void output()
        {
            cout<<top<<"/"<<bottom<<endl;
        }
        void input(int t,int B)
        {
            top=t;
            bottom=B;
            
        }
//        void add(const Fraction &f);
//        void subtract(const Fraction &f);
//        void multiply(const Fraction &f);
//        void divide(const Fraction &f);
        void compare (const Fraction &f);
        Fraction operator+(const Fraction &f1) const; 
        Fraction operator-(const Fraction &f1) const; 
        Fraction operator*(const Fraction &f1) const; 
        Fraction operator/(const Fraction &f1) const; 
        protected:
        int top;
        int bottom; 
};
#endif

iFraction.h

#ifndef IFRACTION_H_INCLUDED
#define IFRACTION_H_INCLUDED
#include"Fraction.h"
class iFraction:public Fraction
{
    public:
        iFraction(int t,int b):Fraction(t,b)
        {
            cout<<" consructing iFraction ....."<<endl;
        }
        void display()
        {
            cout<<top<<"/"<<bottom<<endl;
        }
    
};
#endif

Fraction.cpp

#include"Fraction.h"
#include <iostream>
using namespace std;
Fraction Fraction::operator+(const Fraction &f1) const
{
    return Fraction(bottom*f1.bottom,top*f1.bottom+f1.top*bottom) ;
}
Fraction Fraction::operator-(const Fraction &f1) const
{
    return Fraction(bottom*f1.bottom,top*f1.bottom-f1.top*bottom) ;
}
    Fraction Fraction::operator*(const Fraction &f1) const
{
    return Fraction(bottom*f1.bottom,top*f1.top) ;
}
    Fraction Fraction::operator/(const Fraction &f1) const
{
    return Fraction(bottom*f1.top,top*f1.bottom) ;
}
void Fraction::compare (const Fraction &f1)
{
    double x;
    double y;
    x=double(top) /double (bottom) ;
    y=double (f1.top)/double (f1.bottom);
    if(x>y)
    cout<<top<<"/"<<bottom<<">"<<f1.top<<"/"<<f1.bottom;
          
          else if(x<y) 
          
          cout<<top<<"/"<<bottom<<"<"<<f1.top<<"/"<<f1.bottom<<endl;
          
        else
         cout<<top<<"/"<<bottom<<"="<<f1.top<<"/"<<f1.bottom<<endl;
    } 

main

#include <iostream>
#include"iFraction.h"
#include"Fraction.h"
using namespace std;
int main()
{
    
        int t,y;
    cin>>t>>y;
    Fraction a;
    Fraction b(3,4);
    Fraction c(5);
    Fraction d;
    Fraction f1,f2,f3,f4;
    cout<<"input a fraction named d ="<<endl;
    d.input(t,y);
    d.output();
    cout<<"a=";
    a.output();
    cout<<"b=";
    b.output();
    cout<<"c=";
    c.output();
    cout<<"b+c=";
    f1=b+c;
    f1.output();
//    b.add(c); b.output();
    cout<<"b-c=";
   f2=b-c;
   f2.output();
    cout<<"b*c="; 
    f3=b*c;
    f3.output();
//    b.multiply(c); b.output();
    cout<<"b/c="; b.output();
    f4=b/c;
    f4.output();
//    b.divide(c); b.output();
    cout<<"compare b and c"<<endl;
    b.compare(c);
    iFraction m(3,4) ;
    m.display();
    
    
    
    
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/rohahaablog/p/9125687.html