Upload
watcharin-puangplia
View
222
Download
1
Embed Size (px)
DESCRIPTION
แนะนำการเขียนด้วยโปรแกรม C++
Citation preview
����� ����� 33
������ ��������������������� ���������������
����� ����� 33
������ ��������������������� ��������������������������� ������������
Introduction to CIntroduction to C++++ ProgrammingProgrammingWatcharin Puangplia 01/07/2554
2
,����-./��,����-./��
1. �������2-��3� 4-�52. �������� �78-94��3. -3��;���� -�5�;�4. 2�7�-9,��� �3���4. 2�7�-9,��� �3���
3
1. �������2-��3� 4-�5����������� �!" #$"%&$!'(�)*+,�)-'./!0)12�����$3�,0�-� �0,)�4�14�1!" #$561741- (Programming) �() 4�1!" #$�����������,/#=�>��)-'./!0)12=�>�*�=�>�+$%�� !�?$ FORTRAN, COBOL, PASCAL, C, C++,C++, Java !6N$0,$ =�>�*�=�>�+$%�� !�?$ FORTRAN, COBOL, PASCAL, C, C++,C++, Java !6N$0,$ "�&$0)$����,�#OP0,)�- 4�176Q*+,!6N$=�>�� ��)-'./!0)12!",�*O!� #4?)$
4
*$=�>�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
2. ���������������� �78-94��
*$+$%��561741-)�O61P4)U�,/#+Q�#ST�42��$4V3�, 70?QPST�42��$OP0�&��(�)/?�)P314V3�,561741-=�>�R 'Q��'Q��� ���-�1W�����$ (Execute) 3�,OP0,)�- )#?��$,)# 1 ST�42��$ �(�)/?� main
5
int main(void){
�=����>?9�5@�� main
int main(){
,�7-
{
return 0;}
.
..
,�� ,4;
{
return 0;}
.
..
- 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
1. ���9 Source File : - 3SQ2",)�/�- (Text File) � �W\4!" #$"%&$0�- Syntax ")�=�>�R 'Q��'Q��
6
��8�4-������9�����������������������
- 3SQ2",)�/�- (Text File) � �W\4!" #$"%&$0�- Syntax ")�=�>�R 'Q��'Q��- - $�-�4�Q!6N$ .cpp
7
�)-3'!Q)12=�>�R 'Q��'Q�� (C++ Compiler) OP76Q�",)�/�-� �)#\?*$ Source File *+,4Q�#!6N$1+��� �!�1(�)��)-'./!0)12!",�*O7QP!4VU3/,*$) 43SQ2+$%��!1 #4/?� Object File
2. ������� (Compile)
+$%��!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 File1/-ST�42��$-�01h�$!+Q?�$�&$!",�4�U Object File^Q")�4�1Q.��2 OP3�,!6N$3SQ2� ������$3�, (Execute File)
8
C++ Program EnvironmentC++ Program Environment
Object File Computer Execute FileComputer
C++ Compiler Linker C++ Library
Source File
9
2���9���@;�2���9
2���9 (Statement)�() "�&$0)$*$4�1�����$+$%��"�&$0)$ ��4�������OP0,)�OU�,/#!�1(�)�+-�# ; !�?$�() "�&$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
0�/761 �() �(�)� � \̂,!" #$561741-0�&�"%&$-� !'(�)*�,),��).�'(&$� �!4VU",)-\Q*$+$?/#�/�-O��
4����� (Variable)
+$?/#�/�-O��"$��")�0�/761OP"%&$)#\?4�U�$.�0�/761 !�?$ int, char, float, double 0�/761 0,)�)#\?����,�$"/�")�!�1(�)�+-�#!�?�4�U (=) !�-)
����� !"#$ $%#"&&'()*%+ '�,� (X�45)
char 0�/)�4>1 1 0�/ 1char 0�/)�4>1 1 0�/ 1
int O��$/$!0V- 4
float O��$/$O1.� (�n$.#- 6 0��7+$?�) 4
double O��$/$O1.� (�n$.#- 12 0��7+$?�) 8
11
@�3�4����� (Variable Type) (con.)
char -> !6N$0�/761� �*�,���+1�U!4VU",)-\Q� �!6N$0�/)�4>1"$�� 10�/)�4>1 5�#*�, 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?tint -> !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)!�.- (-�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
Double -> !6N$0�/761� �!4VU",)-\Q� �!6N$!Q"�n$.#-!+-()$4�U float 70?OP*�,'(&$� �*$
@�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+308unsigned -> 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 intunsigned int
13
��4�89@7�-4�����
)�4>10�/714OP0,)�!6N$0�/)�4>1=�>�)��4{> +1()!�1(�)�+-�# _ (under scroll )�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!QV47QP0�/'.-'2*+}?W()!6N$�$QP0�/4�$ !�?$ name 4�U NAME0�/'.-'2*+}?W()!6N$�$QP0�/4�$ !�?$ name 4�U NAME
14
��4�89@7�-4�����
auto default float register struct volatile void+,�-0�&��(�)R&��4�U�����/$ (Keywords) R%��!6N$���� �- )#\?7Q,/*$=�>�R 'Q��'Q�� !�?$auto default float register struct volatile voidbreak do for return switch while static case double goto short typedef char longelse if signed union const enum externint sizeof unsigned continue
0�/)#?��!�?$0�/)#?��!�?$ number student_id
2value floatgrade1 _score
15
������.4�����
4?)$� �OP*�,��$0�/761*�] OP0,)�- 4�161P4�n0�/761��4�1�&�1\67UU4�161P4�n0�/761:1\67UU4�161P4�n0�/761:
type identifier;
5�#� � type �() �$.�",)-\Q� �0�/761$�&$OP!4VU3/,*$+$?/#�/�-O���)-'./!0)12 identifier �() �(�)0�/761
4��-�R9 @R�4��-�R9 @R�int number;char grade;
16
������.4����� (4R-)
+�40,)�4�161P4�n0�/761� �- �$.�!� #/4�$+Q�#] 0�/ ��-�1W!" #$3�,*$�������!� #/5�#*�,!�1(�)�+-�# , ���$1P+/?��0�/761 !�?$5�#*�,!�1(�)�+-�# , ���$1P+/?��0�/761 !�?$
type identifier1, identifier2, `;
int width;int high;
+1() int width, high ;+1() int width, high ;
17
���,��2Ra,����4�����
*�,!�1(�)�+-�# = 0�/)#?��!�?$0�/)#?��!�?$
int score;score = 80;
+-�#W%� 4��+$�*+,0�/761 score - �?�!�?�4�U 80O�4�������0?)36$ &
int a,b,c;int a,b,c;a = 2;b = 3;c = a + b;
O�+��?�")�0�/761 c C = C = 55
18
O�4�������0?)36$ & O�4�������0?)36$ &
4��-�R9���,��2Ra,����4�����
O�4�������0?)36$ &int a,b;a = 2;a = 5;b = a + 4;
O�4�������0?)36$ &int a;a = 2;a = a + 3;
b=b=66
oror
b=b=99
a=a=22
oror
a=a=55
O�+��?�")�0�/761 b O�+��?�")�0�/761 a
19
4��-�R9���,��2Ra,����4�����
int a;▫ �/�-+-�#�() 61P4�n0�/761 a !6N$0�/761� �*�,���+1�U!4VU�?�� �!6N$!Q"O��$/$▫ �/�-+-�#�() 61P4�n0�/761 a !6N$0�/761� �*�,���+1�U!4VU�?�� �!6N$!Q"O��$/$!0V-� �- �?�)#\?1P+/?�� -2147483648 W%� 2147483649
int a, b, c, d ;▫ �/�-+-�#�() 61P4�n0�/761 a, b, c, d !6N$0�/761� �*�,���+1�U!4VU�?�� �0�/!Q"O��$/$!0V-� �- �?�)#\?1P+/?�� -2147483648 W%� 2147483649 !1���-�1W61P4�n0�/761 �$.�!� #/4�$3�,+Q�#] 0�/*$4�161P4�n0�/761�1�&�+$%��]
unsigned M;▫ �/�-+-�#�() 61P4�n0�/761 M *+,!6N$0�/761�$.� unsigned intunsigned M;▫ �/�-+-�#�() 61P4�n0�/761 M *+,!6N$0�/761�$.� unsigned intint num1=8;▫ �/�-+-�#�() 61P4�n0�/761 num1 !6N$0�/761� �!4VU�?�0�/!Q"O��$/$!0V- 5�#*+,- �?�!1.�-0,$!�?�4�U 8
20
4��-�R9���,��2Ra,����4�����
float money, price ;▫ �/�-+-�#�() 61P4�n0�/761 money 7QP price *+,!6N$0�/761� �*�,���+1�U!4VU▫ �/�-+-�#�() 61P4�n0�/761 money 7QP price *+,!6N$0�/761� �*�,���+1�U!4VU�?�� �!6N$!Q"�n$.#-3�,3-?!4.$ 6 +Q�4
char ch=tAt;▫ �/�-+-�#�() 61P4�n0�/761 ch !6N$0�/761� �!4VU�?�0�/)�4>1!' #� 1 0�/ �() 0�/)�4>1 rAt
unsigned long int test;▫ �/�-+-�#�() 61P4�n0�/761 test !6N$0�/761� �*�,���+1�U!4VU�?�� �!6N$!Q"O��$/$!0V-7UU#�/� �3-?- 4�1�.�!�1(�)�+-�#�/�-+-�#�() 61P4�n0�/761 test !6N$0�/761� �*�,���+1�U!4VU�?�� �!6N$!Q"O��$/$!0V-7UU#�/� �3-?- 4�1�.�!�1(�)�+-�#
21
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
����b�a����,��4�����
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�P (Public) W,�3-?- 4�14��+$��?�!1.�-0,$")�0�/761!)�3/,OP- �?�!1.�-0,$!6N$ 02. Local Variable OP!6N$4�14��+$�0�/7613/,!�'�P=�#*$4Q�?-�������*��������+$%��+1()ST�42���$*�ST�42���$+$%�� R%��OP��-�1W!1 #4*�,��$0�/7613�,!�'�P=�#*$4Q�?-�������+1()ST�42���$!�?�$�&$ ST�42���$)(�$OP3-?��-�1W$��36*�,��$3�,!'1�P61P4�n*$Q�4>�P+1()ST�42���$!�?�$�&$ ST�42���$)(�$OP3-?��-�1W$��36*�,��$3�,!'1�P61P4�n*$Q�4>�P�?/$0�/ (Private) +1()!�'�P� �
,�� ,4; : 41� �(�)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�,
Operator Operator
!� #/ +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>�2� �*�,*$4�141P���4�U",)-\Q !�?$ +, -, *, /
2 + 3 a - b
Operands
2 + 3 a - b
Operands
((3 3 + + 55))
Operator
23
4��������g��R-� (Preprocessor)
�?/$� ��)-3'!Q)12OP0,)����4?)$� �OP!1.�-�)-3'Q2561741-$��+$,��,/#!�1(�)�+-�# #$��+$,��,/#!�1(�)�+-�# #0�/)#?��!�?$▫ #include <iostream.h> ()#\?=�#*$!�1(�)�+-�# <>) OP!6N$4�1*�,3SQ23QU1�1 iostream R%��!6N$3SQ23QU1�1 -�01h�$ 7QP- ST�42��$4�11�U�?�).$'��(cin) 7QP4�17���^Q!)��2'�0(cout)▫ #define *�,4��+$��(�)"%&$-�*+-? $��-�*�,4��+$��?���� � !�?$ #define PI 3.14 R%��!6N$4�14��+$��?�*+,4�U PI !�?�4�U 3.14
#include #define #if #program#endif #error #ifndef #undef#elif #else #ifdef
R%��!6N$4�14��+$��?�*+,4�U PI !�?�4�U 3.14
24
#include <filename> - searches standard library for file (use
#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 “filename” - searches current directory, then standard library (use for user-defined files)
+-�#!+0�+-�#!+0�▫ W,�������� #include <> ()#\?*$!�1(�)�+-�# <>) OP*�,3SQ23QU1�1 -�01h�$▫ 70?W,�0�-�,/# #include � � OP*�,3SQ2!��!�,)� ��1,��"%&$-�!)�
25
#include
#include <iostream.h>▫ *+,$��3SQ2 stdio.h -�O�43�!14�)1 � �3�,4��+$�3/,7Q,/ (/usr/include) !",�▫ *+,$��3SQ2 stdio.h -�O�43�!14�)1 � �3�,4��+$�3/,7Q,/ (/usr/include) !",�-�1?/-*$4�1�)-3'Q2
#include "sample.h"▫ *+,$��3SQ2 �sample.h� O�43�!14�)1 6TOO�U�$+1()3�!14�)1 � �4��+$�3/,7Q,/ !",�-�1?/-*$4�1�)-3'Q2▫ *�,*$41� � �- 4�1�1,��3SQ2 .h "%&$-�!)�
26
#define
*�,*$4�14��+$��?���� � !'(�)�/�-�P�/4*$4�1!" #$561741-0�/)#?��!�?$0�/)#?��!�?$
▫ #define PI 3.14159
+-�#W%� 4��+$��?���� ��(�) PI ���+1�U*�,7�$�?� 3.14159
▫ #define START_STR "Welcome to my program."
+-�#W%� 4��+$��?���� ��(�) START_STR ���+1�U*�,7�$",)�/�-/?� Welcome to my program.+-�#W%� 4��+$��?���� ��(�) START_STR ���+1�U*�,7�$",)�/�-/?� Welcome to my program.
27
�� ����2-i3��(Comment)
- 2 /._ �()▫ /*.............*/ : long comment▫ /*.............*/ : long comment▫ //............. : short comment*�,*$4�1)_.U�#561741-",)�/�-� �W\4 comment �)-3'!Q)12OP3-?$��-�1?/-*$4�1�)-3'Q2
28
4�14��+$��?���� �
4�1!" #$���)_.U�# (comment)
4�14��+$��?���� �
29
3. -3��;���� -�5�;� (Input and Output)).$'�� (Input)▫ 4�11�U�?�",)-\QO�4 \̂,*�,!",�36*$!�1(�)��)-'./!0)12▫ )�641�2).$'��-�01h�$ (Standard Input) :- � #2U)12�, !-��2▫ )�641�2).$'��-�01h�$ (Standard Input) :- � #2U)12�, !-��20�/)#?��!�?$▫ 4�17���",)�/�- +1()�?�0�/761*�] *+, \̂,*�,3�,1�U1\,▫ )�641�2!)��2'��-�01h�$ (Standard Output) :- +$,�O), !�1(�)�'.-'2, Q��5'�
30
>?9�5@��a������9g���-�=�
cout0,)� include 3SQ2 iostream.h0,)� include 3SQ2 iostream.h0�/)#?��!�?$
���/8�������a,�R
g����i5Hello 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. quotation marks.
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.
return is a way to exit a function from a function.
return 0, in this case, means that the program terminated normally.
32
Printing a Line of Text
cout▫ Standard output stream object▫ 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”;▫ cout << “Welcome to C++! \ n”;\▫ Escape character ▫ Indicates that a �special� character is to be output
33
Printing a Line of Text
Escape Sequence
DescriptionSequence
\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.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��-�R9
g����i5The volume of the box is 24
35
2��������i5��,�R9cout ���4��������float
4�17����?�")�0�/7617UU float $�&$ OP7����?�3�,3-?!4.$ 6 digits+�40,)�4�17����?�")�0�/761 *+,-�44/?� 6 digits 0,)�*�,�������+�40,)�4�17����?�")�0�/761 *+,-�44/?� 6 digits 0,)�*�,�������setprecision(digit_number) 7QP0,)� include<iomanip>
9 9 digitsdigits
f =123.4f1=123.456f2=123.457f3=1.23457e+08
6 6 digits => digits => 2+'3445,+ 62+'3445,+ 6
36
#include <iostream.h>#include <conio.h>#include<iomanip>int main()int main(){
float f=123.4,f1=123.456,f2=123.456789,f3=123456789 ;cout<< setprecision(8) <<f<<“\n”);cout<< setprecision(8) <<f1<<“\n”);cout<< setprecision(8) <<f2<<“\n”);cout<< setprecision(8) <<f3<<“\n”);getch();return 0;
}g����i5
f=123.4f1=123.456f2=123.45679f3=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)#\?!�-)()�}61P4�n +1() Double Quote) Q,)-1)U)#\?!�-)*�,0�/761�$.� char *$4�1!4VU",)-\Q 70?0,)�- 4�14��+$�O��$/$0�/)�4>1�,/# !�?$▫ char name[10];▫ !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��-�R9��a@�4�������� string
g����i5My name is Mallika
39
>?9�5@��a������2R��-�=�
cin0,)� include 3SQ2 iostream.h0,)� include 3SQ2 iostream.h0�/)#?��!�?$
40
2�7�-9,��� �3��� (Operators)
0�/���!$.$4�1�����.0n��012 (Arithmetic Operators) 0�/���!$.$4�1��-'�$_2 (Relational 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�- ,����� �=������a@� 4��-�R9 g����i5* �\� $.'O$2714 * $.'O$2�)� 5 * 3 15* �\� $.'O$2714 * $.'O$2�)� 5 * 3 15/ +�1!)�!n> $.'O$2714 / $.'O$2�)� 5 / 3 1% +�1!)��?/$ $.'O$2714 % $.'O$2�)� 5 % 3 2+ U/4 $.'O$2714 + $.'O$2�)� 5 + 3 8- QU $.'O$2714 - $.'O$2�)� 5 - 3 2QU $.'O$2714 - $.'O$2�)� 5 - 3 2
42
�����-94��� �3���
@7�- ,�����() R,�#36"/�() R,�#36"/�
* / % R,�#36"/�+ - R,�#36"/�
4��-�R9 O�+��?�")�$.'O$20?)36$ &
4410 + 2 * 3 * - 1 _________44
--55
1515
5 * 3
10 + 2 * 3 * - 1 _________
2 * 5 % 8 + -7 _________
(3 + 5/2)*(7 - 3 - 5%2) _________
43
4��-�R9
c9,2R�-9�3�c�54R-X���8�,��a,� �,��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 – e * g;
2. x = (f * b) – a % c + d * h;2. x = (f * b) – a % c + d * h;
3. x = ((g * h)+a)- e % d * f + b – c;
44
@7�- ,����� �=������a@� 4��-�R9 g����i5
�- �-�5 � 4-�5�� �3��2R�����2R
@7�- ,����� �=������a@� 4��-�R9 g����i5
++ �3����,��9 lvalue++ x=1; x++; x=1
-- ����,��9 lvalue-- x=1; x--; x=1
++ �3���R-� ++lvalue x=1; ++x; x=2
-- ���R-� --lvalue x=1; --x; x=0-- ���R-� --lvalue x=1; --x; 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��-�R9 c9 ����g����i5�-9�������4R-X���8
g����i5a= 2 b= 4 c=1b= 3 a=3b= 4 a=3
46
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�?�
�- �-�5 � 4-�5�� �R��� (Equality Operator)
7QPW\4O��/?�!6N$5)!6)12!1!0)123U$�1 ���$�&$ OP0,)�*�,�)�$.'O$2!'(�)!61 #U!� #U�?�
@7�-�- �-�5- � 4-�5
,����� �=������a@� 4��-�R9 g����i5
== !61 #U!� #U/?�0,)�!�?�4�$ $.'O$2714 == $.'O$2�)� 5 == 5 true
!= !61 #U!� #U/?�0,)�3-? $.'O$2714 != $.'O$2�)� 5 != 5 false!= !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 7QPW\4O��!6N$5)!6)12!1!0)123U$�1 ���$�&$OP0,)�*�,�)�$.'O$2!'(�)!61 #U!� #U�?� W\4O��!6N$5)!6)12!1!0)123U$�1 ���$�&$OP0,)�*�,�)�$.'O$2!'(�)!61 #U!� #U�?�
@7�-�- �-�5- � 4-�5
,����� �=������a@�4��-�R9
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<= $,)#4/?�+1()!�?�4�U $.'O$2714 <= $.'O$2�)� a<=b false
> -�44/?� $.'O$2714 > $.'O$2�)� a>b true
>= -�44/?�+1()!�?�4�U $.'O$2714 >= $.'O$2�)� a>=b true
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�- ���� �=������a@�
! not !$.'O$2
&& and $.'O$2714 && $.'O$2�)�
|| or $.'O$2714 || $.'O$2�)�
!�?�$�&$ 3-?��-�1W*�,�$.�",)-\Q)(�$ ] 3�,
|| or $.'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+-�$)4$�&$^QQ�'_2OP!6N$!�VO+-�
2R�3�c�5��� 2R�3�c�5�-9 g����i5
true true true
true false false
false true falsefalse true false
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.�+-�$)4$�&$^QQ�'_2OP!6N$O1.�+-�
2R�3�c�5��� 2R�3�c�5�-9 g����i5
true true true
true false true
false true truefalse true true
false false 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� 2�7�-9,�� �3.���9�
() R,�#36"/�* / % R,�#36"/�
+ - R,�#36"/�< > <= >= == != R,�#36"/�
&& || R,�#36"/�&& || R,�#36"/�
53
4��� �3�������-� (Compound Operator)
!6N$1\6#?)")�0�/���!$.$4�17QP0�/761� �W\4���!$.$4�10�/)#?��!�?$
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��-�R9 @R� O�4�������0?)36$ &
0�/)#?��!�?$
4��-�R9 @R� O�4�������0?)36$ &int num1 = 3, num2 = 1;num1 *= 4;num2 += num1 - 2;
O�+��?�")�0�/761 num2
g����i5num = 11
54
�� ���������92Rg����i5 �t�@�3�4�����a,�R (Casting)
1\67UU▫ (�$.�0�/761) 0�/761+1()$.'O$2+1()�?���� �▫ (�$.�0�/761) 0�/761+1()$.'O$2+1()�?���� �0�/)#?��!�?$
float div;int a = 1, b = 2;div = (float)a/b;
g����i50.5
55
1. !" #$561741-=�>�R *$4�11�U",)-\Q R%��61P4)U�,/# �(�) $&��+$�4 �/�-�\� 7QP"$��1)�!�,�")�$�4n%4>� 5�#*�,ST�42���$ cout '.-'2�?�))4� �+$,�O) 5�#'.-'2
���uv�,��
"$��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��-�R9������������
Please enter your name: AjanO Jaidee
Weight: 52.5Weight: 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 B5�#*�,0�/761 C *$4�1O��4�1 7Q,/'.-'2^QQ�'_2�?� A 7QP B ))4���O)=�') 4�1�&�
4��-�R9������������
A: 18B: 7Before swapping A = 18, B = 7After swapping A = 7, B = 18After swapping A = 7, B = 18
57
4��-�R9������������
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) : __Cal(Input 1 % Input 2) : __