继承组合类

#include<iostream>

#include<string>

using namespace std;

//继承与组合类的构造实例复杂版本

//Motor类存储和处理发动机相关信息

class Motor

{

private:

    string m_factory;//工厂

    int m_cylinders;//缸

    string m_capacity;//能力

public:

    //无参构造函数

    Motor() {

        cout << "Motor's Constructor" << endl;

    }

    Motor(string fa, int cy, string ca)

    {

        //此处对属性进行初始化,并输出一句话作为标记(结合输入样例)

        this->m_factory = fa;

        this->m_capacity = ca;

        this->m_cylinders = cy;

        cout << "Motor's Constructor with parameters" << endl;

        //-----------------------------------------------------

    }

    ~Motor() {

        //输出一句话作为标记(结合输入样例)

        cout << "Motor's Destructor" << endl;

        //--------------------------------------------------

    }

    string get_factory() {

        return m_factory;

    }

    void set_Motor() {

        cin >> m_factory >> m_cylinders >> m_capacity;

    }

    void show_Motor() {

        cout << "Factory:" << m_factory << endl;

        cout << "Cylinders:" << m_cylinders << endl;

        cout << "capacity:" << m_capacity << endl;

    }

};

//Vehicle为交通工具基类

class Vehicle

{

protected:

    string m_brand; //品牌

    double m_weight;//重量

    double m_height;//高度

    int m_passengerCapacity;//乘客数

public:

    Vehicle() {

        cout << "Vehicle's Constructor" << endl;

    }

    Vehicle(string br, double we, double he, int pa) {

        //此处对属性进行初始化,并输出一句话作为标记(结合输入样例)

        m_brand = br;

        m_weight = we;

        m_height = he;

        m_passengerCapacity = pa;

        cout << "Vehicle's Constructor with parameters" << endl;

        //-----------------------------------------------------

    }

    ~Vehicle() {

        //输出一句话作为标记(结合输入样例)

        cout << "Vehicle's Destructor" << endl;

        //--------------------------------------------------

    }

    void setvehicle(string br, double we, double he, int pa) {

        m_brand = br;

        m_weight = we;

        m_height = he;

        m_passengerCapacity = pa;

    }

    void showvehicle() {

        cout << "品牌: " << m_brand << endl;

        cout << "重量: " << m_weight << endl;

        cout << "高度: " << m_height << endl;

        cout << "乘客数: " << m_passengerCapacity << endl;

    }

};

//Car类公有继承了Vehicle类,并拥有自己的属性

class Car :virtual public Vehicle /*此处补全继承语法,使得后续的子类可以直接继承祖先类的成员*/

{

private:

    int m_gears;

public:

    //无参构造函数

    Car() {

        cout << "Car's Constructor" << endl;

    }

    //使用初始化列表方式,构造基类成员和自己的成员

    Car(string br, double we, double he, int pa, int mg) :

    Vehicle(br, we,he,pa)

        /*此处补全代码,以初始化列表方式构造所有成员*/

    {

        m_gears = mg;

        cout << "Car's Constructor with parameter" << endl;

    }

    Car(int mg) {

        //此处对属性进行初始化,并输出一句话作为标记(结合输入样例)

        cout<< "Car's Constructor with 1 parameter" << endl;


 

        //-----------------------------------------------------

        m_gears = mg;

    }

    //析构函数

    ~Car() {

        cout << "Car's Destructor" << endl;

    }

    void setcar(string br, double we, double he, int pa, int mg) {

        //此处一条语句调用基类成员函数完成基类属性的赋值。

       

        setvehicle(br, we, he, pa);

        //-----------------------------------------------------------

        m_gears = mg;

    }

    void showcar() {

        //此处一条语句调用基类成员函数完成基类属性的显示。

        showvehicle();

        //------------------------------------------------------------

        cout << "档位数:" << m_gears << endl;

    }

    int getgears() {

        return this->m_gears;

    }

};

//Plane类,继承Vehicle类,并拥有自己的属性

class Plane :virtual public Vehicle /*此处补全继承语法,使得后续的子类可以直接继承祖先类的成员*/{

private:

    int m_swings;//机翼的数量

public:

    //无参构造函数

    Plane() {

        cout << "Plane's Constructor" << endl;

    }

    //利用初始化列表,构造基类成员和自身成员

    Plane(string br, double we, double he, int pa, int msw) : Vehicle(br,we,he,pa),m_swings(msw)/*此处补全代码,以初始化列表方式构造所有成员*/

    {

        cout << "Plane's Constructor with parameter" << endl;

    }

    Plane(int msw) {

        //此处对属性进行初始化,并输出一句话作为标记(结合输入样例)

        cout << "plane's Constructor with 1 parameter" << endl;

        //-----------------------------------------------------

        m_swings = msw;

    }

    //析构函数

    ~Plane() {

        cout << "Plane's Destructor" << endl;

    }

    void setplane(string br, double we, double he, int pa, int msw) {

        //此处一条语句调用基类成员函数完成基类属性的赋值。

        setvehicle(br, we, he, pa);

        //-----------------------------------------------------------

        m_swings = msw;

    }

    int getswings() {

        return this->m_swings;

    }

    void showplane() {

        //此处一条语句调用基类成员函数完成基类属性的显示。

        showvehicle();

        cout << "机翼数:" << m_swings << endl;

        //------------------------------------------------------------

       

    }

};

//FlyingCar多重继承Car类和Plane类。

class FlyingCar :public Plane,public Car /*此处补全继承语法,使得后续的子类可以直接继承祖先类的成员*/{

private:

    string m_movingmodel;//移动方式

    Motor m;//组合类Motor对象m。

public:

    //无参构造函数

    FlyingCar() {

        cout << "FlyingCar's Constructor" << endl;

    }

    //利用初始化列表,构造祖先类,父类,组合类和自身的属性。

    FlyingCar(string br, double we, double he, int pa, int mg, int msw, string fa, int cy, string ca, string mm)

        :Vehicle(br, we, he, pa),Plane(msw),Car(mg),m(fa, cy, ca)/*此处补全代码,以初始化列表方式构造所有成员*/

    {

       

        m_movingmodel = mm;

    }

    void showflyingcar() {

        showvehicle();

        cout << "档位数:" << this->getgears() << endl;

        cout << "机翼数:" << this->getswings() << endl;

        //此处补全发动机参数显示的代码

        m.show_Motor();

        //---------------------------------------------

        cout << "行驶状态:" << this->m_movingmodel << endl;

    }

};

int main(int argc, char** argv) {

    string br;

    double we, he;

    int pa, mg, msw;

    string fa;

    int cy;

    string ca, mm;

    //输入初始化参数

    cin >> br >> we >> he >> pa >> mg >> msw >> fa >> cy >> ca >> mm;

    //构建一个flyingcar对象fc

    FlyingCar fc(br, we, he, pa, mg, msw, fa, cy, ca, mm);

    //显示参数

    fc.showflyingcar();

}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值