Calculating Array Dimensions

Chapter 16 - Object-oriented Programming Foundations

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Object-oriented Terminology
Much of the terminology of object-oriented programming is language independent—that is, it is not associated with a specific language such as Pascal or C++. Therefore, many of the following definitions apply to the various implementations of object-oriented languages. Chapter 17 discusses terms that are more C++ specific.
Object-oriented programming is a programming technique that allows you to view concepts as a variety of objects. By using objects, you can represent the tasks that are to be performed, their interaction, and any given conditions that must be observed. A data structure often forms the basis of an object; thus, in C or C++, the struct type can form an elementary object. Communicating with objects can be done through the use of messages, as mentioned earlier. Using messages is similar to calling a function in a procedure-oriented program. When an object receives a message, methods contained within the object respond. Methods, also called member functions, are similar to the functions of procedure-oriented programming. However, methods are part of an object.
The C++ class is an extension of the C and C++ struct type and forms the required abstract data type for object-oriented programming. The class can contain closely related items that share attributes. Stated more formally, an object is simply an instance of a class. In Figure 16-1, the Lincoln automobile class is illustrated.
Figure 16-1: A diagram of the Lincoln class
Assume that the Lincoln automobile class is described in the program’s code. This class might include a description of items that are common to all Lincolns and data concerning maintenance intervals. At run time, three additional objects of the Lincoln class can be created. They could include the Lincoln Town Car, the Lincoln Mark, and the Lincoln Continental. The additional objects might include details of features and data common to each individual model. For example, a Mark VII is an object that describes a particular type of Lincoln automobile. It is an instance of the Lincoln class.
If a message is sent to the instance of the Lincoln class (similar to a call to a function) with instructions to adjust the air suspension on all four wheels, that message could be utilized only by the Mark object of the class. Only the Lincoln Mark has four-wheel air suspension.
Ultimately, there should emerge class libraries containing many object types. Then, instances of those object types could be used to piece together program code. You will see interesting examples of this when Windows class libraries are described in Chapters 22 and 23.
Before you examine these terms in closer detail, it is a good idea to become familiar with several additional concepts that relate to C++ and object-oriented programming, as described in the next few sections.
Encapsulation
Encapsulation refers to the way each object combines its member data and member functions (methods) into a single structure. Figure 16-2 illustrates how you can combine data fields and methods to build an object.
Figure 16-2: Data fields and methods combined to build an object
Typically, an object’s description is part of a C++ class and includes a description of the object’s internal structure, how the object relates with other objects, and some form of protection that isolates the functional details of the object from outside the class. The C++ class structure does all of this.
Functional details of the object are controlled in a C++ class by using private, public, and/or protected descriptors. In object-oriented programming, the public section is typically used for the interface information (methods) that makes the class reusable across applications. If data or methods are contained in the public section, they are available outside the class. The private section of a class limits the availability of data or methods to the class itself. A protected section containing data or methods is limited to the class and any derived subclasses.
Class Hierarchy
The C++ class actually serves as a template or pattern for creating objects. The objects formed from the class description are instances of the class. It is possible to develop a class hierarchy where there is a parent class and several child classes. In C++, the basis for doing this revolves around derived classes. Parent classes represent more generalized tasks, while derived child classes are given specific tasks to perform. For example, the Lincoln class discussed earlier might contain data and methods common to the entire Lincoln line, such as engines, instrumentation, batteries, braking ability, and handling. Child classes derived from the parent, such as Town Car, Mark, and Continental, could contain items specific to the class. For example, the Mark is the only car in the Lincoln line with four-wheel air suspension.
Inheritance
Inheritance, in object-oriented programming, allows a class to inherit properties from a class of objects. The parent class serves as a pattern for the derived class and can be altered in several ways. (In the next chapter you will learn that member functions can be overloaded, new member functions can be added, and member access privileges can be changed.) If an object inherits its attributes from a single parent, it is called single inheritance. If an object inherits its attributes from multiple parents, it is called multiple inheritance. Inheritance is an important concept since it allows reuse of a class definition without requiring major code changes. Inheritance encourages the reuse of code since child classes are extensions of parent classes.
Polymorphism
Another important object-oriented concept that relates to the class hierarchy is that common messages can be sent to the parent class objects and all derived subclass objects. In formal terms, this is called polymorphism.
Polymorphism allows each subclass object to respond to the message format in a manner appropriate to its definition. Imagine a class hierarchy for gathering data. The parent class might be responsible for gathering the name, social security number, occupation, and number of years of employment for an individual. You could then use child classes to decide what additional information would be added based on occupation. In one case, a supervisory position might include yearly salary, while in another case a sales position might include an hourly rate and commission information. Thus, the parent class gathers general information common to all child classes while the child classes gather additional information relating to specific job descriptions. Polymorphism allows a common data-gathering message to be sent to each class. Both the parent and child classes respond in an appropriate manner to the message. Polymorphism encourages extendability of existing code.
Virtual Functions
Polymorphism gives objects the ability to respond to messages from routines when the object’s exact type is not known. In C++, this ability is a result of late binding. With late binding, the addresses are determined dynamically at run time rather than statically at compile time, as in traditional compiled languages. This static (fixed) method is often called early binding. Function names are replaced with memory addresses. Late binding accomplishes this by using virtual functions, which are defined in the parent class when subsequent derived classes will overload the function by redefining the function’s implementation. When virtual functions are used, messages are passed as a pointer that points to the object instead of directly to the object.
Virtual functions utilize a table for address information. The table is initialized at run time by using a constructor. A constructor is invoked whenever an object of its class is created. The job of the constructor here is to link the virtual function with the table of address information. During the compile operation, the address of the virtual function is not known; rather, it is given the position in the table (determined at run time) of addresses that will contain the address for the function.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

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