0% found this document useful (0 votes)
208 views

Microlink Information Technology College

Microlink Information Technology College provides courses in programming fundamentals with C++. The document outlines the college's programming courses, including topics covered such as variables, data types, control structures, functions and more. It also provides examples of simple "Hello World" C++ programs to demonstrate basic concepts like input/output, comments and using libraries.

Uploaded by

Kidus Wendesen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
208 views

Microlink Information Technology College

Microlink Information Technology College provides courses in programming fundamentals with C++. The document outlines the college's programming courses, including topics covered such as variables, data types, control structures, functions and more. It also provides examples of simple "Hello World" C++ programs to demonstrate basic concepts like input/output, comments and using libraries.

Uploaded by

Kidus Wendesen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

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 - .((

+
!

Programming Fundamentals with C++ November 2009 1


Microlink Information Technology College

3 !

• 8 9 !
• - 9
• :

9! 3 !

; .
!
<
+ !
8
" #
9
<
9 <
<
!
$ %
• ,
• 9
• :
• :
• +
• 8

Programming Fundamentals with C++ November 2009 2


Microlink Information Technology College
9! 3 !

start

a1, a2, a3 , a4 , a5 , , ,an

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

) &
:
<
:

Programming Fundamentals with C++ November 2009 3


Microlink Information Technology College
& ;
+ 9 !
9 22 D
+ 22E E
D

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

Programming Fundamentals with C++ November 2009 4


Microlink Information Technology College
-

+
,
, '

( - !9 A+ ' B !
+

1 ; 9 ! @
8

& , 9
3 !

) *

. )

<
o 3
o 3
o 3

/ "

9
" A, # B
3 A B
8

Programming Fundamentals with C++ November 2009 5


Microlink Information Technology College

!
> 3

// my first program in C++ Hello World!


#include<iostream.h>
#include<conio.h>

void main ()
{
cout << "Hello World!";
getch();
}
// my first program in C++
3 A//B

#include<iostream.h> and #include<conio.h>

; A# B 3
! P 8
#include<iostream.h> #include<conio.h>

void main ()

3 3
+GG !

cout << "Hello World";

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

// my second program in C++ Hello World! I'm a C++ program


#include<iostream.h>
#include<conio.h>
void main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";

Programming Fundamentals with C++ November 2009 6


Microlink Information Technology College

getch();
}

8
F main

,
+ 3 3

+GG

// line comment
/* block comment */

/* my second program in C++ Hello World! I'm a C++


with more comments */ program

#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,

Programming Fundamentals with C++ November 2009 7


Microlink Information Technology College
operator, private, protected, public, register, reinterpret_cast, return, short,
signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try,
typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t,
while
0 1 3 +GG Q Q 3
# 3 !
RESULT result Result 3

% 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
!

Programming Fundamentals with C++ November 2009 8


Microlink Information Technology College

// operating with variables 4

#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;

// print out the result:


cout << result;

// terminate the program:


getch();
}

< C

! & *

Programming Fundamentals with C++ November 2009 9


Microlink Information Technology College

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 !

const int pathwidth = 100;


const char tabulator = '\t';

pathwidth tabulator 3 F !

#
3
a = 5;

3 5 a

a = b;

4 ! '8

// assignment operator a:4 b:7

#include<iostream.h>
#include<conio.h>

void main ()
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
b = 4; // a:10, b:4

Programming Fundamentals with C++ November 2009 10


Microlink Information Technology College

a = b; // a:4, b:4
b = 7; // a:4, b:7

cout << "a:";


cout << a;
cout << " b:";
cout << b;

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 !

// compound assignment operators 5


#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b=3;
Programming Fundamentals with C++ November 2009 11
Microlink Information Technology College

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

(a == 5) // evaluates to false since a is not equal to 5.


