Chapter 3 Computer Networking

Download Report

Transcript Chapter 3 Computer Networking

Chapter 5
Elementary C++
Programming
Dept of Computer Engineering
Khon Kaen University
Major Concepts








แนะนำ C++ เบื้องต้น
โอเปอร์เรเตอร์ Input/Output
Characters และ Literals
ตัวแปรและกำรประกำศ
Tokens ของโปรแกรม
กำรกำหนดค่ำเริ่ มต้นของตัวแปร
ออปเจ็กต์, ตัวแปรและค่ำคงที่
คำสงวนและคำเฉพำะ (Reserved Words and
Identifiers)
178110: Computer Programming (II/2546)
2
ประวัติของภำษำ C++




BCPL (Basic CPL) สร้ำงโดย Martin Richards ในปี
1969. (CPL= Combined Programming
Language.)
ภำษำ B สร้ำงโดย Ken Thompson ในปี 1970 สำหรับระบบ
UNIX
C สร้ำงโดย Dennis Ritchie แห่ง AT&T Bell Lab ใน
ทศวรรษที่ 1970s.
C++ สร้ำงโดย Bjarne Stroustrup แห่ง AT&T Bell
Lab ในช่วงต้นทศวรรษที่ 1980s. ( C with Classes )

C++ ได้กำรรับรองมำตรฐำนในปี 1998. (ISO/IEC 14882:1998)
178110: Computer Programming (II/2546)
3
กำรพัฒนำ a C++ program
Text editor
prog.cpp
Compiler
prog.obj
Linker
prog.exe
ใช้ text editor
เขียนโปรแกรม prog.cpp
ใช้ compiler ซึ่งเป็ นโปรแกรม
เปลี่ยนโปรแกรมภาษา C++
เป็ นออปเจ็กต์ไฟล์
ใช้ linker ในการเชื่อมต่อ
ออปเจ็กต์ไฟล์ต่าง ๆ เข้าด้วยกัน
ให้เป็ นไฟล์ที่สามารถรันได้
4
178110: Computer Programming (II/2546)
Compile & run C++ programs




Editor ที่ใช้คือ Crimson Editor
Compiler ที่ใช้เป็ น Borland C++ ในเวอร์ ชนั่ command
line
ในกำรติดตั้ง compiler, อ่ำนไฟล์
\Borland\bcc55\readme.txt ซึ่ งไฟล์น้ ีจะบอกว่ำคุณต้องแก้ไข
ไฟล์ bcc32.cfg และ ilink32.cfg ซึ่ งอยูใ่ น directory เดียวกัน
กำร compile โปรแกรมให้ใช้คำสั่ง


bcc32 prog.cpp
จำกนั้นให้ run program prog.exe

prog.exe
178110: Computer Programming (II/2546)
5
องค์ประกอบพื้นฐำนของ C++
Comments
 Compiler directive #include
 using namespace
 Main function definition
 Variable declarations
 Executable Statements

178110: Computer Programming (II/2546)
6
ตัวอย่ำงโปรแกรม 1
// Example 1
// The “Hello, World!” Program
#include <iostream>
int main()
{
std::cout << “Hello, World!\n”;
}
178110: Computer Programming (II/2546)
7
Comments

จุดประสงค์
ใช้เพื่อเขียนกำกับควำมหมำยของคำสัง่ ในโปรแกรมเพื่อให้เข้ำใจ
โปรแกรมง่ำยขึ้น
 ช่วยเตือนควำมจำเมื่อมำดูโปรแกรมทีหลัง


รู ปแบบของ comments
// comment
 /* comment */

178110: Computer Programming (II/2546)
8
Comments (Cont.)

ตัวอย่ำงของ comments







// This text1 is a comment or not
/* This text2 is a comment or not */
/* /* This text3 is a comment or not */
/* /* This text4 is a comment or not */ */
// // This text5 is a comment or not
// /* This text6 is a comment or not
All lines are legal comments, except text4

Comments cannot be nested
178110: Computer Programming (II/2546)
9
Compiler Directive: #include


จุดประสงค์
 ใช้เพื่อบอกให้ compiler รู ้วำ่ ในโปรแกรมของเรำจะต้องใช้ไฟล์อะไรใน
