My Blog
Articles related to programming, computer science, technology and research.

10/08/2016 Categories: C++ Programming. 3 Comments on Introduction to C++

History of C++


  • C++ was developed by Bjarne Stroustrup in 1979 at AT&T Bell Labs.
  • C++ is an object-oriented language which includes imperative as well as generic programming features.
  • C++ was developed with features inherited from SIMULA 67 and ALGOL 68.
  • C++ was initially called “C with classes”.


Following are various applications of C++ language:

  • Systems programming
  • Embedded systems programming
  • Weather control systems
  • Defence systems
  • Web applications
  • Server programming
  • Databases
  • Web search
  • e-commerce


Structure of a C++ Program


A C++ program can contain several parts or sections as shown below:




All sections except the main() function section are optional. Every C++ program must contain a main() function section. Classes in C++ are similar to structures in C. Classes in C++ can contain both variables and functions, where as it is not possible with structures in C. Functions can be defined inside and outside classes in C++.


Include Files Section


A C++ program might include one or more header files using the #include directive. The syntax for including header files is given below:


#include<stdio.h> [or] #include “stdio.h”


The extension of header files in C++ is “.h”. In the above example, the header files stdio.h. is included. All the function declarations in that header file are available in our program. Header files can be included anywhere in the program. Good programming practice is to include them at the beginning of the program.


Class Declaration or Definition Section


One or more classes can be defined in this section. A class can be defined after the main() section. Good programming practice is to define classes before main() section.


A class definition must terminate with a semi-colon (;). A class definition contains variables, function declarations (prototypes), or function definitions.


Class Function Definitions Section


This contains the definitions of functions that were declared in the above section (class declaration section). Functions with less code are defined within the class. Such functions are treated as inline functions. Functions with large amount of code are defined outside the class.


main() Function Section


Every C++ program must contain a main() function as the execution starts from main() function itself. A main() function might contain the code to create and use the objects of classes created in above sections.


Header Files and Libraries in C++


A library provided by a programming language consists of pre-defined functionality arranged in several files. These libraries can be used by programmers to write programs with their own logic without focusing on housekeeping functionality.


C++ library provides several header files (.h files) which includes a lot of pre-defined functionality like reading and writing to files, memory management routines, console I/O routines etc.


Below are some of the header files and the library functions available in those header files in C++:




A Sample C++ Program


Let’s consider the following C++ program which prints “Hello World” on to the console or terminal window. We will look at various elements used in the program.



Output of the above code is:



In the above program, iostream is the name of a header file which contains I/O functionality. In order to take input and print some output using our C++ program, we have to include that header file using #include directive.


Note that writing “.h” at the end of the header file name results in an error. Such style is deprecated in newer versions of C++.


The cin and cout elements are declared in std namespace inside iostream header file. So, to use either cin or cout, we have to write using namespace <name>. If you don’t want to write this line, you have to write std::cin and std::cout in your program instead of cin and cout.


Every C++ program must contain a main() function which specifies the starting point of execution. Since return type of main is int, you should return an integer value back in your program.


cout is the standard output object. It is used in conjunction with insertion operator (<<) to print output on the screen.


The return statement returned a value 0 which tells the operating system that the program has completed successfully. A non-zero value tells that the program execution was unsuccessful.


Finally, the first line is a comment. We can write single line comments in C++ using // and multi-line comments using /* and */.


Differences between C and C++


Following are various difference between C and C++:



Related Links:


You can follow any responses to this entry through the RSS 2.0 feed.

thank soo much teacher


Scroll Up