インターフェースクラスを表現したいときは抽象クラスを使います。
C++には多重継承がありますが、CLIには多重継承がありません。
複数のインターフェースを実装することはできます。
| C++ | CLI | |
|---|---|---|
インターフェース![]() | - | ○ |
複数インターフェースの実装![]() | - | ○ |
抽象クラス![]() | ○ | ○ |
継承![]() | ○ | ○ |
多重継承![]() | ○ | - |
インターフェース
[CLI]
interface class Interface1 {
void Method1();
};
interface class Interface2 {
void Method2();
};
ref class Implement : Interface1, Interface2 {
public:
virtual void Method1() {}
virtual void Method2() {}
};
void func1(Interface1^ server)
{
server->Method1();
}
void func2(Interface2^ server)
{
server->Method2();
}
int main()
{
Implement^ implement = gcnew Implement();
func1(implement);
func2(implement);
return 0;
}
抽象クラス
[C++]
class Abstract {
public:
virtual void Method1() = 0;
void Method2() {}
};
class Concrete : public Abstract {
public:
virtual void Method1() {}
};
void func(Abstract* obj)
{
obj->Method1();
obj->Method2();
}
int main()
{
func(new Concrete());
return 0;
}
[CLI]
ref class Abstract abstract {
public:
virtual void Method1() abstract;
void Method2() {}
};
ref class Concrete : Abstract {
public:
virtual void Method1() override {}
};
void func(Abstract^ obj)
{
obj->Method1();
obj->Method2();
}
int main()
{
func(gcnew Concrete());
return 0;
}
継承
[C++]
class Base1 {
public:
void Method1() {}
};
class Base2 {
public:
void Method2() {}
};
class Derived : public Base1, public Base2 {
public:
void Method3() {}
};
int main()
{
Derived* derived = new Derived();
derived->Method1();
derived->Method2();
derived->Method3();
return 0;
}
[CLI]
ref class Base {
public:
void Method1() {}
};
ref class Derived : Base {
public:
void Method2() {}
};
int main()
{
Derived^ derived = gcnew Derived();
derived->Method1();
derived->Method2();
return 0;
}