กำรหำนิยำมของตัวแปรและฟั งก์ชนั ที่ใช้
รู ปแบบ
 ถ้ำ header file เป็ น standard or system file
#include <filename>
 ถ้ำ header file เป็ น fileที่เรำเขียนขึ้น (user defined)
#include “filename”
178110: Computer Programming (II/2546)
10
Compiler Directive (Cont.)

ตัวอย่ำงของ compiler directives
// cin and cout are defined in ‘iostream.h’
// which is already in the computer when
// we install a compiler
#include <iostream>
// myfile.h เป็ น fileที่เรำเขียนขึ้นให้เรำใช้เอง
#include “myfile.h”
178110: Computer Programming (II/2546)
11
Using Namespace




Namespace คือชื่อที่ใช้เรี ยกกลุ่มนิยำมต่ำง ๆ ที่ถูกจัดให้อยูใ่ น
กลุ่มเดียวกัน
Group a set of classes, objects, and/or
functions under a name
Split the global scope in sub-scopes
known as namespaces
กำรใช้ namespaces จะเป็ นประโยชน์มำกสำหรับกรณี ที่มี
global object หรื อ function ที่มีชื่อเดียวกัน
178110: Computer Programming (II/2546)
12
Using Namespace (Cont.)

// namespaces
#include <iostream.h>
namespace first { int var = 5; }
namespace second { double var = 3.1416; }
int main ()
{ cout << first::var << endl;
cout << second::var << endl;
return 0; }
178110: Computer Programming (II/2546)
13
Using Namespace (Cont.)
// Using Namespace
// Another "Hello, World" Program
#include <iostream>
using namespace std;
int main()
{ // prints "Hello, World!":
cout << "Hello, World!\n";
return 0;
}
178110: Computer Programming (II/2546)
14
Using Namespace (Cont.)
In order to access these variables
from outside the namespace we
have to use the scope operator ::
 Previously, we have std::cout in
ตัวอย่ำงโปรแกรม 1

178110: Computer Programming (II/2546)
15
ตัวอย่ำงโปรแกรม 1
// The “Hello, World!” Program
#include <iostream>
int main()
{
std::cout << “Hello, World!\n”;
}
178110: Computer Programming (II/2546)
16
Using Namespace (Cont.)


The using directive followed by
namespace serves to associate the
present nesting level with a certain
namespace so that the objects and
functions of that namespace can be
accessible directly
Example:
using namespace std;
std is the namespaceที่มีคำนิยำมของ
variables และ functions ของ C++ standard library
178110: Computer Programming (II/2546)
17
ตัวอย่ำงโปรแกรม 2
// Using Namespace
// Another "Hello, World" Program
#include <iostream>
using namespace std;
int main()
{ // prints "Hello, World!":
cout << "Hello, World!\n";
return 0;
}
178110: Computer Programming (II/2546)
18
นิยำมของfunction main

โปรแกรมภำษำ C++ จะต้องประกอบด้วยฟังก์ชนั่ ทีชื่อ main()
เสมอ



เพื่อคอมพิวเตอร์ จะได้ทรำบว่ำจะเริ่ มจำกฟั งก์ชนั่ ใดก่อน
โปรแกรมภำษำ C++ จะเริ่ มต้นทำงำนในฟังก์ชนั่ main() ก่อน
เสมอ
ถ้ำหำกโปรแกรม run ได้อย่ำงถูกต้อง ค่ำที่ควรจะส่ งออกจำก
function main คือค่ำ 0

return 0;
178110: Computer Programming (II/2546)
19
Function main (Cont.)
#include <iostream>
using namespace std;
int add(int a, int b)
{
int c = a + b;
return c;
}
int main()
{
int result = add(2,5);
cout << “result is “ << result << endl;
return 0;
178110: Computer Programming (II/2546)
}
20
Declaration Statements

Variable declaration


เป็ นคำสัง่ ประกำศซึ่ งเป็ นกำรบอกให้ compiler รู้วำ่ มี
object ใดบ้ำงที่จะใช้ในโปรแกรมและบอกประเภทของ
object
Function declaration
 เป็ นคำสัง
