3
Introduction to C++ C++ Programming Watcharin Puangplia 01/07/2554
2
, -./
1. 2. 3. 4.
2- 3 4- 5 78-94 -3 ; - 5 ; 2 7- 9, 3
3
1. 2- 3 4- 5 !"#$"%&$!'( )*+, )-'./!0)12 $3 ,0 - 0,) 4 1 4 1!"#$561741- (Programming) () 4 1!"#$ ,/#= > )-'./!0)12 = > * = > +$% ! ?$ FORTRAN, COBOL, PASCAL, C, C++, Java !6N$0,$ " &$0)$ , #OP0,) -4 176Q*+,!6N$= > )-'./!0)12!", *O! #4?)$
4
2. 78-94 *$= > R'Q 'Q OP!"#$561741-5 #4 1!1#4* ,561741-#?)#70?QP !1#4/? ST 42 $ (Function) *$+$% 561741-) O61P4)U ,/#+Q #ST 42 $4V3 , 70?QPST 42 $OP0 & ( )/? )P314V 3 , 561741-= > R'Q 'Q - 1W $ (Execute) 3 ,OP0,) -)#? $,)# 1 ST 42 $ ( )/? main
5
= >?9 5@ main int main(void) { .
int main() { .
..
return 0; }
..
, 7-
return 0; }
, ,4; - ST 42 $ main !6N$ST 42 $+Q 4 0,) -)#\?! -)*$561741--. - !6N$ !", ") 561741- 7QP-+$, $0 - !"#$3/, +1()36!1#4ST 42 $#?)#)( $ ] (W, -) O 4$ &$7 ^QQ '_2))4- O)= ' 7QP , #OP ($ ? 0 !'( )U)4/? 561741-$&3 , $ ! 1VO7Q,/ 561741-4VOPW\46` 5 #) 05$- 0. - ST 42 $ main )#\?= #*$")U!"0! 1( ) +- # { } -+$, +Q 4*$4 1 0? ] = #*$")U!"0 ! ?$ '.-'2",) / - !-( )! 1VOST 42 $$&7Q,/ OP ($ ? 0
6
8 4-
9 1. 9 Source File : - 3SQ2",) / - (Text File) W\4!"#$"%&$0 - Syntax ") = > R'Q 'Q - -$ - 4 Q!6N$ .cpp
7
2.
(Compile) )-3'!Q)12= > R'Q 'Q (C++ Compiler) OP76Q ",) / - )#\?*$ Source File *+,4Q #!6N$1+ ! 1( ) )-'./!0)12!", *O7QP!4VU3/,*$)43SQ2 +$% !1#4/? Object File
3.
3925 (Link) 0 /! ( )- (Linker) OP01/O )UO 4 Object File /? -4 1!1#4* ,ST 42 $- 01h $ )#\?*$ C++ Library +1()3-? 1/-ST 42 $- 01h $!+Q? $ &$!", 4 U Object File ^Q") 4 1Q. 2 OP3 ,!6N$3SQ2 $3 , (Execute File)
8
C++ Program Environment
C++ Compiler Source File
Computer
Object File
Linker
C++ Library
Computer
Execute File
9
2 9 @; 2 9 2 9 (Statement) () " &$0)$*$4 1 $+$% " &$0)$ 4 OP0,) OU ,/#! 1( ) +- # ; ! ?$ cout<<"Hello World ! \n"; ;R 2 9 (Compound Statement) () +Q #] )#\?= #*$! 1( ) +- #6j44 ! ?$ int main() { cout<<"Hello World ! \n"; return 0; }
10
4 (Variable) 0 /761 () ( ) ^\,!"#$561741-0 & "%&$- !'( )* ,), ). '(&$ !4VU",)-\Q*$ +$?/# / -O "$ ") 0 /761OP"%&$)#\?4 U $. 0 /761 ! ?$ int, char, float, double 0 /761 0,) )#\? , $"/ ") ! 1( ) +- #! ? 4 U (=) ! -) !"#$ char int float double
$%#"&&'()*%+ 0 /) 4>1 1 0 / O $/$!0VO $/$O1. ( n$.#- 6 0 7+$? ) O $/$O1. ( n$.#- 12 0 7+$? )
' , (X 45) 1 4 4 8
11
@ 3 4 (Variable Type) (con.) char -> !6N$0 /761 * , +1 U!4VU",)-\Q !6N$0 /) 4>1"$ 10 /) 4>1 5 #* , !$(&) *$4 1O !4VU 1 3U02 0 /)#? ! ?$ rMt, rIt, rTt, r1t, r?t int -> !6N$0 /761 * , +1 U4 1!4VU ? ") 0 /!Q" !6N$O $/$!0V- - ? 1P+/? -2147483648 W% 2147483649 5 #* ,!$(&) *$4 1O !4VU",)-\Q 4 3U02 0 /)#? ! ?$ -5,-10, 2000 , 24571 !6N$0,$ long -> OP!6N$0 /761 !4VU ? !6N$O $/$!0V- -O $/$3U02!6N$ 2 ! ? ") O $/$ ! .- (- 4OP* ,!6N$ $ +$, ") 0 /761 ! ?$ long int) float -> !6N$0 /761 * ,!4VU",)-\Q !6N$!Q" n$.#- 5 #OP!4VU)#\?*$1\6") a.b x 10^e * ,'(&$ *$4 1O !4VU",)-\Q 4 3U02 - ? 1P+/? 3.4E-38 W% 3.4E+38 +1() 7 !6N$!Q" n$.#-3 ,3-?!4.$ 6 0 7+$? 0 /)#? 0 /761 $. $& ! ?$ 10.625, -6.67
12
@ 3 4 (Variable Type) (con.) Double -> !6N$0 /761 !4VU",)-\Q !6N$!Q" n$.#-!+-()$4 U float 70?OP* ,'(&$ *$ 4 1O !4VU- 44/? ! .- 2 ! ? () 8 3U02 - ? 1P+/? 1.7E-308 W% 1.7E+308 unsigned -> 7 /? !6N$0 /761 !4VU ? !6N$O $/$!0V-7UU3-? . ! 1() +- # (OP- ? !6N$U/4! ? $ &$) - 4* ,!6N$ $ +$, 0 /761 0 /)#? 4 1* , $ ! ?$ unsigned int $ &$) - 4OP* ,!6N$ $ +$, 0 /761 0 /)#? 4 1* , $ ! ?$ unsigned int
13
4 89@7 -4
) 4>10 /714OP0,) !6N$0 /) 4>1= > ) 4{> +1()! 1( ) +- # _ (under scroll character) - 1W0 & ( )0 /7615 #* ,0 /!Q"1?/-4 U0 /) 4>1= > ) 4{>3 , 70?+, -* ,0 /!Q" !6N$0 /) 4>10 /714 = > C++ OP!6N$61P!= Case sensitive 0 /) 4>1= > ) 4{>0 /'.-'2!QV47QP 0 /'.-'2*+}?W()!6N$ $QP0 /4 $ ! ?$ name 4 U NAME
14
4 89@7 -4
+, -0 & ( )R& 4 U /$ (Keywords) R% !6N$ -)#\?7Q,/*$= > R'Q 'Q ! ?$ auto default float register struct volatile void break do for return switch while static case double goto short typedef char long else if signed union const enum extern int sizeof unsigned continue 0 /)#? ! ?$ number 2value grade1
student_id float _score
15
.4
4?)$ OP* , $0 /761* ] OP0,) -4 161P4 n0 /761 4 1 & 1\67UU4 161P4 n0 /761: type identifier; 5 # type () $. ",)-\Q 0 /761$ &$OP!4VU3/,*$+$?/# / -O )-'./!0)12 identifier () ( )0 /761 4 - R 9 @R int number; char grade;
16
.4 (4R-) + 40,) 4 161P4 n0 /761 - $. ! #/4 $+Q #] 0 / - 1W!"#$3 ,*$ ! #/ 5 #* ,! 1( ) +- # , $1P+/? 0 /761 ! ?$ type identifier1, identifier2, `;
+1()
int width; int high; int width, high ;
17
, 2R a, 4
* ,! 1( ) +- # = 0 /)#? ! ?$ int score; score = 80; +- #W% 4 +$ *+,0 /761 score - ? ! ? 4 U 80 O 4 0?)36$& int a,b,c; a = 2; b = 3; c = a + b; O + ? ") 0 /761 c C=5
18
4 - R 9
, 2R a, 4
O 4 0?)36$& int a,b; a = 2; a = 5; b = a + 4; O + ? ") 0 /761 b
b=6 b=6 or b=9 b=9
O 4 0?)36$& int a; a = 2; a = a + 3; O + ? ") 0 /761 a
a= a=2 2 or a= a=5 5
19
4 - R 9
, 2R a, 4
int a; â&#x2013;Ť / -+- # () 61P4 n0 /761 a !6N$0 /761 * , +1 U!4VU ? !6N$!Q"O $/$ !0V- - ? )#\?1P+/? -2147483648 W% 2147483649 int a, b, c, d ; â&#x2013;Ť / -+- # () 61P4 n0 /761 a, b, c, d !6N$0 /761 * , +1 U!4VU ? 0 /!Q" O $/$!0V- - ? )#\?1P+/? -2147483648 W% 2147483649 !1 - 1W 61P4 n0 /761 $. ! #/4 $3 ,+Q #] 0 /*$4 161P4 n0 /761 1 & +$% ] unsigned M; â&#x2013;Ť / -+- # () 61P4 n0 /761 M *+,!6N$0 /761 $. unsigned int int num1=8; â&#x2013;Ť / -+- # () 61P4 n0 /761 num1 !6N$0 /761 !4VU ? 0 /!Q"O $/$!0V5 #*+,- ? !1. -0,$ ! ? 4 U 8
20
4 - R 9
, 2R a, 4
float money, price ; â&#x2013;Ť / -+- # () 61P4 n0 /761 money 7QP price *+,!6N$0 /761 * , +1 U!4VU ? !6N$!Q" n$.#-3 ,3-?!4.$ 6 +Q 4 char ch=tAt; â&#x2013;Ť / -+- # () 61P4 n0 /761 ch !6N$0 /761 !4VU ? 0 /) 4>1!'# 1 0 / () 0 /) 4>1 rAt unsigned long int test; â&#x2013;Ť / -+- # () 61P4 n0 /761 test !6N$0 /761 * , +1 U!4VU ? !6N$!Q" O $/$!0V-7UU# / 3-?-4 1 . ! 1( ) +- #
21
b a
, 4
4 14 +$ 0 /7615 #4 1 U ? - 1W 3 , 2 7UU () 1. Global Variable OP!6N$4 14 +$ 0 /7613/,$)44Q ?-") +1()ST 42 $ R% OP *+, - 1W!1#4* , $0 /7613 , & 561741- !'1 P!6N$4 161P4 n0 /7617UU _ 1Â&#x201E;P (Public) W, 3-?-4 14 +$ ? !1. -0,$") 0 /761!) 3/,OP- ? !1. -0,$!6N$ 0 2. Local Variable OP!6N$4 14 +$ 0 /7613/,!Â&#x2026;' P= #*$4Q ?- * +$% +1()ST 42 $* ST 42 $+$% R% OP - 1W!1#4* , $0 /7613 ,!Â&#x2026;' P= #*$4Q ?- +1()ST 42 $! ? $ &$ ST 42 $)( $OP3-? - 1W$ 36* , $3 ,!'1 P61P4 n*$Q 4>Â&#x201E;P ?/$0 / (Private) +1()!Â&#x2026;' P , ,4; : 41Â&#x201E; ( )0 /761 Global 7QP Local R& 4 $ OPW()!6N$ $QP0 /4 $ !$( ) O 44 1* ,'(&$ +$?/# / -O 0 7+$? 0? 4 $ *$ C++ OP$ 0 /76161P!= Local - * ,4?)$! -)
22
3 c 5 (Expression) $.'O$2 () 4 1$ ",)-\Q- 41P 0?)4 $0 & 70?+$% O $/$"%&$36 R% ) O!6N$",)-\Q ! #/ +1()+Q # ] ",)-\Q4V3 , ! ?$ ? 0 /761 !-( )$ ",)-\Q- 41P 4 $OP3 , ^QQ '_2*+-? R% ^QQ '_2$ &$OP"%&$)#\?4 U0 /41P 61P4)U ,/# 0 /41P (Operators) 7QP 0 /W\441P (Operands) }Q 4>Â&#x201E;2 * ,*$4 141P 4 U",)-\Q ! ?$ +, -, *, /
Operator 2+3
Operator Operands
(3 + 5) a - b
Operator Operands
23
4 g R- (Preprocessor) ?/$ )-3'!Q)12OP0,) 4?)$ OP!1. - )-3'Q2561741$ +$, ,/#! 1( ) +- # # 0 /)#? ! ?$ â&#x2013;Ť #include <iostream.h> ()#\?= #*$! 1( ) +- # <>) OP!6N$4 1* ,3SQ23QU1 1 iostream R% !6N$3SQ23QU1 1- 01h $ 7QP-ST 42 $4 11 U ? ).$' (cin) 7QP 4 17 ^Q!) 2' 0(cout) â&#x2013;Ť #define * ,4 +$ ( )"%&$- *+-? $ - * ,4 +$ ? ! ?$ #define PI 3.14 R% !6N$4 14 +$ ? *+,4 U PI ! ? 4 U 3.14 #include #endif #elif
#define #error #else
#if #ifndef #ifdef
#program #undef
24
#include !6N$4 17O, *+, )-3'!Q)12$ 3SQ2)( $!", -1?/-*$4 1 )-3'Q2 #include <filename> - searches standard library for file (use for standard library files) #include â&#x20AC;&#x153;filenameâ&#x20AC;? - searches current directory, then standard library (use for user-defined files)
+- #!+0 â&#x2013;Ť W, #include <> ()#\?*$! 1( ) +- # <>) OP* ,3SQ23QU1 1- 01h $ â&#x2013;Ť 70?W, 0 - ,/# #include Â&#x2030; Â&#x160; OP* ,3SQ2!Â&#x2039; ! ,) 1, "%&$- !)
25
#include #include <iostream.h> â&#x2013;Ť *+,$ 3SQ2 stdio.h - O 43 !14 )1 3 ,4 +$ 3/,7Q,/ (/usr/include) !", - 1?/-*$4 1 )-3'Q2 #include "sample.h" â&#x2013;Ť *+,$ 3SQ2 Â&#x2030;sample.hÂ&#x160; O 43 !14 )16TOO U $+1()3 !14 )1 4 +$ 3/,7Q,/ !", - 1?/-*$4 1 )-3'Q2 â&#x2013;Ť * ,*$41Â&#x201E; -4 1 1, 3SQ2 .h "%&$- !)
26
#define * ,*$4 14 +$ ? !'( ) / - P /4*$4 1!"#$5617410 /)#? ! ?$ â&#x2013;Ť #define PI 3.14159
+- #W% 4 +$ ? ( ) PI +1 U* ,7 $ ? 3.14159 â&#x2013;Ť #define START_STR "Welcome to my program."
+- #W% 4 +$ ? ( ) START_STR +1 U* ,7 $",) / -/? Welcome to my program.
27
2 -i3 (Comment) - 2 /._ () â&#x2013;Ť /*.............*/ : long comment â&#x2013;Ť //............. : short comment * ,*$4 1)_.U #561741",) / - W\4 comment )-3'!Q)12OP3-?$ - 1?/-*$4 1 )-3'Q2
28
4 1!"#$ )_.U # (comment)
4 14 +$ ?
29
3. -3 ; - 5 ; (Input and Output) ).$' (Input) â&#x2013;Ť 4 11 U ? ",)-\QO 4^\,* ,!", 36*$! 1( ) )-'./!0)12 â&#x2013;Ť ) 641Â&#x201E;2).$' - 01h $ (Standard Input) :- #2U)12 , !- 2 0 /)#? ! ?$ â&#x2013;Ť 4 17 ",) / - +1() ? 0 /761* ] *+,^\,* ,3 ,1 U1\, â&#x2013;Ť ) 641Â&#x201E;2!) 2' - 01h $ (Standard Output) :- +$, O), ! 1( ) '.-'2, Q 5'
30
>?9 5@ a
9g - = cout
0,) include 3SQ2 iostream.h 0 /)#? ! ?$
/8
a, R
g i5 Hello World !
31
OP!6N$4 17O, C++ preprocessor 4?)$4 1 )-3'Q2/? *$561741-$&OP-4 17 ",)-\QU$ +$, O)+1() 1 U",)-\QO 4 U)12 (return) ! "# (integer)
Prints the string of characters contained between the quotation marks. return is a way to exit a function from a function. return 0, in this case, means that the program terminated normally.
The entire line, including cout, the << insertion operator, the string "Welcome to C++!\n" and the semicolon (;), is called a statement All statements must end with a semicolon.
32
Printing a Line of Text cout ▫ Standard output stream object ▫ Connected to the screen << ▫ Insertion operator ▫ Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) ▫ cout << “Welcome to C++!\n”;
\ ▫ Escape character ▫ Indicates that a special character is to be output
33
Printing a Line of Text Escape Sequence
Description
\n
Newline. Position the screen cursor to the beginning of the next line.
\t
Horizontal tab. Move the screen cursor to the next tab stop
\r
Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.
\a
Alert. Sound the system bell.
\\
Backslash. Used to print a backslash character.
\"
Double quote. Used to print a double quote character.
34
4 - R 9
g i5 The volume of the box is 24
35
2 i5 , R 9cout 4 float 4 17 ? ") 0 /7617UU float $ &$ OP7 ? 3 ,3-?!4.$ 6 digits + 40,) 4 17 ? ") 0 /761 *+,- 44/? 6 digits 0,) * , setprecision(digit_number) 7QP0,) include<iomanip> 9 digits
f =123.4 f1=123.456 f2=123.457 f3=1.23457e+08 6 digits => 2+'3445,+6
36
#include <iostream.h> #include <conio.h> #include<iomanip> int main() { float f=123.4,f1=123.456,f2=123.456789,f3=123456789; cout<< setprecision(8) <<f<<â&#x20AC;&#x153;\nâ&#x20AC;?); cout<< setprecision(8) <<f1<<â&#x20AC;&#x153;\nâ&#x20AC;?); cout<< setprecision(8) <<f2<<â&#x20AC;&#x153;\nâ&#x20AC;?); cout<< setprecision(8) <<f3<<â&#x20AC;&#x153;\nâ&#x20AC;?); getch(); return 0; g i5 } f=123.4 f1=123.456 f2=123.45679 f3=1.2345679e+08
37
4 39 (String) 4Q ?-",)-\Q 61P4)U ,/#0 /) 4"1P!"#$!1# 4 $36 5 #OP-! 1( ) +- # "" () }61P4 n +1() Double Quote) Q,)-1)U)#\?! -) * ,0 /761 $. char *$4 1!4VU",)-\Q 70?0,) -4 14 +$ O $/$0 /) 4>1 ,/# ! ?$ â&#x2013;Ť char name[10]; â&#x2013;Ť !6N$61P4 n0 /761 ( ) name !6N$ string !4VU0 /) 4"1P3 , 9 0 / 1/-4 U0 / ) 4"1P6` , #)4 1 0 / () '\0' 1/-!6N$ 10 0 /
38
4 - R 9
a@ 4 string
g i5 My name is Mallika
39
>?9 5@ a
2R - = cin
0,) include 3SQ2 iostream.h 0 /)#? ! ?$
40
2 7 -9, 3
(Operators) 0 / !$.$4 1 Â&#x201E;.0n 012 (Arithmetic Operators) 0 / !$.$4 1 -' $_2 (Relational Operators) 0 / !$.$4 10114P (Logical Operators) 0 / !$.$4 161P4)U (Compound Operators) 0 / !$.$4 1U)4"$ (Sizeof Operator) 0 / !$.$4 17UU-! ( )$3" (Conditional Operator)
41
4 3
92b34. 4 5 (Arithmetic Operator) @7 * / % + -
, \Â&#x201E;
=
a@ $.'O$2714 * $.'O$2 )
4 - R 9 5*3
g i5 15
+ 1!) !n> + 1!) ?/$ U/4 QU
$.'O$2714 / $.'O$2 ) $.'O$2714 % $.'O$2 ) $.'O$2714 + $.'O$2 ) $.'O$2714 - $.'O$2 )
5/3 5%3 5+3 5-3
1 2 8 2
42
-94 3
@7 -
,
() * / % + -
R, #36"/ R, #36"/ R, #36"/
4 - R 9 O + ? ") $.'O$20)? 36$& 10 + 2 * 3 * -1
4 _________
2 * 5 % 8 + -7
-5 _________ 15
(3 + 5/2)*(7 - 3 - 5%2) _________ 5
*
3
43
4 - R 9 c9, 2R -9 3 c 54-R X 8 , a, int a = 50, b = 30, c = 5, d = 3, e = 10; float f = 3.75, g = 1.5, h = 40, x; 1. x = a / c + h â&#x20AC;&#x201C; e * g; 2. x = (f * b) â&#x20AC;&#x201C; a % c + d * h; 3. x =
((g * h)+a)- e % d * f + b â&#x20AC;&#x201C; c;
44
- - 5 4- 5
3 2R 2R
@7 -
,
=
a@
++ -++ --
3 , 9
lvalue++ lvalue-++lvalue --lvalue
, 9 3 R- R-
4 - R 9
x=1; x++; x=1; x--; x=1; ++x; x=1; --x;
g i5
x=1 x=1 x=2 x=0
, ,4; 5 #640.7Q,/ 4 1!"#$ x++ +1() ++x OP- / -+- #! ? 4 U4 1!'. - ? 36+$% 70?W, 5)!6)12!1!0)12 ++ / )#\?+Q 0 /761 +- #W% ^QQ '_2OP!6N$ ? ") x 4?)$ ? *$ x OPW\4!'. 36)4 1
45
4 - R 9 c9 g i5 -9 4R-X 8
g i5 a= 2 b= 4 c=1 b= 3 a=3 b= 4 a=3
46
- - 5 4- 5
R (Equality Operator) 5)!6)12!1!0)12 $. $&* ,4 U4 1!61#U! #U ? /? !6N$O1. (True) +1()! VO (False) 7QPW\4O /? !6N$5)!6)12!1!0)123U$ 1 $ &$ OP0,) * , ) $.'O$2!'( )!61#U! #U ? @7 - - - 5 4- 5
== !=
,
=
a@
4 - R 9 g i5
!61#U! #U/? 0,) ! ? 4 $
$.'O$2714 == $.'O$2 )
5 == 5
true
!61#U! #U/? 0,) 3-? ! ? 4 $
$.'O$2714 != $.'O$2 )
5 != 5
false
47
- - 5 4- 5
(Relational Operator) 5)!6)12!1!0)12 $. $&* ,4 U4 1!61#U! #U ? - 44/? $,)#4/? +1()! ? 4 U 7QP W\4O !6N$5)!6)12!1!0)123U$ 1 $ &$OP0,) * , ) $.'O$2!'( )!61#U! #U ? @7 - - - 5 4- 5
< <= > >=
,
=
a@
4 - R 9 a=5; b=3;
g i5
$,)#4/?
$.'O$2714 < $.'O$2 )
a<b
false
$,)#4/? +1()! ? 4 U
$.'O$2714 <= $.'O$2 )
a<=b
false
- 44/?
$.'O$2714 > $.'O$2 )
a>b
true
a>=b
true
- 44/? +1()! ? 4 U
$.'O$2714 >= $.'O$2 )
48
- - 5 4- 54
. 4 5(Logical Operator) 5)!6)12!1!0)12 $. $&* ,!61#U! #U4 U ? U\QQ$ (Boolean) R% OP- ? O1. +1()! VO ! ? $ &$ 3-? - 1W* , $. ",)-\Q)( $ ] 3 , - - 5 4- 5 ! && ||
@7 - not and or
=
a@ !$.'O$2 $.'O$2714 && $.'O$2 ) $.'O$2714 || $.'O$2 )
49
- - 5 4- 5 and (&&) 1. 5)!6)12!1!0)12 and (&&) OP!6N$O1. 4V0?)!-( )$.'O$27144 U$.'O$2 ) !6N$O1. & \? $)4$ &$^QQ '_2OP!6N$! VO+- 2R 3 c 5
2R 3 c 5 -9
g i5
true true false false
true false true false
true false false false
50
- - 5 4- 5 or (||) 2. 5)!6)12!1!0)12 or (||) OP!6N$! VO4V0?)!-() $.'O$27144 U$.'O$2 ) !6N$! VO & \? $)4$ &$^QQ '_2OP!6N$O1. +- 2R 3 c 5
2R 3 c 5 -9
g i5
true true false false
true false true false
true true true false
51
- - 5 4- 5 not (!) 3. 5)!6)12!1!0)12 not (!) * ,4Q U ? O 4O1. !6N$! VO +1()O 4! VO!6N$O1. 2R 3 c 5
g i5
!true !false
false true
52
-94 3
2 7 -9,
3.
9
()
R, #36"/
* / %
R, #36"/
+ -
R, #36"/
< > <= >= == !=
R, #36"/
&& ||
R, #36"/
53
4 3
- (Compound Operator) !6N$1\6#?)") 0 / !$.$4 17QP0 /761 W4\ !$.$4 1 0 /)#? ! ?$ x = x + 1; !"#$7 $3 , ,/# x += 1; a = a - b; !"#$7 $3 , ,/# a -= b; x = x * 2; !"#$7 $3 , ,/# x *= 2; y = y / 5; !"#$7 $3 , ,/# y /= 5; 4 - R 9 @R O 4 0?)36$& int num1 = 3, num2 = 1; num1 *= 4; num2 += num1 - 2; O + ? ") 0 /761 num2
g i5 num = 11
54
92R g i5 t @ 3 4 a, R (Casting) 1\67UU â&#x2013;Ť ( $. 0 /761) 0 /761+1()$.'O$2+1() ? 0 /)#? ! ?$ float div; int a = 1, b = 2; div = (float)a/b;
g i5 0.5
55
uv , 1. !"#$561741-= > R*$4 11 U",)-\Q R% 61P4)U ,/# ( ) $& +$ 4 / - \ 7QP "$ 1) ! , ") $ 4n%4> 5 #* ,ST 42 $ cout '.-'2 ? ))4 +$, O) 5 #'.-'2 ",) / -(string) !'( ))_.U # ? ") ",)-\Q0? ] '1,)- & * , Conversion Character Modifier *$4 1O 1\67UU*+,3 , 0 /)#? 4 11 $561741-0?)36$& 4 - R 9
Please enter your name: AjanO Jaidee Weight: 52.5 Height: 158.5 Shoes Size: 40 ____________________________________________ NAME WEIGHT HEIGHT SIZE AjonO Jaidee 52.5 158.5 40 ____________________________________________
56
2. !"#$561741-!'( )1 U ? 0 /!Q"O $/$!0V- 2 ? O 4^\,* , 7Q,/!4VU3/, 0 /761 A 7QP B 0 -Q U7Q,/'.-'2 ? A 7QP B ))4 O)= ' O 4$ &$ Q U ? 0 /761 A 7QP B 5 #* ,0 /761 C *$4 1O 4 1 7Q,/'.-'2^QQ '_2 ? A 7QP B ))4 O)= ')4 1 & 4 - R 9
A: 18 B: 7 Before swapping A = 18, B = 7 After swapping A = 7, B = 18
57
4 - R 9
Please enter your name: __________ Input 1: __ Input 2 : __ Cal(Input 1 + Input 2) : __ Cal(Input 1 - Input 2) : __ Cal(Input 1 * Input 2) : __ Cal(Input 1 / Input 2) : __ Cal(Input 1 % Input 2) : __