Last Updated:

The Keyword _interface in C++

The keyword _interface (interface) is not technically related to the manageability extension, because it can be used in both managed and unmanaged code. However, it is often used when generating managed code, so it is worth stopping to consider it.

Interfaces are used as generic base types for classes that implement some common conventions (contracts). These contracts are used to align the implementation of the core program and the client program by defining a common polymorphic set of methods. An interface can be considered the extreme form of an abstract class, since the purposes of their existence are similar, but interfaces are the least concrete variety of it. It so happens that programmers working with C++ use the term "interface" to refer to any class that contains only pure virtual methods.

Therefore, the new keyword _interface (interface) only makes this agreement explicit.

A class defined using the _interface (interface) keyword can contain only publicly available pure virtual methods. In particular, none of the class's methods must be implemented, the class cannot contain static or non-static data elements, constructors, destructors, static methods, and cannot overload operators. An interface can be a descendant of any number of base interfaces, but not a descendant of any abstract or non-abstract class.

Note that although an interface cannot contain data elements, it can contain properties (accessed by get/set methods). The properties will be described below. As with abstract classes, you cannot create an instance of the interface, so they are used as polymorphic base classes.

Only the public specifier can be used in the interface description; however, it is not necessary, since public is accepted as the default access specifier. Based on the fact that the task of the interface is to define a base contract for derived classes, it is easy to conclude that it is pointless to describe an interface with a keyword _sealed (finite).

There are some additional requirements for managed interfaces (i.e. defined with the keyword _g c (garbage collector)). They cannot derive from unmanaged interfaces. However, they can be direct descendants of an arbitrary number of managed interfaces. The following snippet presents an example of a typical use of the keyword _interface (interface):

//InterfaceExample.cpp
fusing <mscorlib.dll>
using namespace System;
// use namespace System;
_interface Somelnterface // interface
{
public:
virtual void method() = 0; // pure virtual explicit
void Method2(); // pure virtual implied
};
class DerivedClass: public SomelInterface
{
public:
void Methodl() // implemented here
{
Console::WriteLine("Method1");
}
void Method2() // implemented here
{
Console::WriteLine("Method2");
}
};
void main(void)
{
//Somelnterface *psi = new Somelnterface; // error
SomeInterface *psi = new DerivedClass; // pointer
psi › Method1();
psi › Method2();
SomeInterface &si = *new DerivedClass; // link
si.Method1();
si.Method2();
}

The program will print:

Method1
Method2
Method1
Method2