Introduction To Programming: Engr. Rashid Farid Chishti
Introduction To Programming: Engr. Rashid Farid Chishti
Programming
Chapter 02
C++ Programming Basics
Explanation:
This program consists of a single function
called main().
Basic Program Construction
Because of the parentheses() the compiler
comes to know that this a function not a
variable.
The parentheses aren’t always empty. They’re
used to hold function arguments (values passed
from the calling program to the function).
Line number 2, 3 and 4 are not part of the
function.
The word int preceding the function name
indicates that this particular function has a
return value of type int.
The body of a function is surrounded by braces
(sometimes called curly brackets).
Basic Program Construction
Every function must use this pair of braces
around the function body.
A function body can consist of many statements
but this function has only three statements
(line number 6, 7 and 8)
You can put several statements on one line and
one statement in over two or more lines.
1 cout
2 <<"Welcome to this course\n"
3 ;
cout.setf(ios::left); cout<<setw(10)<<"Pakistan";
cout.unsetf(ios::left); cout<<setw(12)<<212742631 << endl;
getch();
return 0;
}
The setw() Manipulator
setw(10) setw(12)
L O C A T I O N P O P .
I s l a m a b a d 1 0 1 5 2 0 0
K a r a c h i 2 1 2 1 3 4 5 6
P a k i s t a n 2 1 2 7 4 2 6 3 1
The Data Types Range For 4-bit Storage
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
signed char ch = 127;
cout<<(int)ch<<endl; ch = ch+1;
cout<<(int)ch<<endl; ch = ch+1;
cout<<(int)ch<<endl; ch = ch+1;
cout<<(int)ch<<endl<<endl;
ch = ch-1;
cout<<(int)ch<<endl; ch = ch-1;
cout<<(int)ch<<endl; ch = ch-1;
cout<<(int)ch<<endl; ch = ch-1;
cout<<(int)ch<<endl; ch = ch-1;
getch();
return 0;
}
The Data Types Range
// signtest.cpp tests signed and unsigned integers
#include <iostream>
using namespace std;
int main(){
int signedVar = 1500000000; //signed
unsigned int unsignVar = 1500000000; //unsigned
signedVar = (signedVar * 2)/3; //calculation exceeds range
unsignVar = (unsignVar * 2)/3; //calculation within range
cout << "signedVar = " << signedVar << endl; //wrong
cout << "unsignVar = " << unsignVar << endl; //OK
system("PAUSE");
return 0;
}
Type Conversion
// shows mixed expressions
#include <iostream>
using namespace std;
int main(){
int count = 7;
float avgWeight = 155.5F;
double totalWeight = count * avgWeight;
/* the lower-type variable is converted to the type of the
higher-type variable. Thus the int value of count is
converted to type float and stored in a temporary variable
before being multiplied by the float variable avgWeight. The
result (still of type float) is then converted to double so
that it can be assigned to the double variable totalWeight
*********/
cout << "totalWeight=" << totalWeight << endl;
system("PAUSE");
return 0;
}
Type Conversion
Type Cast
In C++ the term applies to data conversions specified by
the programmer, as opposed to the automatic data
conversions.
Sometimes a programmer needs to convert a value from one
type to another in a situation where the compiler will
not do it automatically or without complaining.
Here’s a statement that uses a C++ cast to change a
variable of type int into a variable of type char:
aCharVar = static_cast<char>(anIntVar);
system("PAUSE"); return 0;
}
The Remainder Operator (%)
// remaind.cpp demonstrates remainder operator
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
cout << 6 % 8 << endl // 6
<< 7 % 8 << endl // 7
<< 8 % 8 << endl // 0
<< 9 % 8 << endl // 1
<< 10 % 8 << endl // 2
<< -2 % 8 << endl // -2
<<-13 % 8 << endl; // -5
system("PAUSE");
return 0;
}
Arithmetic Assignment Operators, += , -= , *= , /= , %=
// demonstrates arithmetic assignment operators
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int ans = 27;
ans += 10; //same as: ans = ans + 10;
cout << ans << ", ";
ans -= 7; //same as: ans = ans - 7;
cout << ans << ", ";
ans *= 2; //same as: ans = ans * 2;
cout << ans << ", ";
ans /= 3; //same as: ans = ans / 3;
cout << ans << ", ";
ans %= 3; //same as: ans = ans % 3;
cout << ans << endl;
system("PAUSE"); return 0;
} // output is : 37, 30, 60, 20, 2
Increment Operators, ++ , --
// increm.cpp demonstrates the increment operator
#include <iostream>
#include <stdlib.h>