(a*b >= c) // evaluates to true since (2*3 >= 6) is true.
(b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false.
((b=2) == a) // evaluates to true.

' =
8@@8AA

Programming Fundamentals with C++ November 2009 12


Microlink Information Technology College
3 ! +GG @ 0 3

@ @ 4 !

!(5 == 5) // evaluates to false because the expression in (5 == 5) is true.


!(6 <= 4) // evaluates to true because (6 <= 4) would be false.
!true // evaluates to false
!false // evaluates to true.

3 && || ! 3
&& @ 0< 3
3 && ! a &&
b

@@ 6 4 64

* @@ *

3 || @ , 3

a || b

AA6 4 64

* AA*

4 !

( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ).


( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).

, B
3 ! !
! 8

condition ? result1 : result2

8 condition ! result1 result2

7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.


7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
5>3 ? a : b // returns the value of a, since 5 is greater than 3.
a>b ? a : b // returns whichever is greater, a or b.

Programming Fundamentals with C++ November 2009 13


Microlink Information Technology College

// 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

cout << "Output sentence"; // prints Output sentence on screen


cout << 120; // prints number 120 on screen
cout << x; // prints the content of x on screen

3 << 8 !
Output sentence 120 x
cout 0 # A" B

# A" B 4 !

cout << "Hello"; // prints Hello


cout << Hello; // prints the content of Hello variable

3 A<<B

cout << "Hello, " << "I am " << "a C++ statement";

3 Hello, I am a C++ statement 3


A<<B

cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;

Programming Fundamentals with C++ November 2009 14


Microlink Information Technology College
8 age 24 zipcode 90064

Hello, I am 24 years old and my zipcode is 90064

8 ! ' cout 8 +GG


' \n A B

cout << "First sentence.\n ";


cout << "Second sentence.\nThird sentence.";

First sentence.
Second sentence.
Third sentence.

' endl 4 !

cout << "First sentence." << endl;


cout << "Second sentence." << endl;

First sentence.
Second sentence.

! 2
3 +GG
! A>>B cin 3
! 4 !
int age;
cin >> age;

3 int age cin


A B

// i/o example Please enter an integer value: 702


The value you entered is 702 and its
#include<iostream.h> double is 1404.
#include<conio.h>

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 #

Programming Fundamentals with C++ November 2009 15


Microlink Information Technology College

cin >> a >> b;

cin >> a;
cin >> b;

8 a b

cin ! A>>B
cin >> mystring;

cin !
F ! 3 D
! !

8 getline
cin

// cin with strings What's your name? Juan Souli


#include<iostream.h> Hello Juan Souli .
#include <string> What is your favorite team? The Isotopes
#include<conio.h> I like The Isotopes too!

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

Programming Fundamentals with C++ November 2009 16


Microlink Information Technology College

,
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

if (condition) statement1 else statement2

4 !

if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";

x is 100 x 100 ' ' 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";

Programming Fundamentals with C++ November 2009 17


Microlink Information Technology College

2
;

while (expression) statement

!
4 ! '

// custom countdown using while Enter the starting number > 8


8, 7, 6, 5, 4, 3, 2, 1, FIRE!
#include<iostream.h>
#include<conio.h>

void main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;

while (n>0) {
cout << n << ", ";
--n;
}
cout << "FIRE!\n";
getch();
}

do statement while (condition);

8 ! ! condition '
! ! statement
condition 4 ! !
0

// number echoer Enter number (0 to end): 12345


You entered: 12345
#include<iostream.h> Enter number (0 to end): 160277
#include<conio.h> You entered: 160277
Enter number (0 to end): 0
void main () You entered: 0
{
unsigned long n;
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
} while (n != 0);
getch();
}

Programming Fundamentals with C++ November 2009 18


Microlink Information Technology College
3 '

8 0 !

for (initialization; condition; increase) statement;

statement condition @
for initialization increase
- R

( initialization ! $ 3
!
1 condition 8 statement
A ! B
& statement ! { }
% increase ! 1

// countdown using a for loop 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!


#include<iostream.h>
#include<conio.h>
void main ()
{
for (int n=10; n>0; n--) {
cout << n << ", ";
}
cout << "FIRE!\n";
getch();
}

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

for ( n=0, i=100 ; n!=i ; n++, i-- )


{
// whatever here...
}

3 ! 57 n i

Programming Fundamentals with C++ November 2009 19


Microlink Information Technology College

n 0 i 100 n!=i A n # iB @ n
i P 57 n i
# 50

* D

" break 8
4 !
A B

// break loop example 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!

#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

// continue loop example 10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!


#include<iostream.h>
#include<conio.h>

void main ()
{
for (int n=10; n>0; n--) {
if (n==5) continue;
cout << n << ", ";
}
cout << "FIRE!\n";
getch();
}

Programming Fundamentals with C++ November 2009 20


Microlink Information Technology College

goto F C
! F
3
A: B

$ F
' 4 ! goto

// goto loop example 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!

#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

void exit (int exitcode);

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
}

