In this chapter we shall study about
- C++ introduction
- C++ Features
- C++ Hello World program with explanation
- C++ Tokens
- C++ Keywords
C++ is often considered as a first programming language to be learned. Because it is easy to learn. Our C++ tutorial has been designed in such a way that a newbie will be easily to learn the language.
1. C++ Introduction
- C++ is an object oriented language. It is heavily influenced by C language. Any valid C program can be run as a C++ program.
- C++ was developed by Bjarne Stroustrup in 1979.
- Today most of the gaming libraries are written in C++
- C++ is a middle level language. It has the features of High level language and also low level language.
- The latest version C++ 17 was released on Dec 1st 2017.
- “.cc” “.cpp” is the file extensions.
- We shall learn about the latest features introduced from C++ 11 to C++ 17 in this tutorial series.
2. C++ Features
- It is portable.
- It has stronger type checking. Example, if a function is declared that it will accept int variable, if you pass a float the compiler will give an error.
- It is object oriented.
- C++ is extensible; it means new features can be added easily.
- New memory management features. Any dynamically allocated memory can be freed by calling free ().
- C++ is platform independent. A program compiled and executed in one platform, same program can be used and compiled in another platform also.
- Inline functions are available instead of macros.
- C++ includes a Standard Template Library [STL]. This includes set of methods manipulating data structure.
- C++ uses bottom up approach.
3. C++ Hello World program with explanation
We shall learn about C++ program structure along with how to run in Linux.
Open a file and name it as “hello_world.cpp”
First Program:
#include<iostream> using namespace std; int main() { cout << "Hello World"<<endl; return 0; }
#include<iostream>
#include
This is a pre-processor directive.
iostream is a header file name. In C++ we don’t need to include “.h” extension. Older C++ compilers still need “.h” extension. Newer compilers don’t need that extension. For most of the programmes in C++ we use “iostream”, it provides methods to take the input and print the output to the console.
using namespace std;
namespace
is a special keyword used in C++. Here we are using std namespace. The compiler will check for classes, functions in the standard library. All of the standard library definitions are inside namespace std.
If we don’t use standard namespace, you need to write “std” behind every keyword.
std::cout << “Hello World”<<std::endl;
int main()
main()
function is the starting point of a C++ program.
{ ... }
They denote function body.
cout<< “Hello World” <<endl;
cout is used to write the output to the console and endl is used to put a new line.
To compile a CPP program in Linux use below command:
g++ hello_world.cpp –o hello_world
to execute use:
./ hello_world
Output:
Hello World
4. C++ Tokens
Tokens are the smallest available unit of a program. In C++ tokens are divided into 5 types:
- Keywords Ex: if, for, while
- Identifiers Ex: num, name
- Constants Ex: 10, 20
- operators Ex: +, /, –
- Special Symbols Ex: { }, ()