M. Taimoor Khan
#include void main() { //This is my first C++ Program /* This program will display a string message on the screen */ cout << “Hello to C++”; } Header file Preprocessor directives Main method Console output stream Insertion operator String constant or string literal Comments White spaces Program statements
#include void main() { int var1; int var2, var3; var1 = 5; int var4 = 10; cout << var1 + var4 << endl; } Variable names Type Declaration Initialization Arithmetic's / Comparisons End line manipulator
#include int numb = 15; void main() { int var1; int var2, var3; { int var 4; int var 5; } var1 = 5; int var6 = 10; cout << var1 + var4 << endl; }
#include void main() { int var1; int var2, var3; { static int var4 = 9; } var1 = 5; int var5 = 10; const int var6 = 20; cout << var1 + var4 << endl; }
Float Double Char Bool Short long
#include void main() { cout << “ Things are \a getting \n better in \t c++”; } \a beep \b backspace \n new line \t tab \\ \’ \” etc
#include void main() { int score; cout << “Please enter the score value\n”; cin >> score; cout << “Score is: \t” << score; } Extraction operation / get from operator
#include void main() { int height = 7; float length = 4.5; double sum = height + length; }
#include void main() { int var1 = 5; char var2 = static_cast (var1); } Static_cast (Variable)
#include void main() { int a = 5; int b = 6; cout << a % b; } +, -, %, * /
#include void main() { int a = 5; int b = 6; b += a; cout << a % b; } =, +=, -=, *=, /=
#include void main() { int a = 5; cout << a << endl; cout << a++ << endl; cout << ++a << endl; cout << a-- << endl; cout << --a << endl; } Prefix, postfix
#include void main() { int a = 5; int b = 6; cout << (a <= b); }, ==, !=, =
For Loop While Loop Do While Loop
#include void main() { for(int i = 0; i < 5; i++) cout << i*2; } Declaration and initialization expression Test expression Increment expression
#include void main() { int a; for( a =0; a<5; a++) { a = a*2; cout << a; } Blocks
#include void main() { for(int i=0; i<2;i++) { for(int j=0; j<3; j++) cout << i+j <<endl; }
#include void main() { int a = 4; while (a > 1) { cout <<a <<endl; a--; }
#include void main() { int a = 5; do{ cout <<a <<endl; a--; }while(a>2); }
#include void main() { int a = 5; int b = 6; if(a > b) cout << a % b; } if
#include void main() { int a = 5; int b = 6; if(a > b) cout << a % b; else cout << b % a; }
#include void main() { int a = 5; int b = 6; if(a < 3) if( a > b) cout << a % b; else cout << b % a; }
#include void main() { int num; cout > marks; switch(marks) { case 0: cout << “low”; break; case 1: cout << “medium”; break; case 2: cout << “high”; break; default: cout << “very high”; }
#include void main() { int var1 = 5; int var2 = 6; if(var1>3 && var1>var2) cout << “var1 is a high number” <<endl; } &&, ||, !
Unary !, ++, -- Arithmetic *, /, %, +, - Relational, =,==,!= Logical&&, || Conditional?: Assignment=, +=, -=, *=, /=, %=
#include void display(); void main() { cout << “main body” <<endl; display(); cout << “main body” <<endl; } void display() { cout << “Display function called” <<endl; } Declaration Definition Calling
#include int sum(int v1, int v2); void main() { int a = 5; int b = 6; cout << sum(a, b); } int sum(int v1, int v2) { int temp = v1 + v2; return temp; } By value Return statement
#include int sum(int& v1, int& v2) { return v1 + v2; } void main() { int a = 5; int b = 6; cout << a % b; } Passing objects
#include int sum(int v1, int v2) { cout << “function 1” <<endl; return v1 + v2; } int sum(int v1, int v2, int v3) { cout << “function 2” <<endl; return v1 + v2 + v3; } float sum (float v1, float v2) { cout << “function 3” <<endl; return v1 + v2; } void main() { int a = 5; int b = 6;int c = 10;float d = 4.5;float e = 6.4; cout << sum(a, b); cout << sum(a, b, c); cout << sum (d, e); }
#include Int fact(int n) { if(n > 1) return n * fact(n – 1); else return 1; } void main() { fact(3); }
#include inline int getTwice(int a) { return a*2; } void main() { cout << getTwice(4); }
Local Variables Global Variables Static Local Variables
#include int a = 5; int someFunction() { return a; } void main() { int b = a; cout << a % b; }
#include int a = 5; int someFunction() { static int halfValue = a/2; return a; } void main() { int b = a + halfValue; cout << a % b; }
#include int& sum(int a, int b) { return a+b; } void main() { int a = 4; int b = 5; sum(a, b); }
#include int someFunction(const int b) { if(b < 2) cout << “B < 2” <<endl; } void main() { int b = 5; cout <<someFunction(b); }