Chapter 8 Quick Reference


To

Do this

Define an abstract base class.

Use the __abstract keyword in the class definition. For example:

__gc __abstract class MyBase { ... };

Define a derived class.

In the derived class definition, use a colon, followed by public, followed by the name of the base class. For example:

__gc class MyDerived : public MyBase { ... };

Construct derived objects.

In the derived-class constructor, use a member initialization list to call the base-class constructor. For example:

MyDerived::MyDerived(int bdata, int ddata) : MyBase(bdata), derivedData(ddata) { ... }

Enable derived classes to access members in the base class while denying access to unrelated classes.

Declare the members as protected in the base class. For example:

__gc __abstract class MyBase { protected: int dataVisibleToDerivedClass; ... };

Define overridable member functions in the base class.

Declare the member functions as virtual in the base class. For example:

__gc __abstract class MyBase { protected: virtual void myOverridableFunction(); ... };

Specify base-class member functions that must be overridden by derived classes.

Declare the member functions as virtual in the base class. After the closing parenthesis, append = 0. For example:

__gc __abstract class MyBase { protected: virtual void myMustBeOverridden() = 0; ... };

Prevent a class from being derived from.

Use the __sealed keyword in the class definition. For example:

__gc __sealed class MySealedClass { ... };

Define an interface.

Use the __interface keyword. For example:

__gc __interface IMyInterface { void function1(int n); int function2(double d); };

Implement an interface.

Use the same syntax as for inheritance. Implement all the required functions in your class. For example:

__gc class MyImplementingClass : public IMyInterface { public: void function1(int n); int function2(double d); // Other members, as needed ... }; 




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net