Programming Fundamentals with C++ November 2009 21


Microlink Information Technology College
8 expression # constant1
! group of statements 1 break break
F switch

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";
}
}

Programming Fundamentals with C++ November 2009 22


Microlink Information Technology College

"
+GG

! 3

type name ( parameter1, parameter2, ...) { statements }

• type
• name
• parameters A B 9
A ! int xB
3 3

• statements P 8 { }

// function example The result is 8


#include<iostream.h>
#include<conio.h>

int addition (int a, int b)


{
int r;
r=a+b;
return (r);
}

void main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
getch();
}

8 ! +GG
! main -

main z int ,
addition >

Programming Fundamentals with C++ November 2009 23


Microlink Information Technology College

3 main addition
5 3 int a int b

main main
addition 3 A5 3B int a
int b

4 addition Aint rB ! r=a+b


r a b @ a b 5 3
8

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

cout << "The result is " << z;

3 !

// function example The first result is 5


#include<iostream.h> The second result is 5
#include<conio.h> The third result is 2
The fourth result is 6
int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}

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;

cout << "The second result is " << subtraction (7,2);

subtraction
cout -

cout << "The second result is " << 5;

5 subtraction (7,2)

cout << "The third result is " << subtraction (x,y);

3 subtraction
3 8 subtraction x y
5 3 2

3 -

z = 4 + subtraction (x,y);

z = subtraction (x,y) + 4;

Programming Fundamentals with C++ November 2009 25


Microlink Information Technology College
! 8 A; B
8 3 !

z = 4 + 2;
z = 2 + 4;

% 1 &
8 !

type name ( argument1, argument2 ...) statement

type A
B @

8 F
8 void 3

// void function example I'm a function!


#include<iostream.h>
#include<conio.h>

void printmessage ()
{
cout << "I'm a function!";
}

void main ()
{
printmessage ();
getch();
}

void P !
4 ! printmessage

void printmessage (void)


{
cout << "I'm a function!";
}

void 8 +GG

3 ' ! !
4 printmessage

printmessage ();

3 +GG
3

printmessage;

Programming Fundamentals with C++ November 2009 26


Microlink Information Technology College

*1& *1
"
3
4 ! addition

int x=5, y=3, z;


z = addition ( x , y );

x y 5 3
x y

3 a b 5 3
a b
x y x y

@ !
4
!

// passing parameters by reference x=2, y=6, z=14


#include<iostream.h>
#include<conio.h>

void duplicate (int& a, int& b, int& c)


{
a*=2;
b*=2;
c*=2;
}

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

Programming Fundamentals with C++ November 2009 27


Microlink Information Technology College
3 ! a b c Ax y zB
a x
b y c z

3 P x y z duplicate
main

void duplicate (int& a, int& b, int& c)

void duplicate (int a, int b, int c)

A& B
x y z

> 4 !
!

// more than one returning value Previous=99, Next=101


#include<iostream.h>
#include<conio.h>

void prevnext (int x, int& prev, int& next)


{
prev = x-1;
next = x+1;
}

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>

int divide (int a, int b=2)


{
int r;
r=a/b;
return (r);
}

Programming Fundamentals with C++ November 2009 28


Microlink Information Technology College

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

Programming Fundamentals with C++ November 2009 29


Microlink Information Technology College

! #

3 ! 5 int 5
8 5
int ! #

4 ! 5 int billy

int 3
0 4 ! 0

; +GG

type name [elements];

type A int float B name elements A


# []B

3 billy

int billy [5];

