第一题:题目要求:

代码:

//chip.h
#include<iostream>
#pragma once
using namespace std;
class chip
{
    public:
        chip(int a,int b):m(a),n(b)    {};
        int geta(){return m;}
        int getb(){return n;}
        int show();
    private:
        int m;
        int n;
};
class chipa:public chip
{
    public:
        chipa(int a ,int b):chip(a,b){};
        int showa();
        
    
};
class chipb:public chip
{
    public:
        chipb(int a,int b):chip(a,b){};
        int showb();
};
class chipc:public chip
{
    public:
        chipc(int a,int b):chip(a,b){};
        int showc();
};
//chip.cpp
#include<iostream>
#include"chip.h"
using namespace std;
int chip::show()
{
    return m+n;
}
int chipa::showa()
{
    cout<<"chipa:"<<endl;
    cout<<"m+n="<<chip::show()<<endl;
    cout<<"m-n=";
    return geta()-getb();
}
int chipb::showb()
{
    cout<<"chipb:"<<endl;
    cout<<"m+n="<<chip::show()<<endl;
    cout<<"m*n=";
    return geta()*getb();
}
int chipc::showc()
{
    cout<<"chipc:"<<endl;
    cout<<"m+n="<<chip::show()<<endl;
    cout<<"m/n=";
    return geta()/getb();
}
#include <iostream>
#include"chip.h"
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    chipa A(1, 2);
    chipb B(1, 2);
    chipc C(1, 2);
    cout << "m=1  n=2" << endl;
    cout << A.showa() << endl;
    cout << B.showb() << endl;
    cout << C.showc() << endl;
    return 0;
}

运行图:

第二题:

代码:

//vehicle
#pragma once
#include<iostream>
using namespace std;

class vehicle {            
public:                                                         //外部接口
    vehicle(int a, int b) :maxspeed(a), weight(b) {             //构造函数 
    cout<<"maxspeed="<<maxspeed<<endl<<"weight="<<weight<<endl;};
    void run() {//run()函数 
        cout << "run" << endl;
    }
    void stop() { //stop()函数 
        cout << "stop" << endl;
    }
    ~vehicle() {                                                //析构函数
        cout << "Destructing vehicle" << endl;
    }
private:
    int maxspeed;//int变量maxspeed和weight 
    int weight;
};
//定义派生类自行车
class bicycle :virtual public vehicle {        
public:                                                         //新增外部接口
    bicycle(int a, int b, int c) :vehicle(a, b), height(c) {
        cout << "height=" << height << endl;
    }
    ~bicycle() {                                                 //析构函数
        cout << "Destructing bicycle" << endl;
    }
private:
    int height;//新增int 变量height 
};
//定义派生类汽车
class motorcar :virtual public vehicle {    
public:                                                          //新增外部接口
    motorcar(int a, int b, int d) :vehicle(a, b), seatnum(d) {
        cout << "seatnum=" << seatnum << endl;
    }
    ~motorcar() {                                                //析构函数
        cout << "Destructing motorcar" << endl;
    }
private:
    int seatnum;//新增int变量seatnum 
};
//定义派生类摩托车
class motocycle :public bicycle, public motorcar {
public:                                                          //新增外部接口
    motocycle(int a, int b, int c, int d) :vehicle(a, b), bicycle(a, b, c), motorcar(a, b, d) {};
    ~motocycle() {                                               //析构函数
        cout << "Destructing motocycle" << endl;
    }
};
#include <iostream>
#include"vehicle.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    motocycle M(1, 2, 1,2 );  //定义motocycle类对象M
    M.run();
    M.stop();
    return 0;
}

运行图:

第三题:

代码:

//Fraction.h
#pragma once
class Fraction {
public:
    Fraction();            //构造函数
    Fraction(int t, int b);//函数重载
    Fraction(int t);       //函数重载
    void show();           //输出
    Fraction operator+ (const Fraction &f0) const {    //重载+
        Fraction f;
        f.top = top * f0.bottom + f0.top*bottom;
        f.bottom = bottom * f0.bottom;
        return f;
    };

    Fraction operator- (const Fraction &f0) const {    //重载-
        Fraction f;
        f.top = top * f0.bottom - f0.top*bottom;
        f.bottom = bottom * f0.bottom;
        return f;
    };

