zoukankan      html  css  js  c++  java
  • 第六次实验报告

    #include<iostream>
    using namespace std;
    class base1 {
    public:
        base1(int m = 0, int n = 0) :m1(m), n1(n) {};
        void setnumber1(int m0, int n0) {
            m1 = m0;
            n1 = n0;
        };
        int getnumber1() { return m1 + n1; };
    private:
        int m1, n1;
    };
    class base2 {
    public:
        base2(int m = 0, int n = 0) :m2(m), n2(n) {};
        void setnumber2(int m0, int n0) {
            m2 = m0;
            n2 = n0;
        };
        int getnumber2() { return m2 - n2; };
    private:
        int m2, n2;
    };
    class base3 {
    public:
        base3(int m = 0, int n = 0) :m3(m), n3(n) {};
        void setnumber3(int m0, int n0) {
            m3 = m0;
            n3 = n0;
        };
        int getnumber3() { return m3 * n3; };
    private:
        int m3, n3;
    };
    class base4 {
    public:
        base4(int m = 0, int n = 0) :m4(m), n4(n) {};
        void setnumber4(int m0, int n0) {
            m4 = m0;
            n4 = n0;
        };
        double getnumber4() { return (double)m4 / n4; };
    private:
        int m4, n4;
    };
    class A :public base1,public base2 {
    public:
        A(int m=1, int n=1) :base1(m, n), base2(m, n) {};
    };
    class B :public base1,public base3 {
    public:
        B(int m=1, int n=1) :base1(m, n), base3(m, n) {};
    };
    class C :public base1,public base4 {
    public:
        C(int m=1, int n=1) :base1(m, n), base4(m, n) {};
    };
    int main() {
        A a;
        B b;
        C c;
        cout << a.getnumber2() << endl;
        cout << b.getnumber3() <<endl;
        cout << c.getnumber4()  <<endl;
        a.setnumber2(2, 5);
        b.setnumber3(2, 5);
        c.setnumber4(2, 5);
        cout << a.getnumber2() << endl;
        cout << b.getnumber3() << endl;
        cout << c.getnumber4() << endl;
        return 0;
    }

    #include<iostream>
    using namespace std;
    class vehicle {
    public:
        vehicle(int maxspeed0=0,int weight0=0):maxspeed(maxspeed0), weight(weight0){}
        ~vehicle(){}
        void run() {
            cout << "run" << endl;
        }
        void stop() {
            cout << "stop" << endl;
        }
    private:
        int maxspeed, weight;
    };
    class bicycle:virtual public vehicle {
    public:
        bicycle(int maxspeed1 = 0, int weight1 =0, int height1 = 0) :vehicle(maxspeed1, weight1), height(height1){}
        ~bicycle(){};
    private:
        int height;
    };
    class motorcar :virtual public vehicle {
    public:
        motorcar(int maxspeed2 = 0, int weight2 = 0, int seatnum2=0):vehicle(maxspeed2, weight2), seatnum(seatnum2){}
        ~motorcar(){}
    private:
        int seatnum;
    };
    class motorcycle :public bicycle, public motorcar {
    public:
        motorcycle(int maxspeed3 = 0, int weight3 = 0, int height3 = 0,int seatnum3=0):bicycle(maxspeed3,weight3,height3),motorcar(maxspeed3,weight3,seatnum3){}
        ~motorcycle(){}
    };
    int main() {
        motorcycle M(6, 6, 6, 6);
        M.run();
        M.stop();
        return 0;
    }

     fraction.h

    #pragma once
    class Fraction {
    public:
        Fraction();
        Fraction(int top0, int bottom0);
        Fraction(int top0);
        void Compare(Fraction&f0);
        void set_fraction();
        void get_fraction();
        Fraction operator+(const Fraction&f0)const;
        Fraction operator-(const Fraction&f0)const;
        Fraction operator*(const Fraction&f0)const;
        Fraction operator/(const Fraction&f0)const;
        ~Fraction();
    
    private:
        int top;
        int bottom;
    };

    fraction.cpp

    #include"Fraction.h"
    #include<iostream>
    using namespace std;
    Fraction::Fraction() :top(0), bottom(1) {}
    Fraction::Fraction(int top0) : top(top0), bottom(1) {}
    Fraction::Fraction(int top0, int bottom0) : top(top0), bottom(bottom0) {}
    
    Fraction::~Fraction() {}
    void::Fraction::set_fraction() {
        cin >> top >> bottom;
    }
    void::Fraction::get_fraction() {
        cout << top << "/" << bottom << endl;
    }
    Fraction Fraction:: operator+(const Fraction&f0)const {
        return Fraction(f0.top*bottom + top * f0.bottom, f0.bottom*bottom);
    }
    Fraction Fraction:: operator-(const Fraction&f0)const {
        return Fraction(top * f0.bottom - f0.top*bottom, f0.bottom*bottom);
    }
    Fraction Fraction:: operator*(const Fraction&f0)const {
        return Fraction(top * f0.top, f0.bottom*bottom);
    }
    Fraction Fraction:: operator/(const Fraction&f0)const {
        return Fraction(top * f0.bottom, bottom * f0.top);
    }
    void::Fraction::Compare(Fraction&f0) {
        if ((top * f0.bottom - f0.top*bottom)>0)
            cout << top << "/" << bottom << ">" << f0.top << "/" << f0.bottom << endl;
        else if ((top * f0.bottom - f0.top*bottom)<0)
            cout << top << "/" << bottom << "<" << f0.top << "/" << f0.bottom << endl;
        else
            cout << top << "/" << bottom << "=" << f0.top << "/" << f0.bottom << endl;
    }

    ifraction.h

    #pragma once
    #include"fraction.h"
    class iFraction: public Fraction{
    public:
        iFraction(int top, int bottom, int wnum) ;
        void showiFraction() ;
    private:
        int wnum;
    };

    ifraction.cpp

    #include"ifraction.h"
    #include<iostream>
    using namespace std;
    iFraction::iFraction(int top0, int bottom0, int wnum0): Fraction(top0,bottom0),wnum(wnum0) {}
    void iFraction::showiFraction() {
        cout << wnum<<" ";
        get_fraction();
    }

    main.cpp

    #include"fraction.h"
    #include"ifraction.h"
    #include<iostream>
    using namespace std;
    int main() {
        Fraction f(1, 1);
        Fraction f1(2, 2);
        Fraction f3 = f1 + f;
        iFraction f4(1, 2, 3);
        f3.get_fraction();
        f3 = f1 - f;
        f3.get_fraction();
        f3 = f1 * f;
        f3.get_fraction();
        f3 = f / f1;
        f3.get_fraction();
        f4.showiFraction();
        return 0;
    }

     做第一二题没有注意要求分文件,加上这两题不是很复杂,就没有采用分文件。

    在第三题时发现了一些问题,因为fraction.h需要被多次包含,就出现了了重复编译的问题,这时候就需要采用宏或者预编译头的方式来避免重复编译

    还有一个问题我没有解决,本来我在fraction类中是定义了复制构造函数的,但是在重载运算符时,创建了临时无名对象作为返回值时,显示fraction类中没有合适的复制构造函数。后来我把自己定义的复制构造函数删除之后就没有问题了,没有想明白,不知道谁可以为我解答吗?

  • 相关阅读:
    30 字符编码
    Xilinx Vivado的使用详细介绍(3):使用IP核
    Xilinx Vivado的使用详细介绍(1):创建工程、编写代码、行为仿真
    Vivado SDK ,调用math.h函数的时候出现 undefined reference to `xxx' ,解决方案
    Xilinx Vivado的使用详细介绍(4):Zedboard+vivado之流水灯(加SDK)
    xilinx Vivado的使用详细介绍(2):创建工程、添加文件、综合、实现、管脚约束、产生比特流文件、烧写程序、硬件验证
    no.4
    no.5
    关于贪心算法
    关于三角形把平面分成几块的问题
  • 原文地址:https://www.cnblogs.com/miaorui1314/p/9104141.html
Copyright © 2011-2022 走看看