C++ Introduction
During the 60s, while computers were still in an early stage of development, many new programming languages appeared. Among them, ALGOL 60, was developed as an alternative to FORTRAN but taking from it some concepts of structured programming which would later inspire most procedural languages, such as CPL and its successors (like C++). ALGOL 68 also directly influenced the development of data types in C. Nevertheless ALGOL was a non-specific language and its abstraction made it impractical to solve most commercial tasks.
In 1963 the CPL (Combined Programming language) appeared with the idea of being more specific for concrete programming tasks of that time than ALGOL or FORTRAN. Nevertheless, this same specificity made it a big language and, therefore, difficult to learn and implement.
In 1967, Martin Richards developed the BCPL (Basic Combined Programming Language), which signified a simplification of CPL but kept the most important features the language offered. Although it too was an abstract and somewhat large language.
In 1970, Ken Thompson, immersed in the development of UNIX at Bell Labs, created the B language. It was a port of BCPL for a specific machine and system (DEC PDP-7 and UNIX) and was adapted to his particular taste and necessities. The final result was an even greater simplification of CPL, although dependent on the system. It had great limitations like it did not compile to executable code but threaded-code, which generates slower code in execution, and therefore was inadequate for the development of an operating system. Therefore, from 1971, Dennis Ritchie, from the Bell Labs team, began the development of a B compiler which, among other things, was able to generate executable code directly. This “New B”, finally called C, introduced in addition, some other new concepts to the language like data types (char).
In 1973, Dennis Ritchie had developed the basis of C. The inclusion of types, it’s handling, as well as the improvement of arrays and pointers, along with the later demonstrated capacity of portability without becoming a high-level language, contributed to the expansion of the C language. It was established with the book “The C Programming Language” by Brian Kernighan and Dennis Ritchie, known as the White Book, and that served as de facto standard until the publication of formal ANSI standard (ANSI X3J11 committee) in 1989.
In 1980, Bjarne Stroustrup, from Bell Labs, began the development of the C++ language, which would receive formally this name at the end of 1983 when its first manual was going to be published. In October 1985, the first commercial release of the language appeared as well as the first edition of the book “The C++ Programming Language” by Bjarne Stroustrup.
During the 80s, the C++ language was being refined until it became a language with its own personality. All that with very few losses of compatibility with the code with C, and without resigning to its most important characteristics. In fact, the ANSI standard for the C language published in 1989 took a good part of the contributions of C++ to structured programming.
From 1990 on, ANSI committee X3J16 began the development of a specific standard for C++. In the period elapsed until the publication of the standard in 1998, C++ lived a great expansion in its use and today is the preferred language to develop professional applications on all platforms.
Why C++?
C++ has certain characteristics over other programming languages. Important features of c++ as followed
Object-oriented programming
The possibility to orientate programming to objects allows the programmer to design applications from a point of view more like communication between objects rather than on a structured sequence of code. In addition, it allows greater reusability of code in a more logical and productive way.
Portability
You can practically compile the same C++ code in almost any type of computer and operating system without making any changes. C++ is the most used and ported programming language in the world.
Modular programming
An application’s body in C++ can be made up of several source code files that are compiled separately and then linked together. Saving time since it is not necessary to recompile the complete application when making a single change but only the file that contains it. In addition, this characteristic allows linking C++ code with code produced in other languages, such as Assembler or C.
C Compatibility
C++ is backward compatible with the C language. Any code written in C can easily be included in a C++ program without making any change.
Performance
The resulting code from a C++ compilation is very efficient, due indeed to its duality as high-level and low-level language and to the reduced size of the language itself.
Object-Oriented Paradigm
The C++ programming language provides a model of memory and computation that closely matches that of most computers. In addition, it provides powerful and flexible mechanisms for abstraction; that is, language constructs that allow the programmer to introduce and use new types of objects that match the concepts of an application.
Thus, C++ supports styles of programming that rely on fairly direct manipulation of hardware resources to deliver a high degree of efficiency plus higher-level styles of programming that rely on user-defined types to provide a model of data and computation that is closer to a human’s view of the task being performed by a computer. These higher-level styles of programming are often called data abstraction, object-oriented programming, and generic programming.
Features of OOPs are the following
- Encapsulation
- Data abstraction
- Inheritance
- Polymorphism
What is Encapsulation
It is a mechanism that associates the code and the data it manipulates into a single unit to and keeps them safe from external interference and misuse. In C++ this is supported by a construct called class. An instance of an object is known as an object which represents a real-world entity.
What is Data Abstraction
Data abstraction is a simplified view of an object that includes only features one is interested in while hides away the unnecessary details. In programming languages, a data abstraction becomes an abstract data type or a user-defined type. In OOP, it is implemented as a class.
Inheritance
Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or superclass, The new class that is formed is called the derived class. A derived class is also known as a child class or subclass. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming.
Polymorphism
Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways.
The implementation of each of the above object-oriented programming features for C++ will be highlighted in later sections.
[quote]To work with object Object-Oriented Paradigm we required classes and objects[/quote]
Classes & Objects
Classes are data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. Thus a Class represents a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects become functions of the class and are referred to as Methods.
An object is the basic unit of object-oriented programming. Objects are identified by their unique name. An object represents a particular instance of a class. There can be more than one instance of an object. Each instance of an object can hold its own relevant data. An Object is a collection of data members and associated member functions also known as methods.
For example consider we have a Class of Cars under which Santro Xing, Alto, and Wagner represent individual Objects. In this context each Car Object will have its own, Model, Year of Manufacture, Colour, Top Speed, Engine Power, etc., which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, Stop form the Methods of Car Class.
No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created.
The First C++ Program
Let us begin with a simple program that prints the string “Hello World” on the screen.
#include<iostream.h> int main() { cout<<”Hello World”; return 0; }
When we executed the program it will print following out on screen
Hello World
Points to remember
- Like C, Every program must have a function called main(), which is where execution starts and return value of always integer i.e. 0 or 1. If the program successfully executed then we should return value 0 and due to some exception or failure if explicitly terminated then it should return non zero value.
The #include directive
- The #include directive instructs the compiler to read another source file in addition to the one that contains the
#include
directive. The name of the additional source file must be enclosed between double quotes or angle brackets, for example #include <filename.h> or #include "filename.h"
- When we are including a file from default directory then recommend use <>, if we are including from current working directory then required to use “”.
- In the previous example, we are using
iostream.h
it t contains declarations for the identifier cout (<<) and cin(>>) operatoprs.
The Output Operator (<<)
- The only statement in the above example is the output statement. The statement
cout<< “Hello World”;
- This statement introduces two features specific to C++. The identifier cout(pronounced as “see out”) is a predefined object that represents the standard output stream in C++.
- The operator “<<”(the bit-wise left shift operator in C) is called the insertion or put to operator. This multiplicity in the behavior of an operator is one of the special features of C++, which will be discussed in the later parts of tutorials.
The Input operator (>>)
- Like, cout, the identifier cin(pronounced as “see in”) is a predefined object that corresponds to the standard input stream, which in this case represents the keyboard.
Comments
- Like C, in C++ also single line comments can possible using //(double slash).
- Comments may start with a double slash symbol and terminate at the end of the line. A comment may start anywhere in a line and whatever flows the symbol is ignored.
- The double slash symbol is basically used for single line comments.
- Multiline comments can be written as
/* ... */
//This is a single line comment /* This is a multi-line comment This is a multi-line comment */