animalclassaccess.cpp

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

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

class animal 
{
private:
    void eat();
protected:
    void run();
public:
    void sleep();
    void breathe();
};

class elephant : public animal 
{
public:
    void run();
    void trumpet();
    void stampede();
};

void animal::eat(){cout << "Eating..." << endl;}
void animal::run(){cout << "Running..." << endl;}
void animal::sleep(){cout << "Sleeping..." << endl;}
void animal::breathe(){cout << "Breathing..." << endl;}

void elephant::run(){animal::run();}
void elephant::trumpet(){cout << "Trumpeting..." << endl;}
void elephant::stampede(){cout << "Stampeding..." << endl;}

int main()
{
    elephant jumbo;

    jumbo.run();
    jumbo.breathe();
    jumbo.trumpet();
    jumbo.stampede();

    return 0;
}

⌨️ 快捷键说明

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