Chapter 2 BCP
Chapter 2 BCP
Chapter 2
Fundamentals of C++ Programming Language
2.1 A brief history of C++
The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was
doing work for his Ph.D. thesis. He began work on "C with Classes", which as the name implies
was meant to be a superset of the C language. His goal was to add object-oriented programming
into the C language, which was and still is a language well-respected for its portability without
sacrificing speed or low-level functionality.
His language included classes, basic inheritance, in lining, default function arguments, and
strong type checking in addition to all the features of the C language. The first C with Classes
compiler was called Cfront, which was derived from a C compiler called CPre. It was a program
designed to translate C with Classes code to ordinary C.
In 1983, the name of the language was changed from C with Classes to C++. The ++ operator in
the C language is an operator for incrementing a variable, which gives some insight into how
Stroustrup regarded the language. Many new features were added around this time, the most
notable of which are virtual functions, function overloading, references with the & symbol, the
const keyword, and single-line comments using two forward slashes.
In 1985, C++ was implemented as a commercial product. The language was not officially
standardized yet. The language was updated again in 1989 to include protected and static
members, as well as an inheritance from several classes.
In 1990, Turbo C++ was released as a commercial product. Turbo C++ added a lot of additional
libraries which have had a considerable impact on C++'s development. In 1998, the C++
standards committee published the first international standard for C++ ISO/IEC 14882:1998,
which is informally known as C++98.
2.2 Major programming paradigms
The major land marks in the programming world are the different kinds of features or properties
observed in the development of programming languages. Among these the following are worth
mentioning: Procedural, Structured and Object Oriented Programming Paradigms.
2.2.1 Procedural programming
Procedural programming is a programming paradigm based upon the
concept of procedure call. Procedural programming is often a better choice
than simple sequential programming in many situations which involve
moderate complexity or which require significant ease of maintainability.
Possible benefits: the ability to re-use the same code (function or procedure)
at different places, an easier way to keep track of program flow than a
collection of “GO TO” or “JUMP” statements.
The main function can be made to return a value to the operating system.
The Left French brace “{“signals the beginning of the main function body and the corresponding
Right French Brace “}” signals the end of the main function body. Every Left French Brace
needs to have a corresponding Right French Brace.
The lines we find between the braces are statements or said to be the body of the function.
A statement is a computation step which may produce a value or interact with input and output
streams. The end of a single statement ends with semicolon (;). The statement in the above
example causes the sting “Hello World!” to be sent to the “cout” stream which will display it on
the computer screen.
2.5 Compilation process of C++
Each C++ source file needs to be compiled into an object file. The object files resulting from the
compilation of multiple source files are then linked into an executable, a shared library, or a
static library (the last of these being just an archive of object files). C++ source files generally
have the .cpp, .cxx or .cc extension suffixes.
A C++ source file can include other files, known as header files, with the #include directive.
Header files have extensions like .h, .hpp, or .hxx, or have no extension at all like in the C++
standard library and other libraries’ header files (like Qt). The extension doesn’t matter for the
C++ preprocessor, which will literally replace the line containing the #include directive with the
entire content of the included file.
The first step that the compiler will do on a source file is run the preprocessor on it. Only source
files are passed to the compiler (to preprocess and compile it). Header files aren’t passed to the
compiler. Instead, they are included from source files.
Each header file can be opened multiple times during the preprocessing phase of all source files,
depending on how many source files include them, or how many other header files that are
included from source files also include them (there can be many levels of indirection). Source
files, on the other hand, are opened only once by the compiler (and preprocessor), when they are
passed to it.
For each C++ source file, the preprocessor will build a translation unit by inserting content in it
when it finds an #include directive at the same time that it’ll be stripping code out of the source
file and of the headers when it finds conditional compilation blocks whose directive evaluates
to false. It’ll also do some other tasks like macro replacements.
Once the preprocessor finishes creating that (sometimes huge) translation unit, the compiler
starts the compilation phase and produces the object file.
2.6 Input(Cin) / Output(Cout) in C++
Cout: is an object used for printing data to the screen.
To print a value to the screen, write the word cout, followed by the insertion
operator also called output redirection operator (<<) and the object to be
printed on the screen.
Syntax: Cout<<Object;
The object at the right hand side can be: