Find Articles in:
All
Business
Reference
Technology
News
Lifestyle

The basics of C++ for programmers: concepts of object-oriented programming are supported

Software Magazine, June, 1990

The Basics of C++ for Programmers

C++ is often characterized as a multiparadigm language, which means that it combines non-object-oriented concepts with object-oriented ones.

The language provides the programmer with a number of built-in variable types. In addition, C++ enables the compiler's type-support mechanism to be extended by allowing the programmer to define types.

Programmer-defined types are implemented as a combination of variables and structures, as well as the permitted operations on these. The operations themselves are implemented as functions written by the programmer. C++ provides the type "class" for this purpose.

A class lets the programmer gather, in a self-contained unit recognized by the compiler, the representational and operational aspects of the new type. A class also provides mechanisms for specifying various levels of accessibility to the data and functions contained within the class.

This places data members and their associated functions into a "capsule." All accesses to these data members must take place through member functions; this is strictly enforced by the compiler.

A C++ programmer begins to write a program by building a conceptual framework of the problem domain.

The programmer identifies various concepts, decides on how to represent these and specifies the operations that can be performed on instances of that concept.

In C++, an instance of a class is called an object. A client (any external function) typically interacts with an object by issuing function calls.

ENCAPSULATION ADVANTAGES

Encapsulation offers many advantages. Hiding the implementation from the client allows the programmer to safely modify a member function. As long as its calling sequences and semantics are preserved, client calls will continue to work, because encapsulation prevents them from relying on particular implementation details.

Encapsulation adds to program understandability, promotes reusability and simplifies debuging (providing that source-level debuggers are used).

Inheritance takes C++ a step further into "object orientedness."

An overall design principle in C++ holds that a concept does not live in isolation, but is related to other concepts. This principle is supported in C++ by inheritance, which is basically a compiler-enforced organizational notion.

In effect, a programmer designing a C++ application needs to first identify and understand the concepts required by the application, in order to create hierarchical classes out of these.

A class hierarchy in C++ is composed of a base class and its derived classes. A base class defines a fundamental notion, which a derived class then refines by providing additional members. This mechanism allows derived classes to reuse the existing capabilities of the base class, and modify only what is needed: The base class is simply extended so that the programmer does not have to rewrite it to add new features.

In C++, a class can have public members (available to clients and derived classes), protected members (available only to derived classes) and private members (only available to the implementing class itself).

C++ also provides the keyword "friend" to designate functions that are allowed access to private class members, but are themselves nonmember functions.

C++ supports multiple inheritance, which is the ability of a derived class to inherit the attributes of more than one base class. The use of multiple inheritance may furtherincrease code reusability and program flexibility.

Single or multiple inheritance has the same characteristics as any other tool or technique to construct software out of pieces: It can be quite powerful when used skillfully, but it can also lead to messy programs.

As a rule, programmers must ensure that base-class functions have generalized functionality (to prevent unexpected behaviors in the context of their derived classes), and that each derived class be provided with its own implementation of the base function.

POLYMORPHISM

To achieve inheritance in an elegant manner, C++ supports the concept of polymorphism. Polymorphism allows a programmer to have the same interface to different objects: A consistent interface will produce different results, depending on the object type.

Polymorphism is intimately associated with late-binding. Binding refers to the attachment of member functions to an instance of a class, and of calls to functions. It is an evaluative process that may occur at compile time, or at runtime. In C++, polymorphism is implemented as the combination of virtual functions and derived classes.

During execution, the runtime environment decides which function to invoke. The decision is based on the object type.

Polymorphism should not be confused with function overloading, which lets the compiler determine at compile time the invocation on the basis of the function argument types.

As a rule of thumb, the later the bind, the greater the flexibility of a program. The price for this flexibility in many object-oriented implementations has been performance.

 

BNET TalkbackShare your ideas and expertise on this topic

The following tags are supported in BNET comments:
<b></b> <i></i> <u></u> <pre></pre>

Leave a Reply

  1. You are currently a guest | Login?
advertisement
CIO SessionsVision Series on ZDNet

See and hear what CIOs the world over thinks about the business of technology and how it's changing the way we live and work.

Go
advertisement
  • Click Here
  • Click Here
advertisement

Content provided in partnership with Thompson Gale