C++如何防止类被继承(二)

#include <iostream>
using namespace std;

class Animal
{
private:
 int  m_nTotal;
 Animal(){m_nTotal = 100;};
public:
   ~Animal(){};
public :
 static Animal Instance()
 {
  Animal instance =  Animal();
  return instance;
 }
 void ShowType();
 void Eat();
 void Walk();
 void Sleep();
};

void Animal::ShowType()
{
 cout << "Animal" << endl;
}


void Animal::Eat()
{
}

void Animal::Walk()
{
}

void Animal::Sleep()
{
}
//class Cow:public Animal//无法继承Animal
//
//};


void main()
{
 Animal animal = Animal::Instance();
 //int total = animal.m_nTotal;
 //cout << total << endl;//输出100

 animal.ShowType();
// Cow cow = Cow();
 int tem;
 cin >> tem;

}

posted @ 2013-06-12 19:37  Predator  阅读(113)  评论(0编辑  收藏  举报