zoukankan      html  css  js  c++  java
  • C++ 实验六

    #include<iostream>
    using namespace std;
    
    class ABC {
    public:
        void abc(int x, int y){
            m = x;
            n = y;
        }
        void add() { cout << m + n << endl; }
    private:
        int m, n;
    };
    
    class A :public ABC {
    public:
        void a(int x, int y) {
            abc(x,y);
            m = x;
            n = y;
        }
        void minus() { cout << m - n << endl; };
    private:
        int m, n;
    };
    
    class B :public ABC {
    public:
        void b(int x, int y) {
            abc(x, y);
            m = x;
            n = y;
        }
        void mul() { cout << m * n << endl; };
    private:
        int m, n;
    };
    
    class C :public ABC {
    public:
        void c(int x, int y) {
            abc(x, y);
            m = x;
            n = y;
        }
        void divide() { cout << m / n << endl; };
    private:
        int m, n;
    };
    
    int main() {
        int a, b;
        cin >> a >> b;
        cout << "A" << endl;
        A t1;
        t1.a(a, b);
        t1.add();
        t1.minus();
        cout << "B" << endl;
        B t2;
        t2.b(a, b);
        t2.add();
        t2.mul();
        cout << "C" << endl;
        C t3;
        t3.c(a, b);
        t3.add();
        t3.divide();
        system("pause");
        return 0;
    }
    abc

    #include<iostream>
    using namespace std;
    
    class vehicle{
    public:
        vehicle(int x, int y):maxspeed(x),weight(y){};
        void run() { cout << "run" << endl; };
        void stop() { cout << "stop" << endl; };
        virtual ~vehicle() { cout << "Deleting..." << endl; };
    protected:
        int maxspeed, weight;
    };
    
    class bicycle:virtual public vehicle{
    public:
        bicycle(int x, int y, int z):vehicle(x,y), height(z){};
        virtual ~bicycle() { cout << "Deleting..." << endl; };
    protected:
        int height;
    };
    
    class motorcar:virtual public vehicle{
    public:
        motorcar(int x, int y, int s) :vehicle(x, y), seatnum(s) {};
        virtual ~motorcar() { cout << "Deleting..." << endl; };
    protected:
        int seatnum;
    };
    
    class motorcycle:public bicycle ,public motorcar {
    public:
        motorcycle(int x, int y, int z, int s) :vehicle(x, y), bicycle(x,y,z),motorcar(x,y,s){};
        virtual ~motorcycle() { cout << "Deleting..." << endl; };
    };
    
    int main() {
        cout<<"vehicle:"<<endl;
        vehicle a(200,20);
        a.run();
        a.stop();
        cout << "bicycle:" << endl;
        bicycle b(200,20,1);
        b.run();
        b.stop();
        cout << "motorcar:" << endl;
        motorcar c(200,20,8);
        c.run();
        c.stop();
        cout << "motorcycle:" << endl;
        motorcycle d(200,20,1,8);
        d.run();
        d.stop();
        return 0;    
    }
    vehicle

    #ifndef _FRACTION_H_
    #define _FRACTION_H_
    #include<iostream>
    using namespace std;
    
    class Fraction {
    public:
        Fraction(int t, int b);
        Fraction(int t);
        Fraction() {
            top = 0;
            bottom = 1;
        }
        void show() { cout << top << "/" << bottom << endl; };
        Fraction operator+(const Fraction &b)const;
        Fraction operator-(const Fraction &b)const;
        Fraction operator*(const Fraction &b)const;
        Fraction operator/(const Fraction &b)const;
        void com(Fraction x);
    
    private:
        int top;
        int bottom;
    };
    
    #endif 
    Fraction.h
    #include "Fraction.h"
    #include <iostream>
    using namespace std;
    
    
    Fraction::Fraction(int t, int b) :top(t), bottom(b) {}
    
    Fraction::Fraction(int t) : top(t), bottom(1) {}
    
    Fraction Fraction::operator+(const Fraction &b)const{
        return Fraction(top * b.bottom + b.top*bottom, bottom * b.bottom);
    }
    
    Fraction Fraction::operator-(const Fraction &b)const{
        return Fraction(top * b.bottom - b.top*bottom, bottom * b.bottom);
    }
    
    Fraction Fraction::operator*(const Fraction &b)const{
        return Fraction(top * b.bottom, bottom * b.bottom);
    }
    
    Fraction Fraction::operator/(const Fraction &b)const{
        return Fraction(top * b.bottom, bottom * b.bottom);
    
    }
    
    void Fraction::com(Fraction x) {
        if (top*x.bottom > bottom*x.top)
            cout << top << "/" << bottom << ">" << x.top << "/" << x.bottom << endl;
        else cout << top << "/" << bottom << "<" << x.top << "/" << x.bottom << endl;
    }
    Fraction.cpp
    #include <iostream>
    #include "Fraction.h"
    using namespace std;
    
    int main() {
        Fraction a(1,2),b(3, 4),c;
        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();
        system("pause");
        return 0;
    }
    main.cpp

    符号重载over

  • 相关阅读:
    网络流24题之圆桌问题
    BZOJ 4276: [ONTAK2015]Bajtman i Okrągły Robin
    网络流24题航空路线问题
    BZOJ1038 瞭望塔
    BZOJ4029 HEOI2015定价
    BZOJ1226 SDOI2009学校食堂
    网络流24题之魔术球问题
    网络流24题之最小路径覆盖问题
    【BZOJ1098】[POI2007]办公楼biu
    BZOJ3065 带插入区间K小值
  • 原文地址:https://www.cnblogs.com/nuo26/p/9151986.html
Copyright © 2011-2022 走看看