实验六

Posted haruna

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验六相关的知识,希望对你有一定的参考价值。

 

 

#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(){}

 

技术分享图片

 

以上是关于实验六的主要内容,如果未能解决你的问题,请参考以下文章

实验六 类的封装

实验六

c++实验六

实验六-类的封装

实验六

操作系统实验六 文件管理