Microlink Information Technology College
Microlink Information Technology College
o
o
•
!
•
"
• # !
• # !
+ !
, !
- ( % ./ - 0
- 1 & ./ &2% 3 &.-
- & '( ./ '(2& 3 '(. -
- % ) ./ )2'( 4 0
- 5 '% ./ '%2'( 3 '%.-
- )( * ./ *2'% 4 0
3 ! '%
* 1 '( 6 (7 6 - .((
+
!
3 !
• 8 9 !
• - 9
• :
9! 3 !
; .
!
<
+ !
8
" #
9
<
9 <
<
!
$ %
• ,
• 9
• :
• :
• +
• 8
start
min= a1 i=2
T
i>n Display min stop
ai<min T min= ai
i=i+1
& '
•
+
• -
( : ;
3
+ 7= (=
3 +>"
1 ;
+
9 : ?9 803 << -"@
8
(
:
3 A+>"B
3 # A: B
C
) &
:
<
:
3 (77
- @ > 4
F +GG H -
> ? @ ? +GG
* ! & !
*
- F
- #
- #
9!
$ A!B . !1G !G I7
8
> " J $ 4
) &
< '
< K
3
(
3
8 9! !
88 -
A!B . !1G !G
I7
< <. 1 ' %
<27
<.7 L K1
1 1
</7 L GMA '% BK1 L 'MA '% BK1
888 <
(
#
"
0
+ , ) &
-
+ !
"
( 9 A9 B
1 + A+ B F
& ; A; B F !
% ; A, B ,:
8 +GG
- +GG
AN +>>B
+ AN +>>'/ N @H
B
; AN @H'/ N 9O9B
, A< '/ , :B
+
,
, '
( - !9 A+ ' B !
+
1 ; 9 ! @
8
& , 9
3 !
) *
. )
<
o 3
o 3
o 3
/ "
9
" A, # B
3 A B
8
!
> 3
void main ()
{
cout << "Hello World!";
getch();
}
// my first program in C++
3 A//B
; A# B 3
! P 8
#include<iostream.h> #include<conio.h>
void main ()
3 3
+GG !
cout +GG
# A Hello World # B
A B
getch()
getch is a code that wait until the user enters any character from keyboard so
here we can see the output on the screen until any character entered.
0 A; B 3
! +GG
getch();
}
8
F main
,
+ 3 3
+GG
// line comment
/* block comment */
#include<iostream.h>
#include<conio.h>
int main ()
{
cout << "Hello World! "; // prints Hello World!
cout << "I'm a C++ program"; // prints I'm a C++ program
getch();
}
0 * ) 1
9 !
a b result
2
# A_ B 0
8 3
A_ B !
8
+GG P 3
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue,
default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern,
false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new,
% 1
P
3 R
+GG
A 7 155B 8 !
'
0! +GG
( ) !3 4 5
'(16 (1*
char + (
7 155
short int '&1*)6 &1*)*
- 8 1
AshortB 7 )55&5
'&1*)6 &1*)*
int 8 1
7 )55&5
long int '1(%*%6&)%6 1(%*%6&)%*
; %
AlongB 7 %1S%S)*1S5
float 4 % & % GK' &6 A* B
double < 6 ( * GK' &76 A(5 B
;
long double (7 ( * GK' &76 A(5 B
) & *
8 +GG 3 !
A B
4 !
int a;
float mynumber;
3 3 int a
3 float mynumber a
mynumber
8
4 !
int a, b, c;
3 +GG
!
#include<iostream.h>
#include<conio.h>
void main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
< C
! & *
The scope of local variables is limited to the block enclosed in braces ({}) where they are declared.
2 3 & *
@
8 R
4 ! R 7
int a = 10;
,
+ ! !
)
const !
pathwidth tabulator 3 F !
#
3
a = 5;
3 5 a
a = b;
4 ! '8
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
getch();
}
3 ! +GG
a = b = c = 5;
8 5 a b c
789858:8;
3 +GG
G
'
N
K
T
3 D
A% B : 4
!
a = 11 % 3;
a 2 2 11 3
, 7#89#85#8:#
< &
value += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);
4 !
a = b;
a+=2; // equivalent to a=a+2
cout << a;
getch();
}
2 77899
- ! A++B A--B
3 # +=1 -=1 3
c++;
c+=1;
c=c+1;
B=3; B=3;
A=++B; A=B++;
// A contains 4, B contains 4 // A contains 3, B contains 4
8 9! ( B A 9! 1 B A
B
4 < 1 ##8=
#8>8?8>#8?#
8 ! # 3
@ @
.. 9#
U. 0 #
/ $
2 ;
/. $ #
2. ; #
(7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
! -
a=2 b=3 c=6
' =
8@@8AA
@ @ 4 !
3 && || ! 3
&& @ 0< 3
3 && ! a &&
b
@@ 6 4 64
* @@ *
3 || @ , 3
a || b
AA6 4 64
* AA*
4 !
, B
3 ! !
! 8
// conditional operator 7
#include<iostream.h>
#include<conio.h>
void main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c;
getch();
}
8 ! a 2 b 7 ! Aa>bB
# A B
b 7
$ 2 :6
! 6
@ +GG F
cout
cout F << A Q Q B
3 << 8 !
Output sentence 120 x
cout 0 # A" B
# A" B 4 !
3 A<<B
cout << "Hello, " << "I am " << "a C++ statement";
cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;
First sentence.
Second sentence.
Third sentence.
' endl 4 !
First sentence.
Second sentence.
! 2
3 +GG
! A>>B cin 3
! 4 !
int age;
cin >> age;
void main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is "<<i*2<<".\n";
getch();
}
C #
cin >> a;
cin >> b;
8 a b
cin ! A>>B
cin >> mystring;
cin !
F ! 3 D
! !
8 getline
cin
void main ()
{
char mystr[20];
cout << "What's your name? ";
cin.getline(mystr , 20);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
cin.getline(mystr , 20);
cout << "I like " << mystr << " too!\n";
getch();
}
0 getline AmystrB
,
3 if ! 8
if (condition) statement
condition ! 8 statement ! 8
statement A ! B
4 ! x is 100 x
100
if (x == 100)
cout << "x is 100";
8 !
{ }
if (x == 100)
{
cout << "x is ";
cout << x;
}
else 8
F if
4 !
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
3 if + else 3
! x A R B
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
2
;
!
4 ! '
void main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << n << ", ";
--n;
}
cout << "FIRE!\n";
getch();
}
8 ! ! condition '
! ! statement
condition 4 ! !
0
8 0 !
statement condition @
for initialization increase
- R
( initialization ! $ 3
!
1 condition 8 statement
A ! B
& statement ! { }
% increase ! 1
3 initialization increase 3
4 ! for (;n<10;)
R D for (;n<10;n++) R
A R B
A, B !
for initialization ! 3 A, B !
! ! 4 !
R
3 ! 57 n i
n 0 i 100 n!=i A n # iB @ n
i P 57 n i
# 50
* D
" break 8
4 !
A B
#include<iostream.h>
#include<conio.h>
void main ()
{
int n;
for (n=10; n>0; n--)
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
getch();
}
3 continue
F 4 !
5
void main ()
{
for (int n=10; n>0; n--) {
if (n==5) continue;
cout << n << ", ";
}
cout << "FIRE!\n";
getch();
}
goto F C
! F
3
A: B
$ F
' 4 ! goto
#include<iostream.h>
#include<conio.h>
void main ()
{
int n=10;
loop:
cout << n << ", ";
n--;
if (n>0) goto loop;
cout << "FIRE!\n";
getch();
}
exit cstdlib
3 exit ! 8
3 exitcode @ !
0 !
&
3 ! 8 F
! -
if else if 8
switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;
.
.
.
default:
default group of statements
}
8 ! # constant1 constant2 8 #
! group of statements 2 F switch
4 expression A
case B !
default: ! A B
9 < &
switch (x) {
if (x == 1) {
case 1:
cout << "x is 1";
cout << "x is 1";
}
break;
else if (x == 2) {
case 2:
cout << "x is 2";
cout << "x is 2";
}
break;
else {
default:
cout << "value of x unknown";
cout << "value of x unknown";
}
}
"
+GG
! 3
• type
• name
• parameters A B 9
A ! int xB
3 3
• statements P 8 { }
void main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
getch();
}
8 ! +GG
! main -
main z int ,
addition >
3 main addition
5 3 int a int b
main main
addition 3 A5 3B int a
int b
return (r);
R addition A
mainB
addition @ return addition
r Areturn (r);B 8 3
-
z addition (5, 3) 8 3 !
Aaddition (5,3)B A8 B
3 !
void main ()
{
int x=5, y=3, z;
Programming Fundamentals with C++ November 2009 24
Microlink Information Technology College
z = subtraction (7,2);
cout << "The first result is " << z << '\n';
cout << "The second result is " << subtraction (7,2)<< '\n';
cout << "The third result is " << subtraction (x,y) << '\n';
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z << '\n';
getch();
}
8 subtraction 3
0 ! main subtraction
8 !
4 ! A
! B
z = subtraction (7,2);
cout << "The first result is " << z;
8 A 5B
z = 5;
cout << "The first result is " << z;
subtraction
cout -
5 subtraction (7,2)
3 subtraction
3 8 subtraction x y
5 3 2
3 -
z = 4 + subtraction (x,y);
z = subtraction (x,y) + 4;
z = 4 + 2;
z = 2 + 4;
% 1 &
8 !
type A
B @
8 F
8 void 3
void printmessage ()
{
cout << "I'm a function!";
}
void main ()
{
printmessage ();
getch();
}
void P !
4 ! printmessage
void 8 +GG
3 ' ! !
4 printmessage
printmessage ();
3 +GG
3
printmessage;
*1& *1
"
3
4 ! addition
x y 5 3
x y
3 a b 5 3
a b
x y x y
@ !
4
!
void main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
getch();
}
3 duplicate
A& B 3
3 P x y z duplicate
main
A& B
x y z
> 4 !
!
void main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
getch();
}
) &
3
3
8
4 !
// default values in functions 6
#include<iostream.h> 5
#include<conio.h>
void main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
getch();
}
divide 8
divide (12)
divide - divide
2
A int b=2 F int bB 3
6 A12/2B
divide (20,4)
b Aint b=2B b
4 # 5 A20/4B
! #
3 ! 5 int 5
8 5
int ! #
4 ! 5 int billy
int 3
0 4 ! 0
; +GG
3 billy
(6 3 elements []
' R
! 8 !
2 3 1
{ } 4 !
void main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
getch();
}
& 1
8
3
name[index]
4 ! billy 5 int
4 ! 75 billy
billy[2] = 75;
! billy a
a = billy[2];
3 ! billy[2] int
" 1
: Q Q4 !
jimmy[1][3]
3 R
A B 3 '
3 #
4 ! (**)
(**5 (*** ! **) !
1**)
) & * 1
3
type * name;
type 3
U 4 !
int * number;
char * character;
float * greatnumber;
4 @
int * ted;
int andy,fred;
3 3
A& B
Q Q4 !
ted = &andy;
4 andy 1776 3
A1776B F
andy = 25;
fred = andy;
ted = &andy;
3 !
4 15 andy A
(**)B
4 ted andy A
1776B 3
andy A& B
A B
) 5
F >
Q Q
" 3
P ANB
Q Q
3 !
beth = *ted;
A beth #
Q tedQB beth 25 ted 1776
(**) 15
• V Q Q
• N Q Q
3 A B & *
andy = 25;
ted = &andy;
, !
andy == 25
&andy == 1776
ted == 1776
*ted == 25
3 ! # andy andy=25
3 A& B andy
1776 3 !
ted ted=&andy 3 !
A* B F Q Q ted 25
- ted
!
*ted == andy
void main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
getch();
}
! , !<
+GG - ;
#
char
4 !
17 char 8
3 # 17 @
# 4 ! jenny #
"Hello" # "Merry christmas" 17
3 #
# '\0'
A R B
17 char jenny #
"Hello" "Merry Christmas"
Notice how after the valid content a null character ('\0') has been included in order to indicate the end of
the sequence. The panels in gray color represent char elements with undetermined values.
! 0 *
3
(
2 /W
2 R /XD
< , 2 R /G( Y
Z7=
9! W (7XD
1
N2 /D
<
9! N D
) 1 ,
< 2 /W
2 R / XW
2 R / XD
9!
W5XW
(7XD
A .7D 25D GG B
// WXD
3
! '* 1%
0 8
[ 2 /
( ! -(
D
. A\ E
BD
22 D KK 5
1 1! 8
! -1 -(
W
17X D
A Y EBD
22 D KK
0 1 ! 8! 8
& ! 8
! -1 -(
OW17X <W 17XD
A -( \: EBD
A -1 \; BD
AO <B D
22 O D KK : ;
% ! -(
NCD
AC \ :8+, ;80 E
BD
ACBD
22CD KK
5 ! -(
NCD
AC \ E
BD
ACBD
22CD KK :8+, ;80
) ! 8
! -/-1 -(2-1 R -(.-1
-+88
0 !=
8! -( -1
9!
// string Example
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main ()
{
char name[100][15] , key[15];
int n,m=0;
cout<<”How many names : “;
cout<<”\nEnter all names one by one:”;
cin>>n;
for(int i=0 ; i<n ; i++)
cin>>name[i];
cout<<”Enter the name you are looking for:”;
cin>>key;
# +GG @
#
)
3
< +GG
!
] ^
] ( ] (D
] 1 ] 1D
] & ] &D
_ F ] D
structure_name object_name
F { }
3
structure_name
4 !
struct product {
int weight;
float price;
} ;
product apple;
product banana, melon;
; P !
void main ()
{
char mystr[20];
3 ! F 4 !
yours.year int mine.title string
"
+GG K
• -
• -
• - K
3 istream ostream
F cin F istream cout F
ostream 3
cin cout
; P !
void main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
getch();
}
3 example.txt
cout myfile
@ P
6
8 F open()
3
'
, A| B 4 !
example.bin open()
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app );
8 P close() 3
myfile.close();
3! ios::binary 3
! K
< ! cout
void main () {
ofstream myfile ("example.txt");
getch();
}
void main () {
string line;
ifstream myfile ("example.txt");
while (! myfile.eof() )
{
myfile.getline (line,100);
cout<<line<<endl;
}
myfile.close();
getch();
}
3 ! ! 0
eof()
myfile.eof() A
B