Basics of C++

Structure of c++ program

Let’s start with a simple code of a c++ program which prints “Hello World” on the screen.

#include < iostream >
using namespace std;
// main() function where execution begins.
int main()
{
cout << “Hello World” << endl; // prints Hello World.
return 0;
}

Let us look at each part of the program.

  •  #include < iostream >

Code begins with # sign are directives , read and interpreted by preprocessor .After this
is a header file which contains information that is necessary or usefull to your program.This line instructs the preprocessor to include a section of  standard c++ code, that allows to perform standard input output operations.

  • using namespace std;

Namespace defines a scope for the identifiers that are used in program. For using the identifiers defined in the namespace scope we must include the using directive. The purpose of namespace is to localize the names of identifiers to avoid name collisions.

  • int main()

main() function is a special function in all c++ programs.It is the main function where program execution begins. This function is called when the program is run.

  • cout << “Hello World” << endl;

This is a C++ statement.A statement is a command given to the computer that instructs the computer to take a specific action, such as display to the screen, or collect input.This line of code instructs the computer to display Hello World to the screen.

  • return 0;

This line terminates the main() function and returns the value 0.

Data types and Variables

In any programming languages Data types refers the various types of data the variable can hold in that particular language. All variable use data types to store different types of data. When we define a variable in C++ , the compiler define some memory for that variable based on the type of data it is declared in. Every data types requires different amount of memory.

Data types is mainly divided into two types :

  1. Primitive Data Types : These data types are built-in and can be used by user to declare variables.
  • Integer.
  • Character.
  • Boolean.
  • Floating Point.
  • Double Floating Point.
  • Void .
  • Wide Character.

    2. Abstract Data Types : These data types are user defined. Ex. defining class or structure.

 

Leave a comment

Website Powered by WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started