(6 3 elements []
' R
! 8 !

2 3 1

{ } 4 !

int billy [5] = { 16, 2, 77, 40, 12071 };

Programming Fundamentals with C++ November 2009 30


Microlink Information Technology College
// arrays example 12206
#include<iostream.h>
#include<conio.h>

int billy [] = {16, 2, 77, 40, 12071};


int n, result=0;

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 & 5 int 3 +GG

int jimmy [3][5];

Programming Fundamentals with C++ November 2009 31


Microlink Information Technology College
! R !

jimmy[1][3]

(remember that array indices always begin by zero).

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;

Programming Fundamentals with C++ November 2009 32


Microlink Information Technology College
3 ted andy andy
A& B
A B

4 andy 1776 3
A1776B F

andy = 25;
fred = andy;
ted = &andy;

3 !

4 15 andy A
(**)B

3 fred andy A 15B 3

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

Programming Fundamentals with C++ November 2009 33


Microlink Information Technology College

C ! ted 1776 *ted A *


B 1776 25 0
A8 !
! B

beth = ted; // beth equal to ted ( 1776 )


beth = *ted; // beth equal to value pointed by ted ( 25 )

• 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

Programming Fundamentals with C++ November 2009 34


Microlink Information Technology College
0

// my first pointer firstvalue is 10


#include<iostream.h> secondvalue is 20
#include<conio.h>

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 !

char jenny [20];

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.

Programming Fundamentals with C++ November 2009 35


Microlink Information Technology College

! 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!

Programming Fundamentals with C++ November 2009 36


Microlink Information Technology College

// 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;

for(int i=0 ; i<n ; i++)


if(strcmp(name[i],key)==0)
m++;

cout<<”There are “<<m<<” “<<key;


getch();
}

Programming Fundamentals with C++ November 2009 37


Microlink Information Technology College

# +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 !

// example about structures Enter title: Alien


#include <iostream.h> Enter year: 1979
#include <string.h>
#include <fstream.h> My favorite movie is:
#include <conio.h> 2001 A Space Odyssey (1968)
And yours is:
struct movies_t { Alien (1979)
char title[20];
int year;
} mine, yours;

void printmovie (movies_t movie);

void main ()
{

Programming Fundamentals with C++ November 2009 38


Microlink Information Technology College

char mystr[20];

strcpy(mine.title,"2001 A Space Odyssey");


mine.year = 1968;

cout << "Enter title: ";


cin.getline(yours.title , 20 );
cout << "Enter year: ";
cin.getline (mystr,20);

cout << "My favorite movie is:\n ";


printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
getch();
}

void printmovie (movies_t movie)


{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}

3 ! F 4 !
yours.year int mine.title string

Programming Fundamentals with C++ November 2009 39


Microlink Information Technology College

"

+GG K

• -
• -
• - K

3 istream ostream
F cin F istream cout F
ostream 3
cin cout
; P !

// basic file operations [file example.txt]


#include <iostream.h> Writing this to a file
#include <fstream.h>
#include <conio.h>

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()

open (filename, mode);

filename ' # const char * A


B mode

3
'

, A| B 4 !
example.bin open()

Programming Fundamentals with C++ November 2009 40


Microlink Information Technology College

ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app );

9 open() ofstream ifstream fstream

4 ifstream ofstream ios::in ios::out


open()

8 P close() 3

myfile.close();

3! ios::binary 3
! K

< ! cout

// writing on a text file [file example.txt]


#include <iostream.h> This is a line.
#include <fstream.h> This is another line.
#include <conio.h>

void main () {
ofstream myfile ("example.txt");

myfile << "This is a line.\n";


myfile << "This is another line.\n";
myfile.close();

getch();
}

Programming Fundamentals with C++ November 2009 41


Microlink Information Technology College
< cin

// reading a text file This is a line.


#include <iostream.h> This is another line.
#include <fstream.h>
#include <string.h>
#include <conio.h>

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

Programming Fundamentals with C++ November 2009 42


Microlink Information Technology College

1. Microlink IT College, Addis Ababa Branch Student Note By Demesachew , 2004


2. Website: WWW.cplusplus.com
3. Jess Liberity, C++ An Introduction to Programming. Prentice Hall, 1996

Programming Fundamentals with C++ November 2009 43

You might also like