Language Features That Support Object-Oriented Programming

 < Day Day Up > 



Table 16-1 lists and describes the C++ language features employed to write object-oriented programs. All of the language features listed are used together to achieve dynamic polymorphic behavior in support of object-oriented programming.

Table 16-1: Language Features That Support Object-Oriented Programming

Language Feature

Description

base class

A class that provides some form of generalized behavior to its subclasses. A base class can declare one or more virtual functions that can be overridden in subclasses. Base classes can exist to declare a common interface only. Such classes will contain pure virtual functions which make them an abstract base class.

A base class must have a virtual destructor to enable the proper destruction on objects in the inheritance hierarchy.

abstract base class

A class containing one or more pure virtual functions that serves as a base class. There can be no instances of abstract base class objects, however, abstract base class type pointers can be assigned the address of derived (concrete) class objects. Example:

class AbsClass {
   public:
     virtual ~AbsClass();
     virtual void function_a() = 0;
     virtual void function_b() = 0;
};

base class pointer

A pointer declared to be a particular base class type. The pointer can hold addresses of non- abstract base class objects or derived class objects. Virtual function calls are made via base class pointers to derived class objects.

virtual function

A function declared virtual in a base class can be overridden in a derived class and accessed via a base class pointer.

pure virtual function

A function declared virtual which is assigned the value of zero. The zero assignment indicates that function implementations must be provided by a derived class. For an example of a pure virtual function see abstract base class description above.

inheritance

The act of extending the functionality of a base class through the declaration of a subclass. Virtual base class functions can be overridden in the subclass if required. A pure virtual function must be overridden in a subclass eventually. Where this happens exactly depends on your design. A base class and its set of related subclasses constitute an inheritance hierarchy. There is no limit to the extent of an inheritance hierarchy other than that dictated by good design.

C++ supports three types of inheritance: public, protected, and private. Inheritance is controlled via the access specifiers public, protected, and private.

derived class (subclass)

A class that inherits the functionality of one or more classes. The derived class provides specialized behavior, extending the generalized behavior of the base class or classes. If a base class contains a virtual function, that function can be overridden in a derived class if the design so dictates. If a base class contains a pure virtual function a derived class can override it and provide an implementation, or it can choose to not override it, in which case the derived class will itself become an abstract base class. (An overriding implementation must eventually appear in a derived class somewhere down the inheritance hierarchy)

virtual destructor

A base class constructor must be declared virtual for the proper destruction of objects in an inheritance hierarchy.

encapsulation

Implementation details should be kept private to a class. An object's desired behavior is made accessible via a set of public interface functions. Accessibility to class functions and attributes is controlled through the use of the access specifiers public, protected, and private.

polymorphic class

A class that declares or inherits a virtual function.

compile-time type checking

The C++ compiler will ensure a particular class supports a particular interface function call. Any attempt to call a non-supported function on an object will result in a compile-time error.



 < 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