抽象类只能作为基类来使用(大多数情况是其他类的基类,但是抽象类本身也有可能是子类),其纯虚函数的实现由派生类给出。如果派生类没有重新定义纯虚函数,而派生类只是继承基类的纯虚函数,则这个派生类仍然还是一个抽象类。如果派生类中给出了基类纯虚函数的实现,则该派生类就不再是抽象类了,它是一个可以建立对象的具体类了
4、纯虚类一般是做为接口使用(这里要好好看设计模式了) [cpp] view plain copy #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; class animal { protected: animal(){} virtual void eat(const string name) =0; }; class dog:public animal { public: vector<string> m_food; void eat(const string name); void push_back(const string name); };
void dog::eat(const string name) { vector<string>::iterator iter=std::find(m_food.begin(),m_food.end(),name); if (m_food.end() !=iter) { cout<<"Dog eat "<<*iter<<endl; } } void dog::push_back(const string name) { if (m_food.end() ==std::find(m_food.begin(),m_food.end(),name)) { m_food.push_back(name); } } int main(void) { dog d; d.push_back("bone"); d.eat("bone"); return 0; }