่ ประกำศซึ่งเป็ นกำรบอกว่ำ function นั้นมีตวั แปรที่
เข้ำมำนั้นเป็ นตัวแปรในประเภทใดและตัวแปรที่ออกไปนั้นเป็ นตัว
แปรประเภทใด
178110: Computer Programming (II/2546)
21
Declaration Statements (Cont.)
 ตัวอย่ำงของ variable declaration
int a; // declare a as an integer
 char c; // declare c as a character


ตัวอย่ำงของ function declaration
int add(int a, int b);
// add accepts 2 integers as inputs
// and produces an integer as an
// output

178110: Computer Programming (II/2546)
22
Executable Statements


Executable statements คือคำสัง่ ที่ทำให้เกิดกำรทำงำนขึ้น
เมื่อมีกำร run โปรแกรม แต่ละคำสัง่ ต้องจบด้วยเครื่ องหมำย ;
ตัวอย่ำงของ executable statements
cout << “Enter two integers:”;
cin >> m >> n;
m = m + n;
cout << “m= “ << m << “, n=“ << n << endl;
return 0;
178110: Computer Programming (II/2546)
23
Return statement


เป็ นกำรสั่งให้ส่งกำรทำงำน (หรื อกำรควบคุม) ออกจำกฟังก์ชนั กลับไปยังจุดที่มี
กำรเรี ยกใช้งำนฟั งก์ชนั นั้น ๆ พร้อมกับให้ส่งค่ำที่ระบุอยูห่ ลังคำ return
กลับไป (ถ้ำมี)
รู ปแบบ:


return expression;
ตัวอย่ำง



return 0;
return base*height/2.0;
return;
178110: Computer Programming (II/2546)
24
The Shortest C++ Program
int main()
{
return 0;
}


ไม่มี program input และไม่มี program output
We can compile and run this simple
program successfully
178110: Computer Programming (II/2546)
25
Input and Output Operations


Stream หมำยถึงลำดับของอักขระที่เชื่อมโยงอยูก่ บั อุปกรณ์ input หรื อ
อุปกรณ์ output หรื อ ไฟล์ที่อยูใ่ นจำนแม่เหล็ก
Library iostream ได้ให้นิยำมต่อไปนี้





cin เป็ น object ของ class istream ที่เชื่อมโยงกับอุปกรณ์ input
มำตรฐำนซึ่งคือแป้ นพิมพ์
cout เป็ น object ของ class ostream ที่เชื่อมโยงกับอุปกรณ์ output
มำตรฐำนซึ่งคือ จอภำพ
Input operator คือ >>
Output operator คือ <<
#include <iostream> หำกต้องกำรใช้ classes
and operators in library iostream
178110: Computer Programming (II/2546)
26
The output operator



เครื่ องหมำย << เรี ยกว่ำ output operator หรื อ
insertion operator
<< จะทำกำรบรรจุค่ำที่อยูท่ ำงด้ำนขวำ (หรื อทำงด้ำนหลัง) ของ
เครื่ องหมำยเข้ำไปใน output stream (cout) ซึ่งมีชื่อ
ปรำกฏอยูท่ ำงด้ำนซ้ำย (หรื อทำงด้ำนหน้ำ) ของเครื่ องหมำย
ตัวอย่ำง
cout << “178110”;
คำสัง่ นี้ทำให้ค่ำ 178110 แสดงออกที่จอภำพ

178110: Computer Programming (II/2546)
27
The Output Operator (Cont.)

รู ปแบบ:


ตัวอย่ำง:




cout << variable;
cout << “Enter two integers:”;
cout << “m = “ << m << “, n = “ << n
<< “m + n = “ << add(m,n) << endl;
endl (end of line) ทำให้ cursor เลื่อนไปบรรทัดใหม่
variable จะเป็ นค่ำจำกตัวแปร หรื อจำกค่ำโดยตรง หรื อจำกค่ำที่
return จำก function ก็ได้
178110: Computer Programming (II/2546)
28
ตัวอย่ำงโปรแกรม 3

// Another “Hello, World!” program
#include <iostream>
int main()
{
cout << “Hel” << “lo, Wo” << “rld!”
<< endl;
return 0;
}