    Fraction operator* (const Fraction &f0) const {     //重载*
        Fraction f;
        f.top = top * f0.top;
        f.bottom = bottom * f0.bottom;
        return f;
    };

    Fraction operator/ (const Fraction &f0) const {     //重载/
        Fraction f;
        f.top = top * f0.bottom;
        f.bottom = bottom * f0.top;
        return f;
    };

    int gett() { return top; }
    int getb() { return bottom; }
private:
    int top;      //分子
    int bottom;   //分母
};
//Fraction.cpp
#include "Fraction.h"
#include <iostream>
#include<stdlib.h>
using namespace std;
int measure(int x, int y)   //构造函数,找出分子与分母的最大公约数
{
    int z = y;
    while (x%y != 0)
    {
        z = x % y;
        x = y;
        y = z;
    }
    return z;
}

Fraction::Fraction() :top(0), bottom(1) {                     //不提供初始值的函数实现
}

Fraction::Fraction(int t, int b) : top(t), bottom(b) {        //提供两个初始值的函数实现
}

Fraction::Fraction(int t) : top(t), bottom(1) {               //提供一个初始值的函数实现
}

void Fraction::show() {                                       //输出
    int t,b,z;
    t = top;
    b = bottom;
    while (b!=0) {
        if (t == 0) {
            cout << "0" << endl;
            break;
        }
        z = measure(t, b);
        t /= z;
        b /= z;

        if (b == 1) {
            cout << t << endl;
            break;
        }
        if (b == -1) {
            cout << -t << endl;
            break;
        }
        if (t > 0 && b> 0) {
            cout << t << "/" << b << endl;
            break;
        }
        if (t < 0 && b < 0) {
            cout << abs(t) << "/" << abs(b) << endl;
            break;
        }
        if (t > 0 && b< 0) {
            cout << -abs(t) << "/" << abs(b) << endl;
            break;
        }
        if (t < 0 && b> 0) {
            cout << -abs(t) << "/" << abs(b) << endl;
            break;
        }
    }

}
//iFraction.h
#pragma once
#include<iostream>
#include"Fraction.h"
using namespace std;

class iFraction :public Fraction {
public:
    iFraction() :Fraction() {};
    iFraction(int t, int b) :Fraction(t, b) {};
    iFraction(int t) :Fraction(t) {};
    void convertF();
};
//iFraction.cpp
#include "iFraction.h"
#include <iostream>
using namespace std;
int measure(int x, int y); //构造函数,找出分子与分母的最大公约数

void iFraction::convertF() {              //输出
    int t, b, z, A;
    t = gett();
    b = getb();
    while (b != 0) {
        A = t / b;
        if (A != 0) {
            cout << A;
            t %= b;
            z = measure(t, b);
            t /= z;
            b /= z;
            if (t == 0) {
                cout << endl;
                break;
            }
            else {
                cout << "(" << abs(t) << "/" << abs(b) << ")" << endl;
                break;
            }
        }
            if (A == 0) {
                Fraction::show();
                break;
            }
        
    }
}
#include <iostream>
#include"raction.h"
#include"iFraction.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    Fraction f1;              //不提供初始值
    Fraction f2(-2, 3);        //提供两个初始值
    Fraction f3(3);           //提供一个初始值
    Fraction f4,f5,f6,f7;
    f4 = f2 + f3;
    f5 = f2 - f3;
    f6 = f2 * f3;
    f7 = f2 / f3;
    f1.show();
    f2.show();
    f3.show();
    f4.show();
    f5.show();
    f6.show();
    f7.show();
    iFraction F1(6, 3);
    iFraction F2(2, 4);
    iFraction F3(10, 6);
    iFraction F4(-10, 6);
    F1.convertF();
    F2.convertF();
    F3.convertF();
    F4.convertF();
    return 0;
}

运行图:

优质内容筛选与推荐>>
1、angularjs学习笔记(3)–i18n使用
2、Codeforces 747C:Servers(模拟)
3、移动开发webapp开发常用meta设置(手机浏览器全屏模式 html5)------转
4、Zookeeper--Zookeeper是什么
5、Daily Scrum 10.29


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn