งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Chapter 5 Elementary C++ Programming Dept of Computer Engineering Khon Kaen University.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Chapter 5 Elementary C++ Programming Dept of Computer Engineering Khon Kaen University."— ใบสำเนางานนำเสนอ:

1 Chapter 5 Elementary C++ Programming Dept of Computer Engineering Khon Kaen University

2 178110: Computer Programming (II/2546) 2 Major Concepts แนะนำ C++ เบื้องต้น โอเปอร์เรเตอร์ Input/Output Characters และ Literals ตัวแปรและการประกาศ Tokens ของโปรแกรม การกำหนดค่าเริ่มต้นของตัวแปร ออปเจ็กต์, ตัวแปรและค่าคงที่ คำสงวนและคำเฉพาะ (Reserved Words and Identifiers)

3 178110: Computer Programming (II/2546) 3 ประวัติของภาษา 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)

4 178110: Computer Programming (II/2546) 4 การพัฒนา a C++ program Text editor Compiler Linker prog.cpp prog.obj prog.exe ใช้ text editor เขียนโปรแกรม prog.cpp ใช้ compiler ซึ่งเป็นโปรแกรม เปลี่ยนโปรแกรมภาษา C++ เป็นออปเจ็กต์ไฟล์ ใช้ linker ในการเชื่อมต่อ ออปเจ็กต์ไฟล์ต่าง ๆ เข้าด้วยกัน ให้เป็นไฟล์ที่สามารถรันได้

5 178110: Computer Programming (II/2546) 5 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

6 178110: Computer Programming (II/2546) 6 องค์ประกอบพื้นฐานของ C++ Comments Compiler directive #include using namespace Main function definition Variable declarations Executable Statements

7 178110: Computer Programming (II/2546) 7 ตัวอย่างโปรแกรม 1 // Example 1 // The “Hello, World!” Program #include int main() { std::cout << “Hello, World!\n”; }

8 178110: Computer Programming (II/2546) 8 Comments จุดประสงค์ ใช้เพื่อเขียนกำกับความหมายของคำสั่งใน โปรแกรมเพื่อให้เข้าใจโปรแกรมง่ายขึ้น ช่วยเตือนความจำเมื่อมาดูโปรแกรมทีหลัง รูปแบบของ comments // comment /* comment */

9 178110: Computer Programming (II/2546) 9 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

10 178110: Computer Programming (II/2546) 10 Compiler Directive: #include จุดประสงค์ ใช้เพื่อบอกให้ compiler รู้ว่าในโปรแกรมของเรา จะต้องใช้ไฟล์อะไรในการหานิยามของตัวแปรและ ฟังก์ชันที่ใช้ รูปแบบ ถ้า header file เป็น standard or system file #include ถ้า header file เป็น fileที่เราเขียนขึ้น (user defined) #include “filename”

11 178110: Computer Programming (II/2546) 11 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 // myfile.h เป็น fileที่เราเขียนขึ้นให้เราใช้เอง #include “myfile.h”

12 178110: Computer Programming (II/2546) 12 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 ที่มีชื่อเดียวกัน

13 178110: Computer Programming (II/2546) 13 Using Namespace (Cont.) // namespaces #include namespace first { int var = 5; } namespace second { double var = 3.1416; } int main () { cout << first::var << endl; cout << second::var << endl; return 0; }

14 178110: Computer Programming (II/2546) 14 Using Namespace (Cont.) // Using Namespace // Another "Hello, World" Program #include using namespace std; int main() { // prints "Hello, World!": cout << "Hello, World!\n"; return 0; }

15 178110: Computer Programming (II/2546) 15 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

16 178110: Computer Programming (II/2546) 16 ตัวอย่างโปรแกรม 1 // The “Hello, World!” Program #include int main() { std::cout << “Hello, World!\n”; }

17 178110: Computer Programming (II/2546) 17 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

