Cpp Tutorial: C++ Introduction

In this chapter we shall study about
1. C++ introduction
2. C++ Features
3. C++ Hello World program with explanation
4. C++ Tokens
5. 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.

C++ Introduction 

1. C++ is an object oriented language. It is heavily influenced by C language. Any valid C program can be run as a C++ program.
2. C++ was developed by Bjarne Stroustrup in 1979.
3. Today most of the gaming libraries are written in C++
4. C++ is a middle level language. It has the features of High level language and also low level language.
5. The latest version C++ 17 was released on Dec 1st 2017.
6. “.cc” “.cpp” is the file extensions.
7. We shall learn about the latest features introduced from C++ 11 to C++ 17 in this tutorial series.

C++ Features

1. It is portable.
2. 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.
3. It is object oriented.
4. C++ is extensible; it means new features can be added easily.
5. New memory management features. Any dynamically allocated memory can be freed by calling free ().
6. C++ is platform independent. A program compiled and executed in one platform, same program can be used and compiled in another platform also.
7. Inline functions are available instead of macros.
8. C++ includes a Standard Template Library [STL]. This includes set of methods manipulating data structure.
9. C++ uses bottom up approach.

C++ Hello World program with explanation

In this chapter we will 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

C++ Tokens:

Tokens are the smallest available unit of a program. In C++ tokens are divided into 5 types:
1. Keywords		Ex: if, for, while
2. Identifiers		Ex: num, name
3. Constants		Ex: 10, 20
4. operators 		Ex: +, /, -
5. Special Symbols	Ex: { }, ()

C++ keywords

break  continue  else    for    long      signed  switch   void
case   default   enum    goto   register  sizeof  typedef  volatile
asm         dynamic_cast  namespace  reinterpret_cast  try
bool        explicit      new        static_cast       typeid
catch       false         operator   template          typename
class       friend        private    this              using
const_cast  inline        public     throw             virtual
delete      mutable       protected  true              wchar_t
auto   const     double  float  int       short   struct   unsigned
char   do        extern  if     return    static  union    while
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *