PGDCA Chapter -1- Overview of C++
Overview of C++
History of C++
C++ was written by Bjarne Stroustrup at Bell Labs during 1983-1985. C++ is an extension of C. Prior to 1983, Bjarne Stroustrup added features to C and formed what he called "C with Classes". He had combined the Simula's use of classes and object-oriented features with the power and efficiency of C. The term C++ was first used in 1983. C++ was designed for the UNIX system environment. With C++ programmers could improve the quality of code they produced and reusable code was easier to write.
Bjarne Stroustrup had studied in the doctoral program at the Computing Laboratory at Cambridge University prior to joining Bell Labs. Now, Bell Labs no longer has that name since part of Bell Labs became AT&T Labs. The other half became Lucent Bell labs. Prior to C++, C was a programming language developed at Bell Labs circa 19691973 The UNIX operating system was also being developed at Bell Labs at the same time. C was originally developed for and implemented on the UNIX operating system, on a PDP-1 computer by Dennis Ritchie.
He extended the B language by adding types in 1971. He called this NB for New B. Ritchie credited some of his inspiration from the Algol 68 language. Ritchie restructured the language and rewrote the compiler and gave his new language the name "C in 1972. 90% of UNIX was then written in C. The committee that wrote the 1989 ANSI Standard for C had started work on the C Standard project in 1983 after having been established by ANSI in that year. There were quite a number of versions of C at that time and a new Standard was necessary.
C is portable, not tied to any particular hardware or operating system. C combines the elements of high-level languages with the functionality of assembly language and has occasionally been referred to as a middle-level computer language. C makes it easy to adapt software for one type of computer to another. Was a direct descendant of the language B. The language B was developed by Ken Thompson in 1970 for the new UNIX OS. B was a descendant of the language BCPL designed by Martin Richards, a Cambridge University student visiting MIT.1. C++ is still one of the most powerful languages in use today.
Structure of C++ program
Every C++ program starts with main(), which returns an integer type value.
Main() declaration:
main ()
{
.....................
.....................
.....................
return(O);
}
OR
void main ()
{
.....................
.....................
.....................
}
C++ has It's own set of I/O library syntax, which are defined in a header file called 'iostream.h'. This header file needs to be included before main().
Probably the best way to start learning a programming language is by writing a program
Therefore, here is our first program:
//my first program in C++
First part
#include<iostream.h>
void main()
{
cout << "Hello World!";
}
Second part
Result
Hello World!
The first part shows the source code for our first program. The second one shows the result of the program once compiled and executed. The way to edit and compile a program depends on the compiler you are using. Depending on whether it has a Development Interface or not and on its version. Consult the compliers section and the manual or help included with your compiler if you have doubts on how to compile a C++ console program.
The previous program is the typical program that programmer apprentices write for the first time, and its result is the printing on screen of the "Hello World!" sentence. It is one of the simplest programs that can be written in C++, but it already contains the fundamental components that every C++ program has. We are going to look line by line at the code we have just written:
// my first program in C++
This is a comment line. All lines beginning with two slash signs(//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.
#include <iostream>
Lines beginning with a pound sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.
vold main()
This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, Independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++,what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.
Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.
Cout<<"Hello World";
This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.
cout represents the standard output stream in C++, and the meaning of the entire statement Is to insert a sequence of characters (In this case the Hello World sequence of characters) into the standard output stream (which usually is the screen) and (<<) represents Insertion operator.
cout represents the standard output stream in C++, and the meaning of the entire statement Is to insert a sequence of characters (In this case the Hello World sequence of characters) into the standard output stream (which usually is the screen) and (<<) represents Insertion operator.
cout is declared in the iostream standard file within the std namespace, so that's why we
needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.
Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).
Comments
Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to Insert notes or description embedded within the source code.
C++ supports two ways to insert comments:
// line comment
/* block comment */
The first of them, known as line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, known as block comment, discards everything between the /*characters and the first appearance of the*/ characters, with the possibility of Including more than one line.
We are going to add comments to our second program:
/* my second program in C++
with more comments */
#include <iostream.h>
void main()
{
cout<< "Hello World!"; //prints Hello World!
cout<< "I'm a C++ program"; //prints I'm a C++ program
}
Hello World! I'm a C++ program
If you include comments within the source code of your programs without using the comment characters combinations //, /* or */, the compiler will take them as if they were C++ expressions, most likely causing one or several error messages when you compile it.
Comments
Post a Comment