Summary

 < Day Day Up > 



A class type can exhibit the behavior of another class type through the mechanism of inheritance. The class whose behavior is inherited is called the base class; the class inheriting the behavior is called the derived class. A public base class function can be called via a derived class object. A base class pointer can be assigned the addresses of a derived class object, however, since the pointer is of a base class type, only the public functions declared in the base class can be called via the pointer unless the base class function is declared to be virtual and there is an overriding function in a derived class.

The access specifiers public, protected, and private are used to control vertical access between base and derived classes.

When a derived class must initialize base class attributes it must make an explicit base class constructor call in its initializer list. The derived class constructor can use its parameters as arguments to its base class constructor. If a default constructor exists for both the base and derived class then some or all of the constructor arguments can be omitted when objects are instantiated.

A function appearing in a derived class having the same function signature as a base class function merely hides the base class function. Virtual functions support dynamic binding and object-oriented programming. If a base class function is declared to be virtual and a derived class declares a function with the same function signature as the virtual base class function, the derived class function overrides the base class function and will be called via a base class pointer if the base class pointer points to a derived class object.

A class can inherit from more than one class. This is called multiple inheritance. Be careful when using multiple inheritance, especially in complex inheritance hierarchies where one class may inherit from two different classes who share a common base class. Use virtual inheritance to control the number of base class instances created in a multiple inheritance hierarchy.

Dynamic polymorphic behavior is achieved through the use of virtual base class functions being overridden in derived classes. Base class pointers can be assigned the addresses of derived class objects and overriding functions called via the base class pointer.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

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