animalclassmultipleconstructors.cpp

来自「C++ sample code, for the book: C++ blac」· C++ 代码 · 共 46 行

CPP
46
字号
#include <iostream>
#include <string>
using namespace std;

class animal 
{
    string name;
public:
    void eat() {cout << "Eating..." << endl;};
    void sleep() {cout << "Sleeping..." << endl;};
    void breathe() {cout << "Breathing..." << endl;};
    animal(string s) {name = s;}
};

class anaconda : public virtual animal
{
    int weight;
public:
    void walk() {cout << "Slithering..." << endl;}
    anaconda(int w, string s) : animal(s){weight = w;}
};

class zebra : public virtual animal
{
    int weight;
public:
    void walk() {cout << "Sauntering..." << endl;}
    zebra(int w, string s) : animal(s){weight = w;}
};

class zoo : public anaconda, public zebra
{
public:
    zoo(int weight, string name) : animal(name), anaconda(weight, name), zebra(weight,name){}
};

int main()
{
    zoo z(1000, "Cuddles");

    z.eat();
    z.breathe();
    z.sleep();

    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?