endl คือ stream manipulator ซึ่ งให้ผลเช่นเดียวกับตอนที่เรำเขียน
\n
178110: Computer Programming (II/2546)
29
The Input Operator



เครื่ องหมำย >> เรี ยกว่ำ input operator หรื อ
extraction operator
>> จะรับค่ำจำก input stream ซึ่งปกติแล้วคือ object
cin ซึ่งเป็ น object ที่ได้รับจำกแป้ นพิมพ์ แล้วค่ำที่ได้จำก
input stream จะถูกส่ งต่อไปเป็ นค่ำของ variable ที่อยู่
ทำงด้ำนขวำมือของ >>
cin สำมำรถใช้ได้กบั ข้อมูลที่เป็ น integers, floatingpoint numbers, characters, booleans, และ
strings
178110: Computer Programming (II/2546)
30
The Input Operator (Cont.)



รู ปแบบ:
cin >> variable
ตัวอย่ำง:
int a;
double b;
char c;
cin >> a >> b >> c;
จะแยกค่ำของ variables แต่ละตัวได้อย่ำงไร
 พิมพ์ space หรื อ กดปุ่ ม enter
178110: Computer Programming (II/2546)
31
Characters and Literals

Each literal consists of a sequence of
characters delimited by quotation marks



Examples: “Hello, World!”, “Hel”, “lo, Wo”
A character is an elementary symbol used
collectively to form meaningful writing
Characters consist of letters, digits, and a
collection of punctuation marks

Examples: a, A, 0, 9, _
178110: Computer Programming (II/2546)
32
Nonprinting Characters




These characters begin with ‘\’
‘\n’: the newline character
‘\t’: the horizontal tab character
How do we want to print “ and \



Use \” to print “
Use \\ to print \
If we want to have “Hello, this is “Dee””,

cout << “Hello, this is \”Dee\””
178110: Computer Programming (II/2546)
33
Variables & Their Declarations



A variable is a symbol that represents a
storage location in the computer’s
memory
The information that is stored in that
location called the value of the variable
The assignment statement has the syntax
variable = expression;
178110: Computer Programming (II/2546)
34
Using Integer Variable
// Using integer variables
#include <iostream>
using namespace std;
// print “m = 44 and n = 77
int main()
{
int m, n;
m = 44; // assign the value 44 to variable m
cout << “m = “ << m;
n = m + 33; // assign the value 77 to variable n
cout << “ and n = “ << n << endl;
}

178110: Computer Programming (II/2546)
35
Variable Declaration





Every variable must be declared before it is
used. The syntax is
specifier type name initializer;
specifier is an optional keyword such as const
type is one of the C++ data types, such as int
name is the name of the variable
initializer is an assignment of the initialized value
of the variable
178110: Computer Programming (II/2546)
36
Variable Declaration (Cont.)

Example: const int m = 44;





const is a specifier
int is a data type
m is a variable name
= 44 is an initializer
Can many variables are declared in the
same line?


int m, n;
int m = 2, n = 3;
178110: Computer Programming (II/2546)
37
Variable Declaration (Cont.)

The purpose of a declaration is to
introduce a name to the program


To explain to the compiler the type of the
name (what kind of data that the name can
store)
The type tells the compiler


What range of values the variable may have
What operations can be performed on the
variable
178110: Computer Programming (II/2546)
38
Variable Scope



The location of the declaration determines
the scope of the variable
The scope of a variable extends from its
point of declaration to the end of the
immediate block in which it is declared or
which it controls
The term scope refers to the availability of
a variable or constant declared (or used)
in one part of a program to other parts of
a program.
178110: Computer Programming (II/2546)
39
Variable Scope (Cont.)
int add(int a, int b)
{
int c = a + b;
return c;
}
 At this point, variables a, b, and c are not known
int main()
{
return 0;
}
178110: Computer Programming (II/2546)
40
Program Tokens


The computer program is a sequence of
elements called tokens
These tokens include






