6 -Introduction to Programming
Basic structure of a C++ Program
#include <headerfile> -----> line 1
using namespace identifier; ---->line 2
int main() ---->line 3
{ statements;
:
return 0;
}
Line 1: Pre-processor directive :
Instructs the compiler to perform an action before actual compilation.
Starts with the symbol #.
Eg:-#define, #undef, #include.
header files
Header files is pre stored program file that help to use some operators and
functions.
eg: <iostream>,<cstdio>,<cmath>
Line 3: main () function:
main() is the essential function for every C++ program.
A c++ program starts and ends within the main() function.
All other function used in the program are called from main ().
A sample program
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello, Welcome to WOHSS";
return 0;
}
PREPARED BY MUHAMMAD RAYEES K A, HSST COMPUTER APPLICATION, WOHSS PINANGODE
Downloaded from hssreporter.com
Guidelines for coding
1. Use suitable name for identifiers.
2. Use clear and simple expressions.
3. Use comments when needed.
4. Indentation
comments
Comments increase the readability of a program and helps in future program
modification.
It is not considered as the part of program and cannot be executed
Comments
Single line Comments Multiline Comments
// /* */
Variable Initialisation:
Supplying the initial value to a variable at the time of declaration.
(staticinitialisation)
eg: int n =5 ;
Supplying the initial value to a variable during execution(dynamic
initialisation)
eg: int sum=a+b; or cin>>a;
const Keyword
The const keyword specifies that the value of a variable does not change
throughout the program.
syntax :const data type variable_ name = value;
Eg:constfloat pi=3.14;
Type modifier
o The keywords signed,unsigned,short and long are type modifiers.
o A type modifier changes the meaning of the base data type to produce a new
data type.
PREPARED BY MUHAMMAD RAYEES K A, HSST COMPUTER APPLICATION, WOHSS PINANGODE
Downloaded from hssreporter.com
Arithmetic assignment operators
The operators which combine arithmetic and assignment operators. They are +=, -=,
*=, /=, %=
eg: a=a+6 can be given as a+=6 a=a/10 can be given as a/=10
Increment (++) and Decrement (--) operators
++ is used to increment the value in a variable by 1.
-- is used to decrement the value in a variable by 1.
There are two forms of increment/decrement operators, prefix form and post fix form.
a++(post increment form) and ++a (preincrement form) are same as a=a+1 or a+=1
a--(post decrement form) and --a (predecrement form) are same as a=a-1 or a-=1
A post-form denotes use, then changemethod.
A pre-form denotes change, then usemethod
int m=5; int m=5;
n=m++; n=--m;
(Now n -> 5 and m -> 6) (Now n -> 4 and m -> 4)
Type conversion: converting one data type to another data type
Two types of conversion:
1.Implicit (Type promotion):
This is done by the compiler and the conversion is from lower datatype to higher.
Implicit type conversion also known as automatic type conversion is performed by the
compiler
Eg: 5/2.0 => 2.5 (Here, int datatype of 5 is converted to float by compiler. Thus the
result of the float expression is also float)
2.Explicit (Type casting):
This is done by the programmer explicitly and conversion can be to any datatype.
The cast operator takes on the format,
(cast type) expressionor cast type (expression)
Eg: 5/(int)2.0 => 2 (Here, programmer uses type casting (int) to convert the float
datatype of 2.0 to int. Thus the result of this integer expression is also an integer.)
PREPARED BY MUHAMMAD RAYEES K A, HSST COMPUTER APPLICATION, WOHSS PINANGODE
Downloaded from hssreporter.com