Wednesday, January 17, 2007

Introduction to C++ Programming [Part 1]

Introduction To C++

Programming has evolved from procedural, structured to object-oriented. Earliest programs were thought of as a series of procedures that acted upon data. A procedure, or function, is a set of specific instructions executed one after the other. The data was quite separate from the procedures, and the trick in programming was to keep track of which functions called which other functions, and what data was changed. It was virtually impossible to understand a program by reading it and modifying such a program was a nightmare.

To get around the potential confusions, structured programming (a more disciplined style of programming) was created. The main idea behind structured programming is to divide and conquer (top-down approach). A computer program can be thought of as consisting of a set of tasks. Any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This approach was enormously successful for solving complex problems.

With time many deficiencies in structured programming became clear. It does not mimic the way we are thinking. It is natural to think of your data and what you can do with your data as related ideas. It does not promote reusability; programmers found themselves constantly reinventing new solutions to old problems. The idea behind reusability is to build components that have known properties, and then to be able to plug them into your program, as you need them. Programs are becoming increasingly interactive, and it has become important to design for that kind of functionality. Structured programming is more biased towards old-fashioned step-by-step user interfaces.

Object oriented programming attempts to address these issues. It provides techniques for managing enormous complexity, achieves reuse of software components, and couples data with the tasks that manipulate that data.

C++ fully supports object-oriented programming, including the four pillars of object-oriented development: encapsulation, data hiding, inheritance, and polymorphism, which we’ll be looking at in detail as we move along.

C++ was initially developed by Bjarne Stroustrup in the early 1980’s. C++ was developed by adding OOP features to C without significantly changing the C component. C++’s OOP aspect was inspired by a computer simulation language called Simula67. C++ is a superset of C, meaning that any valid C program is a valid C++ program too.

A touch of OOP

A touch of OOP would be useful for you to have a good mind-set on C++ programming. OOP emphasizes the data, unlike structured programming which emphasizes algorithms. The idea is to design data forms that correspond to the essential features of a problem. In C++, a class is a specification describing a new data form and an object is a particular data structure constructed according to that plan. For example, a class could describe the general properties of a teacher, while an object would represent a specific teacher, say, Mr. Perera.

In general, a class defines what data are used to represent an object and the operations that can be performed upon that data. The following example should make this point more clear.

You are writing a computer program capable of drawing circles. You could define a class to describe the circle; the data part would contain center, radius, thickness and colour attributes and the operations part would have move, resize, delete, copy, change colour, change thickness operations. Then you use this to create an object according to class specification. The object will hold the values describing the circle and you can use the class methods to modify that circle. If you draw two circles, the program will create two objects, one for each circle.

It should be clear that the OOP approach to program design is to first design classes that accurately represent those things with which the program deals. Then you proceed to design a program using objects of those classes. It follows a bottom-up approach, where you proceed from a lower level of organization, such as classes, to a higher level, such as a program design.

OOP is more than binding data and methods into a class specification. It facilitates creating reusable code which can save a lot of work. Information hiding safeguards data from improper access. Polymorphism lets you create multiple definitions for operators and functions. Inheritance lets you derive new classes from old ones.

Standards

If you can run a program without changing the source code on different platforms (with different C++ compilers), we say the program is portable. There are two obstacles for portability; hardware and language divergence.

Hardware specific programming is less likely to be portable. A good programming practice is to minimize the use of hardware specific functionality.

Compilers from different vendors might have slight incompatibilities. This inevitably creates the problem of language divergence. To minimize this, the Accredited Standards Committee, operating under the procedures of the American National Standards Institute (ANSI), started to create an international standard for C++. Today, most of the compiler vendors support ANSI/ISO C++ standard.

Steps of creating a program

The diagram shown below explains the process. Source code is your program written in a text editor. Compiler converts your source code to machine language and the output is called the object code. Linking combines your object code with object code for the functions you use (user created libraries and built-in libraries) and with some standard startup code to produce a runnable version of your program, which is called the executable code.



The extension you use for source files depends on the C++ implementation. Linux systems use C, cc, cxx or c whereas Windows systems use cpp or cxx.

No comments: