【正文】
nce a design has been conceived, a programming language can be chosen for implementation. By factoring out the inheritance relationships from the object hierarchies discovered during design, one can even implement the system in a traditional, non objectoriented language. However, using an objectoriented language, such as C++, makes it easier to realize the design into an implementation because the inherent relationships among objects can be directly supported in the language. Languages such as C++ are considered hybrid languages because they are multiparadigm languages. C++ is an object oriented extension of C and can be used as a procedural language or as an objectoriented language. In this issue, we continue our tour of the objectoriented features of C++. The ObjectOriented Features of C++ INHERITANCE in C++. One of the major strengths of any objectoriented programming language is the ability to build other classes from existing classes, thereby reusing code. Inheritance allows existing types to be extended to an associated collection of subtypes. 11 Recall that one of the key actions of objectoriented design is to identify realworld entities and the relationships among them. When a software system is designed, a variety of objects arise, which may be related in one way or another. Some classes may not be related at all. Many times it makes sense to anize the object classes into an inheritance hierarchy. Organizing a set of classes into a class hierarchy requires that we understand the relationships among the classes in detail. Not all class relationships dictate that inheritance be used. C++ provides three forms of inheritance: public, private, and protected. These different forms are used for different relation ships between objects. To illustrate these different types of inheritance, let39。 the base class is an implementation detail. Under private inheritance, the public and protected parts of the base class bee part of the private part of the derived class. Users of the derived class cannot access any of the base class interface. However, member functions of the derived class are free to use the public and private parts of the base class. When used this way, users cannot write code that depends on the inheritance. This is a powerful way of preserving your ability to change the implementation to a different base class. One other form of inheritance, which is very rarely used is protected inheritance. Protected inheritance is also used to implement HASA relationships. When protected inheritance is used, the public and protected parts of the base class bee protected in the derived class. So, you may wish to use protected inheritance when the inheritance is part of the interface to derived classes, but not part of the interface to the users. A protected base class is almost like a private base class, except the interface is known to derived classes. 12 It is best to use position where possible. In cases where you must override functions in a base class then by all means use inheritance. Only use public inheritance if your derived class is indeed a specialization of the base class, otherwise, private inheritance should be used. Needlessly using inheritance makes your system harder to understand. In summary, a class specifies two interfaces: one to the users of the class (the public interface) and another to implementers of derived classes (the union of public and protected parts). Inheritance works almost identically. When the inheritance is public, the public interface of the base class bees part of the public interface to users of the derived class. When the inheritance is protected, the public and protected parts of the base class are accessible to the member functions (the implementation) of the derived classes, but not to general users of the derived classes. Finally, when inheritance is private, the public and protected parts of the base class are only accessible to the implementer of the class, but not to users or derived classes. POLYMORPHISM in C++. Polymorphism is the last of the three fundamental primitives of objectoriented programming and the most important. Together with inheritance, polymorphism brings the most power, in terms of runtime flexibility, to objectoriented programming. Polymorphism, which means many forms, provides a generic software interface so that a collection of different types of objects may be manipulated uniformly. C++ provides three different types of polymorphism: virtual functions, function name overloading, and operator overloading. The virtual function mechanism can only be invoked through the use of a base class reference or pointer. Recall that a base class pointer can point to an object of the base type or an object of any type that is derived from the base class. Virtual functions are also used to implement the logic gate hierarchy .The class gate is an abstract base class at the root of the inheritance hierarchy. A class is considered abstract when some of its virtual member functions do not have an implementation. These functions are assigned to be zero in the class classes must provide implementations for them. Another form of polymorphism found in C++ is function overloading. A function is said to be overloaded when it is declared more than once in a program. Overloading allows a set of functions that perform a similar operation to be collected u