SW개발/C++
C++ 인터페이스 클래스 (Interface Class)
Caniro
2021. 3. 21. 23:47
반응형
인터페이스 클래스 (Interface Class)
순수 가상 함수를 이용하여 만드는 클래스
특별한 기능을 하지는 않고 자식 클래스들을 편하게 다루기 위해 사용한다.
예제
#include <iostream>
class IErrorLog
{
public:
virtual ~IErrorLog() {}
virtual bool reportError(const char* errorMessage) = 0;
};
class FileErrorLog : public IErrorLog
{
public:
bool reportError(const char* errorMessage) override
{
std::cout << "Writing error to a file\n";
return true;
}
};
class ConsoleErrorLog : public IErrorLog
{
public:
bool reportError(const char* errorMessage) override
{
std::cout << "Printing error to a console\n";
return true;
}
};
void doSomething(IErrorLog& log)
{
log.reportError("Runtime error!!");
}
int main()
{
FileErrorLog file_log;
ConsoleErrorLog console_log;
doSomething(file_log);
doSomething(console_log);
}
/* stdout
Writing error to a file
Printing error to a console
*/
반응형