Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
An interface is similar to an abstract class in that it is a binding agreement between the class that derives from the abstract class and the class that calls the methods of that derived class. The key difference is that an interface only contains public, pure virtual methods. As the name suggests, it defines an interface to a class. But defining is all it does, as it does not contain variables or implementations for any methods.
Though classes can only inherit one class, they are able to inherit as many interfaces as needed to define the interface to the class. It is up to the class to implement all interfaces.
Like an abstract class, you can't instantiate an object from an interface. Thus, it can't be used as a parameter or return type. However, just like an abstract class, pointers and references to abstract classes can be used as a parameter or return type.
Traditionally, C++ programmers have defined an interface as a class that contains only pure virtual methods. With Managed C++, it has been formalized with the keyword __interface. Basically, to create an interface, replace the keyword class in the definition and then place in the body of the interface a set of public, pure virtual methods.
Because only public access is allowed within an interface, the default logically for interface access is public. This means there is no need to include the public access modifier, as you would if it were a class.
Obviously, because an interface is only made up of pure virtual methods, the __sealed keyword has no relevance to interfaces and will generate an error.
One additional note about interfaces: Even though they cannot contain method variables, is it is perfectly legal to define properties within an interface. The definition of the properties cannot have an implementation—like other methods in the interface, the properties need to be implemented in the interface's inheriting class.
Listing 3-16 shows how to create a couple of interfaces, one with pure virtual methods only and another with a combination of methods and property definitions. It then shows how to do multiple inheritances in a managed class (one base class and two interfaces).
Listing 3-16: Interfaces in Action
#using <mscorlib.dll> using namespace System; __gc __interface Interface1 { public: virtual void Method1() = 0; virtual void Method2() = 0; }; __gc __interface Interface2 { virtual void Method3() = 0; __property String* get_X(); __property void set_X(String*); }; __gc class Base { public: void MethodBase() { Console::WriteLine(S"MethodBase()"); } }; __gc class DerivedClass : public Base, public Interface1, public Interface2 { String* _x; public: __property String* get_X() { return _x; } __property void set_X(String* x) { _x = x; } void Method1() { Console::Write Line(S"Method1()"); } void Method2() { Console::WriteLine(S"Method2()"); } void Method3() { Console::WriteLine(S"Method3()"); } void Print() { MethodBase(); Method1(); Method2(); Method3(); } }; Int32 main(void) { DerivedClass &dc = *new DerivedClass; dc.X = S"Start'n Up"; Console::WriteLine(dc.X); dc.Print(); return 0; }
Figure 3-16 shows the results of this little program.
Категории