18 178110: Computer Programming (II/2546) 18 ตัวอย่างโปรแกรม 2 // Using Namespace // Another "Hello, World" Program #include using namespace std; int main() { // prints "Hello, World!": cout << "Hello, World!\n"; return 0; }

19 178110: Computer Programming (II/2546) 19 นิยามของfunction main โปรแกรมภาษา C++ จะต้องประกอบด้วย ฟังก์ชั่นทีชื่อ main() เสมอ เพื่อคอมพิวเตอร์จะได้ทราบว่าจะเริ่มจากฟังก์ชั่นใด ก่อน โปรแกรมภาษา C++ จะเริ่มต้นทำงานใน ฟังก์ชั่น main() ก่อนเสมอ ถ้าหากโปรแกรม run ได้อย่างถูกต้อง ค่าที่ควร จะส่งออกจาก function main คือค่า 0 return 0;

20 178110: Computer Programming (II/2546) 20 Function main (Cont.) #include 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; }

21 178110: Computer Programming (II/2546) 21 Declaration Statements Variable declaration เป็นคำสั่งประกาศซึ่งเป็นการบอกให้ compiler รู้ว่ามี object ใดบ้างที่จะใช้ใน โปรแกรมและบอกประเภทของ object Function declaration เป็นคำสั่งประกาศซึ่งเป็นการบอกว่า function นั้นมีตัวแปรที่เข้ามานั้นเป็นตัวแปร ในประเภทใดและตัวแปรที่ออกไปนั้นเป็นตัว แปรประเภทใด

22 178110: Computer Programming (II/2546) 22 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

23 178110: Computer Programming (II/2546) 23 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;

24 178110: Computer Programming (II/2546) 24 Return statement เป็นการสั่งให้ส่งการทำงาน (หรือการควบคุม) ออก จากฟังก์ชันกลับไปยังจุดที่มีการเรียกใช้งานฟังก์ชันนั้น ๆ พร้อมกับให้ส่งค่าที่ระบุอยู่หลังคำ return กลับไป (ถ้ามี) รูปแบบ: return expression; ตัวอย่าง return 0; return base*height/2.0; return;

25 178110: Computer Programming (II/2546) 25 The Shortest C++ Program int main() { return 0; } ไม่มี program input และไม่มี program output We can compile and run this simple program successfully

26 178110: Computer Programming (II/2546) 26 Input and Output Operations Stream หมายถึงลำดับของอักขระที่เชื่อมโยงอยู่กับ อุปกรณ์ input หรืออุปกรณ์ output หรือ ไฟล์ที่อยู่ใน จานแม่เหล็ก Library iostream ได้ให้นิยามต่อไปนี้ cin เป็น object ของ class istream ที่เชื่อมโยงกับอุปกรณ์ input มาตรฐานซึ่งคือแป้นพิมพ์ cout เป็น object ของ class ostream ที่เชื่อมโยงกับ อุปกรณ์ output มาตรฐานซึ่งคือ จอภาพ Input operator คือ >> Output operator คือ << #include หากต้องการใช้ classes and operators in library iostream

27 178110: Computer Programming (II/2546) 27 The output operator เครื่องหมาย << เรียกว่า output operator หรือ insertion operator << จะทำการบรรจุค่าที่อยู่ทางด้านขวา ( หรือ ทางด้านหลัง ) ของเครื่องหมายเข้าไปใน output stream (cout) ซึ่งมีชื่อปรากฏอยู่ทาง ด้านซ้าย ( หรือทางด้านหน้า ) ของเครื่องหมาย ตัวอย่าง cout << “178110”; คำสั่งนี้ทำให้ค่า 178110 แสดงออกที่จอภาพ

28 178110: Computer Programming (II/2546) 28 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 ก็ ได้

29 178110: Computer Programming (II/2546) 29 ตัวอย่างโปรแกรม 3 // Another “Hello, World!” program #include int main() { cout << “Hel” << “lo, Wo” << “rld!” << endl; return 0; } endl คือ stream manipulator ซึ่งให้ผลเช่นเดียวกับ ตอนที่เราเขียน \n