Keywords, such as int
Identifiers, such as main
Punctuation symbols, such as {
Operators, such as <<
The compiler scans the text in your
source, parsing it into tokens
If it finds something unexpected, then it
issues error messages
178110: Computer Programming (II/2546)
41
Count the Tokens
int main()
{ // prints “n = 44”:
int n = 44;
cout << “n = “ << n << endl;
}
 How many tokens are there in this source?

19: “int”, “main”, “(“, “)”, “{“, “int”, “n”, “=“, “44”, “;”,
“cout”, “<<“, “n = “, “<<“, “n”, “<<“, “endl”, “;”, and
“}”
178110: Computer Programming (II/2546)
42
Initializing Variables
int main()
{
int m;
cout << “m = “ << m << endl;
}


What is the value of m printed out?
 The value depends on the compiler and the machine
 The value of a variable must be initialized before it is
used
Does the compiler generate an error message because
m is not initialized?
 No
178110: Computer Programming (II/2546)
43
Objects & Variables



An object is a contiguous region of
memory that has an address, a size,
a type, and a value
The address of an object is the
memory address of its first byte
The size of an object is simply the
number of bytes that it occupies in
memory
178110: Computer Programming (II/2546)
44
Objects & Variables (Cont.)




With GNU C++ on a UNIX workstation, the
object n defined by
int n = 22;
Its memory address is 0xbfbffb5c
 The memory address is a hexadecimal
number
 How many bits does this memory address
have?
Its size is 4 and its type is int
Its value is 22
178110: Computer Programming (II/2546)
45
Variables & Constants


Variable is an object that has name, and
the word “variable” is used to suggest that
the value of the object can be changed
However, we can declare an object to
have the constant value with the syntax
const type identifier = value;
Example: const int N = 22;
178110: Computer Programming (II/2546)
46
Constant Definitions

This program illustrates constant definitions







const
const
const
const
const
char BEEP = ‘\b’;
int MAXINT = 2147483647;
int N = MAXINT/2;
float KM_PER_MI = 1.60934;
double PI = 3.1415926535897 ;
We usually use all capital letters in constant
identifiers to distinguish them from other kinds
of identifiers
A good compiler will replace each constant
symbol with its numeric value
178110: Computer Programming (II/2546)
47
Reserved Words & Identifiers

Reserved words (or keywords) have
specific meanings that cannot be used for
other purposes
Reserved Words
Meaning
const
Declaration of a constant data
item
include
A compiler directive
namespace
Region where program elements
are defined
using
Indicate that a program is using
elements from a particular
namespace
178110: Computer Programming (II/2546)
48
All C++ Reserved Words
and
and_eq
asm
auto
bitand
Virtual
bool
volatile
struct
operator
extern
char
xor_eq
throw
true
try
typedef
default
inline
pret_cast
typename
delete
int
return
union
do
long short unsigned
void
double
mutable
signed
using
dynamic_cast
namespace
sizeof
bitor
else
new
static
enum
not
static_cast
break
explicit
not_eq
wchar_t
case
export
switch
while
catch
or
template
xor
false
or_eq
this
class
float
private
compl
for
protected
const
friend
public
const_cast
goto
register
continue
if
reinter typeid
178110: Computer Programming (II/2546)
49
Identifiers

Identifiers are names of variables and
objects manipulated by a program
Identifiers
Meanings
cin
C++ name for standard
input stream
Variables for storing integers
C++ name for the standard
namespace
m, n
std
178110: Computer Programming (II/2546)
50
Rules for Selecting Identifiers
Always begin with a letter or an
underscore symbol (_)
2.
Consists of letters, digits, or underscores
3.
Cannot be a reserved word
4.
Some compilers limit the number of
characters in identifier to 32
Note uppercase and lowercase letters are
viewed as different identifiers
1.
178110: Computer Programming (II/2546)
51
Examples of Invalid Identifiers







1Letter
float
const
Two*Four
Joe’s
two-dimensional
hello world
178110: Computer Programming (II/2546)
52
Choosing Identifier Names




Pick a meaningful name for an identifier
All capital letters are usually used for
constants, e.g., KM_PER_HOUR
Avoid selecting two identifier names that
are different only in their use of uppercase
and lowercase letter
A mistyped identifiers can cause an
undefined identifier error
178110: Computer Programming (II/2546)
53
Examples of Valid Identifiers

Valid identifiers without using underscore
letters:




wattHour
displayGrade
avgScore
Valid identifiers with the use of
underscore letters


max_score
total_students
178110: Computer Programming (II/2546)
54