1 / 54

Chapter 5 Elementary C++ Programming

Chapter 5 Elementary C++ Programming. Dept of Computer Engineering Khon Kaen University. Major Concepts. แนะนำ C++ เบื้องต้น โอเปอร์เรเตอร์ Input/Output Characters และ Literals ตัวแปรและการประกาศ Tokens ของโปรแกรม การกำหนดค่าเริ่มต้นของตัวแปร ออปเจ็กต์, ตัวแปรและค่าคงที่

Download Presentation

Chapter 5 Elementary C++ Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

  13. 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)

  14. 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)

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

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

  18. ตัวอย่างโปรแกรม 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)

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

  20. 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)

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

  24. Return statement • เป็นการสั่งให้ส่งการทำงาน (หรือการควบคุม) ออกจากฟังก์ชันกลับไปยังจุดที่มีการเรียกใช้งานฟังก์ชันนั้น ๆ พร้อมกับให้ส่งค่าที่ระบุอยู่หลังคำ return กลับไป (ถ้ามี) • รูปแบบ: • return expression; • ตัวอย่าง • return 0; • return base*height/2.0; • return; 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 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 <iostream> หากต้องการใช้ classes and operators in library iostream 178110: Computer Programming (II/2546)

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

  29. ตัวอย่างโปรแกรม 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)

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

  35. 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)

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

  48. Reserved Words & Identifiers • Reserved words (or keywords) have specific meanings that cannot be used for other purposes 178110: Computer Programming (II/2546)

  49. All C++ Reserved Words and default inline pret_cast typename and_eq delete int return union asm do long short unsigned void auto double mutable signed using bitand dynamic_cast namespace sizeof Virtual bitor else new static bool enum not static_cast volatile break explicit not_eq struct wchar_t case export operator switch while catch extern or template xor char false or_eq this xor_eq class float private throw compl for protected true const friend public try const_cast goto register typedef continue if reinter typeid 178110: Computer Programming (II/2546)

  50. Identifiers • Identifiers are names of variables and objects manipulated by a program 178110: Computer Programming (II/2546)

More Related