[c++] 인터페이스 다중 상속의 예
#include <iostream>
// 첫 번째 인터페이스
class Interface1 {
public:
virtual void method1() = 0;
};
// 두 번째 인터페이스
class Interface2 {
public:
virtual void method2() = 0;
};
// 클래스가 두 개의 인터페이스를 다중 상속
class MyClass : public Interface1, public Interface2 {
public:
void method1() override {
std::cout << "Implementation of method1" << std::endl;
}
void method2() override {
std::cout << "Implementation of method2" << std::endl;
}
};
int main() {
MyClass obj;
obj.method1();
obj.method2();
return 0;
}
위의 예제에서 MyClass
는 Interface1
과 Interface2
두 개의 인터페이스에서 상속을 받았습니다. 따라서 MyClass
는 method1
과 method2
를 구현하여야 하며, 각각의 인터페이스에서 요구하는 동작을 제공하여야 합니다.
참고 문헌:
- https://en.cppreference.com/w/cpp/language/interfaces