| PUSAT SKRIPSI DAN ARTIKEL2 tutorial_2

cplusplus.com

C++ Language Tutorial

Written by: Juan Soulié
Last revision: June, 2007

Available online at:
The online version is constantly revised and may contain corrections and changes

The C++ Language Tutorial

This document and its content is copyright of cplusplus.com © cplusplus.com, 2008. All rights reserved.
Any redistribution or reproduction of part or all of the content in any form is prohibited other than to print a
personal copy of the entire document or download it to a local hard disk, without modifying its content in any way
(including, but not limited to, this copyright notice).
You may not, except with express written permission from cplusplus.com, distribute the content of this document.
Nor may you transmit it or store it in any other website or other form of electronic retrieval system.

The C++ Language Tutorial


Table of contents
Table of contents ...............................................................................................................................3
Introduction ......................................................................................................................................5
Instructions for use ................................................................................................................................... 5
Basics of C++ ......................................................................................................................................7
Structure of a program ............................................................................................................................. 7
Variables. Data Types. ............................................................................................................................. 11
Constants ................................................................................................................................................ 17
Operators ................................................................................................................................................ 21
Basic Input/Output.................................................................................................................................. 29
Control Structures ............................................................................................................................ 34
Control Structures ................................................................................................................................... 34
Functions (I) ............................................................................................................................................ 41
Functions (II) ........................................................................................................................................... 47
Compound data types ...................................................................................................................... 54
Arrays ...................................................................................................................................................... 54
Character Sequences .............................................................................................................................. 60
Pointers ................................................................................................................................................... 63
Dynamic Memory.................................................................................................................................... 74
Data structures........................................................................................................................................ 77

Other Data Types .................................................................................................................................... 82
Object Oriented Programming.......................................................................................................... 86
Classes (I)................................................................................................................................................. 86
Classes (II) ............................................................................................................................................... 95
Friendship and inheritance ................................................................................................................... 100
Polymorphism ....................................................................................................................................... 107
Advanced concepts ........................................................................................................................ 113
Templates.............................................................................................................................................. 113
Namespaces .......................................................................................................................................... 120
Exceptions ............................................................................................................................................. 123
Type Casting .......................................................................................................................................... 127

The C++ Language Tutorial

Preprocessor directives......................................................................................................................... 133
C++ Standard Library ...................................................................................................................... 138
Input/Output with files ......................................................................................................................... 138

The C++ Language Tutorial


Introduction

!
"

#

"

!

!

$

$

%

!


&
'()"&

*
+

$

,

!

$

#

"

$

$
'

$
$
!

.

.

$

/

'()"&
(

"
0112!


33

(

!

$
'()"&

&
"

$

40153 6
!

& '()"&
!


$

"

The C++ Language Tutorial

'

.

*

The C++ Language Tutorial

Basics of C++

7

!

8

// my first program in C++

Hello World!

#include
using namespace std;
int main ()
{
cout

4{}6 ;
$

2

The C++ Language Tutorial

'

"

$

!

cout
#

!
Hello World

4
4

#

6

6


cout

iostream

!

std

(

.

4;6
$

4

$


6

4
$

06 '

3

$

$

+

$
4

.

//6

4

4

#6
6

!

4

!

cout6!

4{}6
!
=

$

!

!

int main ()
{
cout 6) )

// evaluates to false ( true && false ).
// evaluates to true ( true || false ).

&

(

$
$

$

"

8

condition ? result1 : result2
" condition

$

7==5 ? 4 : 3
7==5+2 ? 4 : 3
5>3 ? a : b
a>b ? a : b

//
//
//
//

result1!
returns
returns
returns
returns

result2

3, since 7 is not equal to 5.
4, since 7 is equal to 5+2.
the value of a, since 5 is greater than 3.
whichever is greater, a or b.

// conditional operator

7

#include
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout
>
4

6

<
>

86
!

!

$

4()68
int i;
float f = 3.14;
i = (int) f;
436!

3.14

:

!

(int) '

8

$

$

8

i = int ( f );
B

!

&(
!

?

% 8
a = sizeof (char);
1
sizeof

char
!

&
$

)
<

!
%

&

!
A

<
;

$ $

!
=

$

!

$

8

a = 5 + 7 % 2

*

The C++ Language Tutorial

8
a = 5 + (7 % 2)
a = (5 + 7) % 2

// with a result of 6, or
// with a result of 0
$

!

6

!

4

6

=
% $
0

!

8

)

?
< & &

::
() [] . .> ++ .. dynamic_cast static_cast
reinterpret_cast const_cast typeid
++ .. ~ ! sizeof new delete

<

$
4

* &

4

& &

$6
>

& &

>

& &

6

+ .
(type)
.* .>*

& &

*

* / %

2

+ .

5

>

1

< > =

03

== !=

00

&

'(-

0

^

I >

0

|

>

0

&&

'(-

0

||

>

0*

?:

02

= *= /= %= += .= >>=

& &

<

F
$
'
(

)!

$

8

a = 5 + 7 % 2;

2

& &

The C++ Language Tutorial

8
a = 5 + (7 % 2);

a = (5 + 7) % 2;

)

$

!

"

5

The C++ Language Tutorial

=

/)

K
K

!

$

!
!
.
#
'

C

%

$

;
&

#
%

iostream!

)
B

&

(

!

!
%

cout

%

!

i;
cout A/

!

!
!

"

..n;
4n6

&

4n>06

8

"

!

n

&

0!

8

do statement while (condition);
"

$

! $

$
=

$

condition
$

!
$

!

// number echoer

Enter number
You entered:
Enter number
You entered:
Enter number
You entered:

#include
using namespace std;
int main ()
{
unsigned long n;
do {
cout > n;
cout

y!

8

// classes example
#include
using namespace std;

area: 12

class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
int main () {
CRectangle rect;
rect.set_values (3,4);
cout