30 178110: Computer Programming (II/2546) 30 The Input Operator เครื่องหมาย >> เรียกว่า input operator หรือ extraction operator >> จะรับค่าจาก input stream ซึ่งปกติแล้วคือ object cin ซึ่งเป็น object ที่ได้รับจาก แป้นพิมพ์ แล้วค่าที่ได้จาก input stream จะ ถูกส่งต่อไปเป็นค่าของ variable ที่อยู่ทางด้าน ขวามือของ >> cin สามารถใช้ได้กับข้อมูลที่เป็น integers, floating-point numbers, characters, booleans, และ strings

31 178110: Computer Programming (II/2546) 31 The Input Operator (Cont.) รูปแบบ : cin >> variable ตัวอย่าง : int a; double b; char c; cin >> a >> b >> c; จะแยกค่าของ variables แต่ละตัวได้อย่างไร พิมพ์ space หรือ กดปุ่ม enter

32 178110: Computer Programming (II/2546) 32 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, _

33 178110: Computer Programming (II/2546) 33 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\””

34 178110: Computer Programming (II/2546) 34 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;

35 178110: Computer Programming (II/2546) 35 Using Integer Variable // Using integer variables #include 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; }

36 178110: Computer Programming (II/2546) 36 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

37 178110: Computer Programming (II/2546) 37 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;

38 178110: Computer Programming (II/2546) 38 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

39 178110: Computer Programming (II/2546) 39 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.variable or constant

40 178110: Computer Programming (II/2546) 40 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; }

41 178110: Computer Programming (II/2546) 41 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

42 178110: Computer Programming (II/2546) 42 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 “}”

43 178110: Computer Programming (II/2546) 43 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

44 178110: Computer Programming (II/2546) 44 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

45 178110: Computer Programming (II/2546) 45 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

46 178110: Computer Programming (II/2546) 46 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;

47 178110: Computer Programming (II/2546) 47 Constant Definitions This program illustrates constant definitions const char BEEP = ‘\b’; const int MAXINT = 2147483647; const int N = MAXINT/2; const float KM_PER_MI = 1.60934; const 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

48 178110: Computer Programming (II/2546) 48 Reserved Words & Identifiers Reserved words (or keywords) have specific meanings that cannot be used for other purposes Reserved Words Meaning constDeclaration of a constant data item includeA compiler directive namespaceRegion where program elements are defined usingIndicate that a program is using elements from a particular namespace

49 178110: Computer Programming (II/2546) 49 All C++ Reserved Words anddefault inlinepret_casttypename and_eqdelete intreturnunion asmdo longshortunsignedvoid autodouble mutable signedusing bitanddynamic_castnamespacesizeof Virtualbitorelsenewstatic boolenumnotstatic_cast volatilebreakexplicitnot_eq structwchar_tcaseexport operatorswitchwhilecatch externortemplatexor charfalseor_eqthis xor_eqclassfloatprivate throwcomplforprotected trueconstfriendpublic tryconst_castgotoregister typedefcontinueifreintertypeid

50 178110: Computer Programming (II/2546) 50 Identifiers Identifiers are names of variables and objects manipulated by a program IdentifiersMeanings cinC++ name for standard input stream m, nVariables for storing integers stdC++ name for the standard namespace

51 178110: Computer Programming (II/2546) 51 Rules for Selecting Identifiers 1. 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

52 178110: Computer Programming (II/2546) 52 Examples of Invalid Identifiers 1Letter float const Two*Four Joe’s two-dimensional hello world

53 178110: Computer Programming (II/2546) 53 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

54 178110: Computer Programming (II/2546) 54 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


ดาวน์โหลด ppt Chapter 5 Elementary C++ Programming Dept of Computer Engineering Khon Kaen University.

งานนำเสนอที่คล้ายกัน


Ads by Google