C++ 虚函数 纯虚函数 抽象类 代码示例
原创
©著作权归作者所有:来自51CTO博客作者DT陶喆的原创作品,请联系作者获取转载授权,否则将追究法律责任
其中抽象类指的是在类的定义中出现了纯虚函数,导致无法实例化,。
代码:头文件
#include "stdafx.h"
using namespace std;
class Person{
public:
Person();
~Person();
virtual void work() = 0;
};
//Coder 继承Person
class Coder : public Person
{
public:
Coder();
~Coder();
virtual void work();
};
实现CPP文件
// AbstractDemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "PersonDemo.h"
using namespace std;
Person::Person()
{
cout <<"测试创建" << endl;
}
Person::~Person()
{
cout << "End" << endl;
}
//void Person::work()
//{
//cout << "I am working" << endl;
//}
Coder::Coder()
{
cout << "Coder Created." << endl;
}
Coder::~Coder()
{
cout << "Clear";
}
void Coder::work()
{
cout << "996"<< endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Coder *mm = new Coder();
mm->work();
int d ;
cin >> d;
return 0;
}