Introduction to C++

C++ is an extension to C Programming language. It was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. It is a deviation from traditional procedural languages in the sense that it follows object oriented programming (OOP) approach which is quite suitable for managing large and complex programs.

An object oriented language combines the data to its function or code in such a way that access to data is allowed only through its function or code. Such combination of data and code is called an object.

C++ Character Set

Character set is a set of valid characters that a language can recognize. A character represents any letter, digit or any other special character. The C++ has the following character set:

Letters: A-Z, a-z

Digits: 0-9

Special Characters: Space + - * / ^ \ ( ) [ ] { } = ! = < > . ‘ " $ , ; : % ! & ? _ # <= >= @

White spaces: Horizontal tab, Blank space, Carriage return, Newline, form feed

Basic Data Types

Every program specifies a set of operations to be done on some data in a particular sequence. However, the data can be of many types such as a number, a character, boolean value, etc.

C++ supports a large number of data types. The built in or basic data types supported by C++ are integer, floating point and character type.

Integer type (int)

An integer is an integral whole number without a decimal point. These numbers are used for counting. For example 26, 373, -1729 are valid integers.

Normally an integer can hold numbers from -32768 to 32767. However, if the need be, a long integer (long int) can also be used to hold integers from -2,147,483,648 to 2,147,483,648.

Floating point type (float)

A floating point number has a decimal point. Even if it has an integral value, it must include a decimal point at the end. These numbers are used for measuring quantities. Examples of valid floating point numbers are: 27.4, -927., 40.03

A float type data can be used to hold numbers from 3.4*10-38 to 3.4*10+38 with six or seven digits of precision. However, for more precision a double precision type (double) can be used to hold numbers from 1.7*10-308 to 1.7*10+308 with about 15 digits of precision.

Character Type (char)

It is a non numeric data type consisting of single alphanumeric character. Examples of valid character types are : ‘A’, ‘9’, ‘P’, ‘8’, ‘&’.


A token is a group of characters that logically belong together.The programmer can write a program by using tokens. C++ uses the following types of tokens:

  • Keywords
  • Identifiers
  • Literals
  • Punctuators
  • Operators

Keywords: There are some reserved words in C++ which have predefined meaning to compiler.

Identifiers: Symbolic names can be used in C++ for various data items used by a programmer in his program. For example, if a programmer wants to store a value 50 in a memory location, he or she can choose any symbolic name (say MARKS) and use it as MARKS = 50. A symbolic name is generally known as an identifier. The identifier is a sequence of characters taken from C++ character set. The rules for the formation of an identifier are:

  • An identifier can consist of alphabets, digits and/or underscores.
  • It must not start with a digit.
  • C++ is case sensitive, i.e., upper case and lower case letters are considered different form each other. 
  • It should not be a reserved word.

Literals: Literals (often referred to as constants) are data items that never change their value during the execution of the program. The following types of literals are available in C++.

  • integer-constants
  • character-constants
  • floating-constants
  • string-literals

Punctuators: The following characters are used as punctuators in C++:

  • Brackets [ ] opening and closing brackets indicate single and multidimensional array subscript.
  • Parentheses ( ) opening and closing brackets indicate functions calls, function parameters for grouping expressions, etc.
  • Braces { } opening and closing braces indicate the start and end of a compound statement.
  • Comma , it is used as a separator in a function argument list.
  • Semicolon ; it is used as a statement terminator.
  • Colon : it indicates a labelled statement or conditional operator symbol.
  • Asterisk * it is used in pointer declaration or as multiplication operator.
  • Equal sign = it is used as an assignment operator.
  • Pound sign # it is used as pre-processor directive.

Operators: Operators are special symbols used for specific purposes. C++ provides six types of operators:

  • Arithmetical operators: +, -, *, / , or %
  • Relational operators: <, <=, ==, >, >=, !=
  • Logical operators: ||, &&, !
  • Unary operators: +, -
  • Assignment operators: =
  • Conditional operators: ?:
  • Comma operator

Type Conversion

The process in which one pre-defined type of expression is converted into another type is called conversion. There are two types of conversion in C++.

  1. Implicit conversion
  2. Explicit conversion (type casting)


A number which does not charge its value during execution of a program is known as a constant. Any attempt to change the value of a constant will result in an error message. A constant in C++ can be of any of the basic data types. const qualifier can be used to declare constant:

const float Pi = 3.1215;


A variable is the most fundamental aspect of any computer language. It is a location in the computer memory which can store data and is given a symbolic name for easy reference. The variables can be used to hold different values at different values at different times during the execution of a program.

Input/Output (I/O)

C++ supports input/output statements which can be used to feed new data into the computer or obtain output on an output device such as: VDU, printer, etc. It provides both formatted and unformatted stream I/O statements. The following C++ streams can be used for the input/output purpose.

  • cin: console input
  • cout: console output

In addition to the above I/O streams, two operators << and >> are also used. The operator << is known as put to or bit wise shift operator. The operator >> is known as extraction or get from operator.

Structure of C++ Program

The structure of a C++ program is:

#include <header file>
main ( )

A C++ program starts with function called main ( ). The body of the function is enclosed between curly braces. The program statements are written within the braces. Each statement must end by a semicolon (statement terminator).

First Program

// This is may first program in C++
/* This program will illustrate different components of a simple program in
# include <iostream.h>
void main ( )
cout <<“This is may first program in C++”;
cout << “\n......................................”;


Comments are ignored by the compiler. Comments are included in a program to make it more readable. If a comment is short and can be accommodated in a single line, then it is started with double slash sequence in the first line of the program. However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */. Everything between /* and */ is ignored by the compiler.

Include <iostream.h>

The lines that start with symbol ‘#’ are called directives and are instructions to the compiler.The word include with ‘#’ tells the compiler to include the file iostream.h into the file of the above program. File iostream.h is a header file needed for input/output requirements of the program. Therefore, this file has been included at the top of the program. 

void main ( )

The word main is a function name. The brackets ( ) with main tells that main ( ) is a function. The word void before main ( ) indicates that no value is being returned by the function main ( ). Every C++ program consists of one or more functions. However, when program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed. 

The curly brackets and body of the function main ( )

A C++ program starts with function called main ( ). The body of the function is enclosed between curly braces. These braces are equivalent to Pascal’s BEGIN and END keywords. The program statements are written within the brackets. Each statement must end by a semicolon, without which an error message is generated.