实验6-类的继承和多态

时间:2021-01-22 21:55:06

四.实验结论

1.实验内容1

#include <iostream>
using namespace std;
class base
{
    public:
        base(int a,int b):m(a),n(b){}
     ~base(){}
void plus(){cout<<m+n<<endl;} private: int m,n; }; class chipA:public base { public: chipA(int a,int b):base(a,b){ m=a; n=b; }
     ~chipA(){}
void minus(){cout<<m-n<<endl;} private: int m,n; }; class chipB:public base { public: chipB(int a,int b):base(a,b){ m=a; n=b; }
     ~chipB(){}
void mul(){cout<<m*n<<endl;} private: int m,n; }; class chipC:public base { public: chipC(int a,int b):base(a,b){ m=a; n=b; }
     ~chipC(){}
void div(){cout<<m/n<<endl;} private: int m,n; }; int main() { int a,b; cin>>a>>b; chipA a1(a,b); a1.plus(); a1.minus(); cin>>a>>b; chipB a2(a,b); a2.plus(); a2.mul(); cin>>a>>b; chipC a3(a,b); a3.plus(); a3.div(); return 0; }

实验6-类的继承和多态

2.实验内容2

#include <iostream>
using namespace std;
class vehicle
{
    public:
        vehicle(int a,int b):maxspeed(a),weight(b){
            cout<<"constructing vehicle"<<endl;}
        ~vehicle(){}
        void run(){cout<<"run"<<endl;}
        void stop(){cout<<"stop"<<endl;}
        int maxspeed,weight;
        
};
class bicycle:virtual public vehicle
{
    public:
        bicycle(int a,int b, int c):vehicle(a,b),height(c){
            cout<<"constructing bicycle"<<endl;}
        ~bicycle(){}
        int height;
};
class motorcar:virtual public vehicle
{
    public:
        motorcar(int a,int b,int c):vehicle(a,b),seatnum(c){
            cout<<"constructing motorcar"<<endl;}
        ~motorcar(){}
        int seatnum;
};
class motorcycle:public bicycle,public motorcar
{
    public:
        motorcycle(int a,int b,int c,int d):
        vehicle(a,b),bicycle(a,b,c),motorcar(a,b,d){
            cout<<"constructing motorcycle"<<endl;}
        ~motorcycle(){}
};
        
int main()
{
    motorcycle a(200,10,1,5);
    cout<<"The maxspeed is:"<<a.maxspeed<<endl;
    cout<<"The weight is:"<<a.weight<<endl;
    cout<<"The height is:"<<a.height<<endl;
    cout<<"The seatnum is:"<<a.seatnum<<endl;
    a.run();
    a.stop();
    return 0;
}

实验6-类的继承和多态.

3.实验内容3

Fraction.h

class Fraction
{
    public:
        Fraction(int x,int y);
        Fraction(int x);
        Fraction();
        void show();
        int gcd(int a,int b);
        void simplify();
        Fraction operator+(Fraction &b);
        Fraction operator-(Fraction &b);
        Fraction operator*(Fraction &b);
        Fraction operator/(Fraction &b);
    protected:
        int top;
        int bottom;
};

 

 

Fraction.cpp

//fraction.cpp
#include"Fraction.h"
#include<iostream>
#include<cmath>
using namespace std;
Fraction::Fraction()
{
    top=0;
    bottom=1;
}
Fraction::Fraction(int x,int y)
{
    top=x;
    bottom=y;
}
Fraction::Fraction(int x)
{
    top=x;
    bottom=1;
}
int Fraction::gcd(int a,int b)
{
    if(a%b==0)
        return b;
    else
        gcd(b,a%b);
}
void Fraction::simplify()
{
    if(bottom<0)
    {
        top=-top;
        bottom=-bottom;
    }
    int g=gcd(abs(top),abs(bottom));
    top=top/g;
    bottom=bottom/g;
}
void Fraction::show()
{
    Fraction temp;
    temp.top=top;
    temp.bottom=bottom;
    temp.simplify();
    if(temp.bottom==1)
        cout<<temp.top<<endl;
    else
        cout<<temp.top<<"/"<<temp.bottom<<endl;
}
Fraction Fraction::operator+(Fraction &b)
{
    Fraction temp;
    temp.top=top*b.bottom+b.top*bottom;
    temp.bottom=bottom*b.bottom;
    return temp;
}
Fraction Fraction::operator-(Fraction &b)
{
    Fraction temp;
    temp.top=top*b.bottom-b.top*bottom;
    temp.bottom=bottom*b.bottom;
    return temp;
}
Fraction Fraction::operator*(Fraction &b)
{
    Fraction temp;
    temp.top=top*b.top;
    temp.bottom=bottom*b.bottom;
    return temp;
}
Fraction Fraction::operator/(Fraction &b)
{
    Fraction temp;
    temp.top=top*b.bottom;
    temp.bottom=bottom*b.top;
    return temp;
}

 

iFraction.h

#include"Fraction.h"
class iFraction:public Fraction
{
    private:
        int left;
    public:
        iFraction(int x=0,int y=1,int z=0):Fraction(x,y),left(z){}
        void print();
        friend iFraction convertF(iFraction &d); 
};

 

iFraction.cpp

#include"iFraction.h"
#include<iostream>
#include<iomanip>
using namespace std;
void iFraction::print()
{
    if(top==0)
        cout<<setw(4)<<left<<endl;
    else if(left==0)
    {
        cout<<setw(4)<<top<<endl;
        cout<<setw(4)<<'-'<<endl;
        cout<<setw(4)<<bottom<<endl;
    }
    else
    {
        cout<<setw(4)<<top<<endl;
        cout<<setw(3)<<left<<'-'<<endl;
        cout<<setw(4)<<bottom<<endl;
    }
}
iFraction convertF(iFraction &d)
{
    d.simplify(); 
    d.left+=d.top/d.bottom;
    d.top%=d.bottom;
    return d;
}

 

main.cpp

#include <iostream>
#include "iFraction.h"
using namespace std;
int main()
{
    Fraction a(-6,8);
    Fraction b(5);
    Fraction c;
    cout<<"a=";
    a.show();
    cout<<"b=";
    b.show();
    cout<<"c=";
    c.show();
    c=a+b;
    cout<<"c=a+b=";c.show();
    c=a-b;
    cout<<"c=a-b=";c.show();
    c=a*b;
    cout<<"c=a*b=";c.show();
    c=a/b;
    cout<<"c=a/b=";c.show();
    iFraction d(8,6,4);
    cout<<"d="<<endl;
    d.print();
    d=convertF(d);
    cout<<"将d规范化处理后d="<<endl;
    d.print(); 
    return 0;
}

 

实验6-类的继承和多态

 

五.实验总结与体会

看见我实验内容3那一层一层的插入代码了吗?

我只想说,辣鸡博客园。

 

实验6-类的继承和多态