工具vs2008
四个文件:
基类文件: Config.h Config.cpp
子类文件:Profile.h Profile.cpp
主文件:boost.cpp
Config.h:
#include "stdafx.h"
#include <iostream>
using namespace std;
class AbstractProfile
{
private:
string str;
public:
virtual bool onSectionBegin(const char* sectionName) = 0;
public:
bool loadFromFile(const char* fileName);
};
Config.cpp:
#include "stdafx.h"
#include "Config.h"
bool AbstractProfile::loadFromFile(const char* fileName)
{
cout<<"AbstractProfile::loadFromFile fileName ="<<fileName<<endl;
if(onSectionBegin(fileName))
{
cout<<"回调成功!!"<<endl;
}
return true;
}
Profile.h:
#include "stdafx.h"
#include "Config.h"
#include <iostream>
using namespace std;
class Profile:AbstractProfile
{
public:
bool onSectionBegin(const char* sectionName);
bool loadFromFile(const char* fileName);
};
Profile.cpp
#include "stdafx.h"
#include "Profile.h"
bool Profile::onSectionBegin(const char* sectionName)
{
cout<<"Profile::onSectionBegin"<<endl;
return true;
}
bool Profile::loadFromFile(const char* fileName)
{
cout<<"Profile::loadFromFile"<<endl;
return AbstractProfile::loadFromFile(fileName);
}
boost.cpp
#include "Profile.h"
int _tmain(int argc, _TCHAR* argv[])
{
Profile pro;
pro.loadFromFile("11.txt");
system("pause");
return 0;
}
虚函数调用自己没有实现的函数,在子类中返回父类实现的函数。
运行结果:
Profile::loadFromFile
AbstractProfile::loadFromfile
Profile::onsectionBeigin