从0学习C++ (八) 继承

#include <iostream>;
using namespace std;


/*
	类的继承
*/
class Animal
{
public:
	Animal()
	{
		cout << "Animal construct" << endl;
	}

	~Animal()
	{
		cout << "Animal deconstruct" << endl;
	}

	void eat()
	{
		cout << "Animal eat" << endl;
	}
protected :
	void sleep()
	{
		cout << "Animal sleep" << endl;
	}
private :
	void breathe()
	{
		cout << "Animal breathe" << endl;
	}
};

class Fish : public Animal
{
public :
	Fish()
	{
		cout << "Fish construct" << endl;
	}

	~Fish()
	{
		cout << "Fish deconstruct" << endl;
	}

	void test()
	{
		eat();
		sleep();
		//breathe();   不可调用
	}
};

  
  
  
int main(){ 

  Animal animal;
  animal.eat();
  //animal.sleep(); 无法调用
  //animal.breathe(); 无法调用

 
  Fish fish;
  fish.test();
  //fish.eat();  无法调用


  

	return 0 ;
}  

猜你喜欢

转载自android-zhang.iteye.com/blog/2000392