Wednesday, 5 June 2013

Beginning with C++

 

A Bit about C++

C++ is an object oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA in the early years of 1980's.

Since the language is an extension of the language C with the major addition being the class construct feature Stroustrup called the new language "C with classes" however in 1983 the name was changed to C++, the idea of C++ coming from the increment operator ++, thus bringing the logic that C++ is an incremented version of language C.

A Simple C Program

Let us begin with a simple example of a C++ prorgam that prints a string on screen.

#include<iostream>                 //Including the header file

using namespace std;

int main ()                                   //Main function
{

      cout<< "Hello World.\n";    //C++ statement to print the string "Hello World"
     
      return 0;                               // we have taken int main() hence we have to return an integer value
}                                                // End of Program.

Analysing the program

Like C, the C++ program is also a collection of functions. In the above example we have only one function i.e the main() . As usual execution of any program begins at main(). Every C++ program will have a main() . Like C , the statements in C++ end with a semicolon (;) .

Comments

Comment symbol is  // (double slash) for single line comments and for multi line comments we use
/*

*/

Similar to C programming.

Output operator

The only statement in the above program is an output statement.

The statement

cout<<"Hello World\n";
causes the string contained within the quotation marks to be displayed on the screen.

We find two new C++ features here 'cout' and '<<' . The identifier "cout" is a predefined object that represents the standard output stream in C++.

The operator "<<" is called the insertion or put to operator. It inserts or sends the contents of the variables on its right to the objects on the left.

Note :-   C++ also supports printf() for displaying an output but we will use the standard C++ notation i.e cout.

Header File

We have used the #include directive in the program :

#include<iostream>

This directive causes the preprocessor to add the contents of the iostream file to the program. It contains the declarations for the identifier cout, cin and the operators << .

The header files iostream should be included in the beginning of all programs that use input/output statements and as practically all programs use I/O statements so using iostream.h is compulsory.

Common Header Files

1. ctype.h - Contains function prototypes for functions to test characters for certain properties and prototypes for function that can be used to convert lowercase letters to uppercase letters and vice versa.

2. float - Contains the floating point size limits of the system.

3. limits - Contains the integral size limits of the system.

4. math - Contains function prototypes for math library functions.

5. string - Contains function prototypes for C style string processing functions.

6. stdlib - Contains function prototypes for conversion of numbers to texts, text to numbers, memory allocation, random numbers and various other utility functions.

7. time - Contains function prototype and types for manipulating the current system time and date.

8. iomanip - Contains function prototypes for stream manipulators that enable formatting of streams of data.

9. fstream - Contains function prototype for functions that perform input from files on disk and output to files on disk.

10. iostream - Contains standard prototype functions for standard input and output functions.

Return type of main ()

In C++, main() returns an integer type value to the operating system. Therefore every main() should ed with a return 0 statement; otherwise a warning or error might occur. Since main() returns an integer type value, return type is specified as int. The default type for all functions in C++ is int.

Try making some more programs to practise things learnt today and print some more strings. We will be back tommorow with some more programs to understand the input operator, variables and some more concepts of C++.

Thats all for today friends do post your comments below for sending us your feedback.

 

No comments:

Post a Comment

Thank you for commenting.