370 likes | 474 Views
M. Taimoor Khan taimoorkhan@ciit-attock.edu.pk. Review of Basic Structured Programming . Basic Program Construction. #include< iostream.h > void main() { //This is my first C++ Program /* This program will display a string message on the screen */
E N D
M. Taimoor Khan taimoorkhan@ciit-attock.edu.pk Review of Basic Structured Programming
Basic Program Construction #include<iostream.h> 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
Variables #include<iostream.h> 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
Local and Global Variables #include<iostream.h> int numb = 15; void main() { int var1; int var2, var3; { intvar 4; intvar 5; } var1 = 5; int var6 = 10; cout << var1 + var4 << endl; }
Static and Constant Variables #include<iostream.h> void main() { int var1; int var2, var3; { static int var4 = 9; } var1 = 5; int var5 = 10; const int var6 = 20; cout << var1 + var4 << endl; }
Other Variables Types • Float • Double • Char • Bool • Short • long
Escape Characters #include<iostream.h> void main() { cout << “ Things are \a getting \n better in \t c++”; } \a beep \b backspace \n new line \t tab \\ \’ \” etc
Input Stream #include<iostream.h> void main() { int score; cout << “Please enter the score value\n”; cin >> score; cout << “Score is: \t” << score; } Extraction operation / get from operator
Type Conversion / Mixed Expression #include<iostream.h> void main() { int height = 7; float length = 4.5; double sum = height + length; }
Type Conversion / Type Casting #include<iostream.h> void main() { int var1 = 5; char var2 = static_cast<char>(var1); } Static_cast<type>(Variable)
Arithmetic Operations #include<iostream.h> void main() { int a = 5; int b = 6; cout << a % b; } +, -, %, * /
Assignment Operators #include<iostream.h> void main() { int a = 5; int b = 6; b += a; cout << a % b; } =, +=, -=, *=, /=
Increment / Decrement Operator #include<iostream.h> void main() { int a = 5; cout << a << endl; cout << a++ << endl; cout << ++a << endl; cout << a-- << endl; cout << --a << endl; } Prefix, postfix
Relational Operators #include<iostream.h> void main() { int a = 5; int b = 6; cout << (a <= b); } <, >, ==, !=, <= >=
Loops • For Loop • While Loop • Do While Loop
For Loop #include<iostream.h> void main() { for(inti = 0; i < 5; i++) cout << i*2; } Declaration and initialization expression Test expression Increment expression
For Loop #include<iostream.h> void main() { int a; for( a =0; a<5; a++) { a = a*2; cout << a; } } Blocks
Nested For Loop #include<iostream.h> void main() { for(inti=0; i<2;i++) { for(int j=0; j<3; j++) cout << i+j<<endl; } }
While Loop #include<iostream.h> void main() { int a = 4; while (a > 1) { cout <<a <<endl; a--; } }
Do-While Loop #include<iostream.h> void main() { int a = 5; do{ cout <<a <<endl; a--; }while(a>2); }
Control / Decision Statements #include<iostream.h> void main() { int a = 5; int b = 6; if(a > b) cout << a % b; } if
If-else statement #include<iostream.h> void main() { int a = 5; int b = 6; if(a > b) cout << a % b; else cout << b % a; }
Nested If-else #include<iostream.h> void main() { int a = 5; int b = 6; if(a < 3) if( a > b) cout << a % b; else cout << b % a; }
Switch Statement #include<iostream.h> void main() { int num; cout << Enter a number <<endl;cin>> marks; switch(marks) { case 0: cout << “low”; break; case 1: cout << “medium”; break; case 2: cout << “high”; break; default: cout << “very high”; } }
Logical Operators #include<iostream.h> void main() { int var1 = 5; int var2 = 6; if(var1>3 && var1>var2) cout << “var1 is a high number” <<endl; } &&, ||, !
Operator Precedence • Unary !, ++, -- • Arithmetic *, /, %, +, - • Relational <,>,<=,>=,==,!= • Logical &&, || • Conditional ?: • Assignment =, +=, -=, *=, /=, %=
Functions #include<iostream.h> void display(); void main() { cout << “main body” <<endl; display(); cout << “main body” <<endl; } void display() { cout << “Display function called” <<endl; } Declaration Definition Calling
Passing variables #include<iostream.h> int sum(intv1, 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
By Reference #include<iostream.h> int sum(int& v1, int& v2) { return v1 + v2; } void main() { int a = 5; int b = 6; cout << a % b; } Passing objects
Function Overloading #include<iostream.h> 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); }
Recursion #include<iostream.h> Int fact(int n) { if(n > 1) return n * fact(n – 1); else return 1; } void main() { fact(3); }
Inline Function #include<iostream.h> inline intgetTwice(int a) { return a*2; } void main() { cout << getTwice(4); }
Scope of variables • Local Variables • Global Variables • Static Local Variables
Global Variable #include<iostream.h> int a = 5; intsomeFunction() { return a; } void main() { int b = a; cout << a % b; }
Static Local Variables #include<iostream.h> int a = 5; intsomeFunction() { static inthalfValue = a/2; return a; } void main() { int b = a + halfValue; cout << a % b; }
Returning by Reference #include<iostream.h> int& sum(int a, int b) { return a+b; } void main() { int a = 4; int b = 5; sum(a, b); }
Const Function Arguments #include<iostream.h> intsomeFunction(const int b) { if(b < 2) cout << “B < 2” <<endl; } void main() { int b = 5; cout <<someFunction(b); }