Search This Blog

Tuesday, 22 October 2013

C++

The C++ language was designed by Bjarne Stroustrup at AT&T’s Bell Labs in Murray Hill, New Jersey, starting in 1979. By that time the C language had become well estab-lished as a powerful tool for systems programming (see C). However Stroustrup (and others) believed that C’s lim-ited data structures and function mechanism were proving inadequate to express the relationships found in increas-ingly large software packages involving many objects with complex relationships.

Consider the example of a simple object: a stack onto which numbers can be “pushed” or from which they can be “popped” (see stack). In C, a stack would have to be imple-mented as a struct to hold the stack data and stack pointer, and a group of separately declared functions that could access the stack data structure in order to, for example “push” a number onto the stack or “pop” the top number from it. In such a scheme there is no direct, enforceable relationship between the object’s data and functions. This means, among other things, that parts of a program could be dependent on the internal structure of the object, or could directly access and change such internal data. In a large software project with many programmers working on the code, this invites chaos.

An alternative paradigm already existed (see object-oriented programming) embodied in a few new languages (see Simula and Smalltalk). These languages allow for the structuring of data and functions together in the form of objects (or classes). Unlike a C struct, a class can contain both the data necessary for describing an object and the functions needed for manipulating it (see class). A class “encapsulates” and protects its private data, and communi-cates with the rest of the program only through calls to its defined functions.

Further in object-oriented languages, the principle of inheritance could be used to proceed from the most gen-eral, abstract object to particular versions suited for specific tasks, with each object retaining the general capabilities and revising or adding to them. Thus, a “generic” list foun-dation class could be used as the basis for deriving a variety of more specialized lists (such as a doubly-linked list).
68        C++

While attracted to the advantages of the object-ori-ented approach, Stroustrup also wanted to preserve the C language’s ability to precisely control machine behavior needed for systems programming. He thus decided to build a new language on C’s familiar syntax and features with object-oriented extensions. Stroustrup wrote the first ver-sion, called “C with Classes” as his Ph.D. thesis at Cam-bridge University in England. This gradually evolved into C++ through the early 1980s.

C++ Features

The fundamental building block of C++ is the class. A class is used to create objects of its type. Each object contains a set of data and can carry out specified functions when called upon by the program. For example, the following class defines an array of integers and declares some func-tions for working with the array. Typically, it would be put in a header file (such as stack.h):

const int Max_size=20; // maximum elements in Stack

class Stack { // Declare the Stack class public: // These functions are available
outside

Stack(); // Constructor to create Stack objects

void push (int); // push int on Stack int pop(); // remove top element private: // This data can only be used in

class int index;

int Data[Max_size]; };

Next, the member functions of the Stack class are defined. The definitions can be put in a source file Stack. cpp:

#include “Stack.h” // bring in the declarations Stack::Stack() { index=0;} // set zero for
new stack

void Stack::push (int item) { // put a num-ber on stack

Data[index++] = item;

}

int Stack::pop(){ // remove top number return Data [index–];

}

Now a second source file (Stacktest.cpp) can be written. It includes a main() function that creates a Stack object and tests some of the class functions:

#include “Stack.cpp” // include the Stack class

#include <iostream.h> // include standard I/O library

main() {

Stack S; // Create a Stack object called S int index;

for (index = 1; index <= 5; index++) S.push(index); // put numbers 1–5 on stack

for (index = 1; index <=5; index++) cout < S.pop(); // print the stack

}

The stack implementation is completely separate from any program code that uses stack objects. Thus, a program-mer could revise the stack class (perhaps using an improved algorithm or generalizing it to work with different data types). As long as the required parameters for the member functions aren’t changed, programs that use stack objects won’t need to be changed.

In addition to classes and inheritance, C++ has some other important features. The data types for function param-eters can be fully defined, and types checked automatically (although programmers can bypass this type checking if they really want or need to). New operators can be added to a class by defining special operator functions, and the same operator can be given different meanings when work-ing with different data types. (This is called overloading.) Thus, the + operator can be defined with a String class to combine (concatenate) two strings. The operator will still mean “addition” when used with numeric data.

An abstract object (one with no actual implementation) can be used as the basis for virtual functions. These func-tions can be redefined in each derived object so that when-ever an object of that type is encountered the compiler will automatically search “downward” from the base class and find the correct derived class function.

Later versions of C++ include a related concept called templates. A template is an abstract specification that can be used to generate class definitions for data types passed to it (see template). Thus, a list template could be passed a vector and a 2D array and it will create a list class definition for each of these types. Templates are generally used when there is no hierarchical inheritance relationship between the types (in that case the virtual base class is a better approach).

C++ provides object-oriented alternatives to the stan-dard libraries. For example, input/output uses a stream model, and I/O operators can be overloaded so they’ll work with new classes. There is also an improved error-handling mechanism using appropriate objects.

Growth of C++

During the late 1980s and 1990s, C++ became a very popu-lar language for a variety of applications ranging from sys-tems programming to business applications and games. The growth of the language coincided with the development of more powerful desktop computers and the release of inexpensive, easy-to-use but powerful development envi-ronments from Microsoft, Borland, and others. Since these compilers could also handle traditional C code, program-mers could “port” existing code and use the object-oriented techniques of C++ as they mastered them. By the late 1990s, however, C++, although still dominant in many areas, was being challenged by Java, a language that simplified some of the more complex features of C++ and that was designed particularly for writing software to run on Web servers and browsers (see Java). For an alternative approach to creating a somewhat more “streamlined” C-type language, see c#.

No comments:

Post a Comment