public,protected,private

#include <iostream>
using namespace std;
class Animal
{
public:
    void eat()
    {
        cout<<"animal eat"<<endl;
    }
protected:
    void sleep()
    {
        cout<<"animal sleep"<<endl;
    }
private:
    void breathe()
    {
        cout<<"animal beathe"<<endl;
    }
};
class Fish:public Animal
{
public:
    void test()
    {
        sleep();//可以调用
    }
    void test1()
    {
        eat();//可以调用
    }
    void test3()
    {
        breathe();//不可调用
    }
};

class Dog:private Animal
{
public:
    void testPublic()
    {
        sleep();
    }
protected:
    void testProtected()
    {
        sleep();
    }
};
class Cat:protected Animal
{

};


void main()
{
    Animal an;
    an.eat();
    an.sleep();//不可调用
    an.breathe();//不可调用
    Fish fh;
    fh.test();//可以调用
    fh.test1();//可以调用

    Dog dg;
    dg.eat();//无法访问
    dg.testPublic();//可以访问
    Cat cat;
    cat.eat();//无法访问

}

 

private                  自己可以访问

protected                自己和派生类可以访问

public                   谁都能访问

 

posted @ 2013-04-18 19:47  shinecox  阅读(204)  评论(0编辑  收藏  举报