【正文】
). In the last two issues of ObjectiveViewPoint we have looked at how C++ supports the objectoriented paradigm. 。), class object selector operator (`.39。) operators work for plex objects just as they do for ints and floats. Operators are defined in much the same was as normal C++ functions and can be members or nonmembers of a class. Operators take one or two arguments and are called unary and binary operators accordingly. In C++, a member operator function is defined like an ordinary member function, but the name is prefixed with the keyword operator. C++ places a number of restrictions on operator overloading. Only the predefined set of C++ operators may be overloaded. It is illegal to define a new operator and then overload it. You cannot turn a unary operator into a binary operator or vice versa. Also, the following operators cannot be overloaded: scope operator (`::39。 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 under the same name. When there are several declarations of the same function, the piler determines which function should be called by examining the return type and argument signature of the function call. 13 When we define new data types, it is often useful to define standard operations that are found in similar types. For example, a plex type also has addition and subtraction defined for it. We can use operator overloading so that the addition (`+39。t really hide the implementation details from the user. C++ does not provide a way to pletely exclude all of the details of the underlying implementation, since the private part of the class must be included with the class definition it is useful to relax the access to variables within a class, particularly under inheritance. Often derived classes need easy access to the private members of their parent classes. C++ defines the keyword protected for this purpose. Protected members can be a