top of page
  C++ language

 

C++ is an object oriented programming language based on C.

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which contain data and code: data in the form of fields and code in the form of methods.

 

C++  is a preferred language in system programming and embedded systems, because it is highly powerful and has excellent performance, efficiency and flexibility.

Many other modern object oriented programming languages like Java, D etc. have been influenced by C++ 

Little bit of history

C++ was invented by Bjarne Stroustrup in the year 1979.

In 1979, Stroustrup was working towards his PhD thesis using a language called Simula which had some object oriented features. But it was too slow. This inspired him to create a new language called "C with classes" based on C language. This had many features of OOPs like classes, inheritance, inline functions etc.

 

In 1983, the name of language was changed from C with classes to C++. 

 

You can read more about these in its Wikipedia page.

Let us plunge right on by writing our first C++ program.

First C++ Program

This simple program above, just displays a message "Hello world" (what else!) on the display.

 

Let us try to analyze various parts of this program now.

main() function

All C++ programs must have a function called main(). main function is the starting point of execution of the program.

int main()

 

Some of you might ask me - why is it int main()? Shouldn't it be void main()?

 

The answer is - Every  function has a return type. And the return type is specified in the function header before the function name. This return type is  the type of the value it returns.

e.g.

int product(), double findAver() etc.

 

If the function does not return a value, its return type is specified as void.

 

As we told already, the type of the return value is specified before the function name in the function header.

 

In this case , main() function returns an integer value.

We will see this concept in detail later when we talk about functions.

 

Note: main() function can also take optional arguments called command line arguments and optional environment arguments.

cout object

 

In C++, the input and output operations are performed using two objects viz cin and cout (console in and console out) which are instances of input stream and output stream.

cout is the object used to display values on console. It is an object of ostream class and it uses the overloaded operator << (insertion operator).

e.g.

cout<<10; 

cout<<m<<n;

The first line prints 10 on the screen. And second line prints the values of variables m and n.

endl

 

endl is a constant which stands for '\n' (new line) character. It displays a newline character. This endl  takes cursor to the next line.

namespace std

 

As  C++ programs get really complicated, related code and classes are placed in different groups called namespaces. To use classes, objects etc. from a namespace, you should import that namespace into your program. That is done by using statement.

 

All the standard library objects are available in a namespace called std.

 

That is why you need to import the namespace std into your programs with the line

     using namespace std;

iostream or iostream.h?

 

If you are familiar with C language, you might have used, include statements with .h (header files) extensions and will be tempted to add   .h to your include filenames.

In modern C++ compilers, cin, cout, endl and other i/o classes and variables are declared in a file called iostream - without .h extension. That is the reason we need to include this header  file iostream.

Some older compilers may still use iostream.h. But most compilers use the header files without .h extension.

Compiling the program

 

A compiler is a software which converts your program written (in C++ here) in high level language into machine language - a language your computer can understand.

 

There are many C++ compilers available - most of them are free. The preferred one is always gnu compiler g++. Using g++, you can compile the program as follows.

                 g++ myprogram.cpp

 

Here is a guide on how to use g++

Like C program, the C++ compiler creates an executable file with the name a.out (in a Linux system) and file with extension .exe in windows system. You can execute this by exeuting a.out.

You can also use C++ integrated development environments like eclipse, VisualStudio, codeblocks, QtCreator etc to develop your programs.

Now go ahead and type and run your hello world program in C++.

bottom of page