C++ Introduction

C++ is immensely popular, particularly for applications that require speed and/or access to some low-level features. It extends C. Infact, at first, it was created as a set of extensions to the C programming language by Bjarne Stroustrup in 1979.

C++ has features like it is object-oriented, it supports generic programming, it is impreative language etc. In addition to these features, it also provides facilities for low-level memory manipulation.

Because of it's various features, C++ has also been found useful in many contexts, with key strengths being software infrastructure and resource-constrained applications, including desktop applications, servers, and performance-critical applications like telephone,  network and space probe.

Lets start our learning with very first program.

 

Writing very first C program

Any text editor like Sublime Text, Notepad++ or Emaces which are available for cross-platform or just a plain text notepad can be used for writing code in C++. However, the text editors come with advanced capabilities such as code completion, code debugging, syntax highlighting, line numbers etc which makes life easier for reading and error detecting.

For all tutorials related to C++ programming in this blog, we've used CodeBlocks as IDE/ editor and GCC as compiler. 

 

Hello World! in C++

Let us start our programming with the traditional Hello World! program.

Here is the classic Helloworld program using C++ :

/* A Hello World program in C++ */
#include <iostream >
using namespace std;
int main()
{
    cout << "Hello, world!\n";
    return 0;
}

The ouput of the program is:

Hello, world!

Internal Details of the Helloworld program

  • First line of the prgram is comment, which in C++ starts with /* and are terminated with */. They can span multiple lines. Single line comment starts with //. For example: 
//First program in C
  • Second line is inclusion of a standard library which defines the procedures for input/output. Headerfiles contain the information necessary to use these libraries, such as function declarations and macros. Most of C++’s functionality comes from libraries. Don't forget to include this line to make your program run.
  • Third line tells the compiler that it should look in the std namespace for any identifier we haven’t defined. If we do this, we can omit the std:: prefix when writing cout as in example presented just after this discussion. But, this is the recommended practice.
  • Forth line represents the the entry-point function which all C++ programs have . This function comes in two forms:
int main() or int main(void)

int main(int argc, char *argv[])

The first takes no arguments, and the second receives command-line arguments from the environment in which the program was executed—typically a command-shell. We'll talk more about command line argments later.

  • Line 5 and 8 represents the start and end of the program scope. The braces { and } delineate the extent of the function block.
  • Line 6 and 7 are the statements of the program. Line 5 prints Hello, World! by calling the standard library object cout, which prints a character string to standard output (usually the screen). In this case, the cout object takes one argument (or input parameter): the string constant "Hello World!\n". The \n at the end of the string is an escape character to start a new line. Finally, the statement is terminated with a semicolon (;).

C is a free-form language with program meaning unaffected by whitespace in most circumstances. Thus, statements are terminated by ; not by a new line.

When a function completes, the program returns to the calling function. In the case of main(), the program terminates and control returns to the environment in which the program was executed. The integer return value of main() indicates the program’s exit status to the environment, with 0 meaning normal termination.Line 6 represents that function return 0 which indicates the successful termination of the program.

Hence, after these long but extremely useful explanation, the ouput of the program becomes much clear.

Another important thing we should keep in mind is that everything in C++ is case sensitive: someName is not the same as SomeName. Similarly, main() is not same as Main().

Here is another version of above program with same ouput.

// A Hello World program in C++
#include <iostream >
int main(){
    std::cout << "Hello, world!\n";
    return 0;
}

Here only the difference is std:: infront cout. And, we’re telling the compiler to look for cout in the std namespace, in which many standard C++ identifiers are defined. ( See more about namespace here)

 

Advantages of using C++

  • High-level language: when you write a program in it, the shorthands are sufficiently expressive that you don’t need to worry about the details of processor instructions. C++ does give access to some lower-level functionality than other languages(e.g. memory addresses).
  • Conciseness: C++ provides some especially powerful shorthands to express common sequence of commands more concisely.
  • Maintainability: C++ is object oriented which helps to improves maintainability.
  • Portability: Since programs written as text can be translated into instructions for many different processors; C++ can be used to write programs for nearly any processor.
C++ Basic Syntax