码迷,mamicode.com
首页 > 其他好文 > 详细

实验六

时间:2018-06-08 00:55:33      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:image   style   private   技术分享   iostream   prot   out   car   pac   

 

 

#include<iostream>
using namespace std;
class base1{
    public:
        base1(double,double);
        void plus(double a,double b){
            cout<<a+b;
        }
    protected:
        double a,b;
};
class chip1:public base1{
    public:
        chip1();
        chip1(double,double);
        void minuse(double a,double b){
            cout<<a-b;
        }
};
class chip2:public base1{
    public:
        chip2();
        chip2(double,double);
        void multiply(double a,double b){
            cout<<a*b;
        }
};
class chip3:public base1{
    public:
        chip3();
        chip3(double,double);
        void divide(double a,double b){
            cout<<a/b;
        }
};
int main(){
    chip1 Achip1;
    chip2 Achip2;
    chip3 Achip3;
    Achip1.plus(2,3);
    Achip1.minuse(2,3);
    Achip2.multiply(2,3);
    Achip3.divide(2,3);
}

 main

#include <iostream>
#include"vehicle.h"
using namespace std;
int main()
{
    motorcycle mycar(200,4,2,4);
    mycar.run();
    mycar.stop();
    return 0;
}

 vehicle.h

using namespace std;
class vehicle
{
public:
    vehicle(int ,int );
    ~vehicle();
    void run();
    void stop();
private:
    int maxspeed;
    int weight;
};
class bicycle:virtual public vehicle
{
public:
    bicycle(int ,int ,int );
    ~bicycle();
private:
    int height;
};
class motorcar:virtual public vehicle
{
public:
    motorcar(int ,int ,int );
    ~motorcar();
private:
    int seatnum;
};
class motorcycle:public bicycle,public motorcar
{
public:
    motorcycle(int ,int ,int ,int );
    motorcycle();
    ~motorcycle();
};

 vehicle.cpp

#include<iostream>
#include"vehicle.h"
using namespace std;
vehicle::vehicle(int a,int b):maxspeed(a),weight(b)
{
    cout<<"最大速度:"<<maxspeed<<"km/h"<<endl;
    cout<<"自重:"<<weight<<"T"<<endl;
}
vehicle::~vehicle(){}
void vehicle::run(){cout<<"run"<<endl;}
void vehicle::stop(){cout<<"stop"<<endl;}
bicycle::bicycle(int a,int b,int c):vehicle(a,b),height(c)
{
    cout<<"高:"<<height<<"m"<<endl;
}
bicycle::~bicycle(){}
motorcar::motorcar(int a,int b,int c):vehicle(a,b),seatnum(c)
{
    cout<<"座位数:"<<seatnum<<""<<endl;
}
motorcar::~motorcar(){}
motorcycle::motorcycle(int a,int b,int c,int d):vehicle(a,b),bicycle(a,b,c),motorcar(a,b,d){}
motorcycle::motorcycle():vehicle(0,0),bicycle(0,0,0),motorcar(0,0,0){}
motorcycle::~motorcycle(){}

 

技术分享图片

 

实验六

标签:image   style   private   技术分享   iostream   prot   out   car   pac   

原文地址:https://www.cnblogs.com/haruna/p/9153134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!