实验6-类的继承和多态

四.实验结论

1.实验内容1

#include 
using namespace std;
class base
{
    public:
        base(int a,int b):m(a),n(b){}
     ~base(){}
void plus(){cout<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<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<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<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-类的继承和多态_第1张图片

2.实验内容2

#include 
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:"<endl;
    cout<<"The weight is:"<endl;
    cout<<"The height is:"<endl;
    cout<<"The seatnum is:"<endl;
    a.run();
    a.stop();
    return 0;
}

实验6-类的继承和多态_第2张图片.

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

 

main.cpp

#include 
#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张图片

 

五.实验总结与体会

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

我只想说,辣鸡博客园。

 

实验6-类的继承和多态_第4张图片

 

 

 

 

转载于:https://www.cnblogs.com/MINT510845604/p/9108634.html

你可能感兴趣的:(实验6-类的继承和多态)