Computer Programming in C++ 黃鐘揚 教授 Prof. Chung-Yang (Ric) Huang Department of Electrical Engineering National Taiwan University 2007/06/26
06/27/07 Chung-Yang (Ric) Huang Course Info 8 Meetings, 3 hours each Text Book C++ How to Program, 5e, Deitel & Deitel Grading 1-2 homeworks (TBD) 1 final exam or project Contact EE-II TA: 葉護熺
06/27/07 Chung-Yang (Ric) Huang Course Outline 1. Introduction to Computers, the Internet and WWW 2. Intro to C++ Programming 3. Intro to Classes and Objects 4. Control Statements: Part I 5. Control Statements: Part II 6. Functions and an Intro to Recursion 7. Array and Vectors 8. Pointers and Pointer-Based Strings
06/27/07 Chung-Yang (Ric) Huang Course Outline 9. Classes: A Deeper Look, Part I 10. Classes: A Deeper Look, Part II 11. Operator Overloading: String and Array Objects 12. Object-Oriented Programming: Inheritance 13. Object-Oriented Programming: Polymorphism 14. Templates 15. Stream I/O 16. Exception Handling
06/27/07 Chung-Yang (Ric) Huang Course Outline 17. File Processing 18. Class string and String Stream Processing 19. Web Programming 20. Searching and Sorting 21. Data Structures 22. Standard Template Library (STL) 23. Other Topics
06/27/07 Chung-Yang (Ric) Huang Components in C++ 1. Keyword 2. Function 3. Variable (object) 4. Data type 5. Name space 6. Comments
06/27/07 Chung-Yang (Ric) Huang Components in C++ 1. Keyword Procedure control (e.g. if, else, for, while, do, break, continue, return, using, try, throw, catch … ) Symbols (e.g. { }, ( ), &, |, ^, &&, ||, ~, !, *, ->, >, >, <<, “”, ‘’, \,, … ) Type declaration (e.g. class, struct, union, public, protected, private, virtual … ) Attribute (e.g. const, static … ) Constant (e.g. 1, 23, 456, 0xff08, true, false, string “ xxxx ”, ….) Predefined types (e.g. int, char, unsigned, short int, long int, float, double … ) Preprocessor deritives (e.g. #include, #ifdef, … )
06/27/07 Chung-Yang (Ric) Huang Components in C++ 2. Function main From standard library (e.g. exit, printf … ) User defined 3. Variable (object) From standard library (e.g. cout, cin … ) User defined
06/27/07 Chung-Yang (Ric) Huang Components in C++ 4. Data type Predefined types (also treated as keywaords) From standard library (e.g. iostream, fstream, vector … ) User defined 5. Name space std User defined 6. Comments Single-line (started with // ) Multiple-line (enclosed by /* */)
06/27/07 Chung-Yang (Ric) Huang Practice Prog-2.1 Write a program … just use cin and cout > Enter the first score : 80 > Enter the second score: 90 > Enter the thrid score : > The average is : 85
06/27/07 Chung-Yang (Ric) Huang Practice Prog-2.2 Follow Prog-2.1 > Enter the first name : Sam > Enter the first score : 80 > Enter the second name : Claire > Enter the second score: > Sam’s score is 80 > Claire’s score is 90
06/27/07 Chung-Yang (Ric) Huang Practice Prog-2.3 Continued from Prog-2.2. However, check if the entered score is between 0 and 100. If not issue an error message as follows: > Enter the first score : -30 Error: “ -30 ” is not a legal score !!
2006 Pearson Education, Inc. All rights reserved Introduction 1.2 What Is a Computer? 1.3 Computer Organization 1.4 Early Operating Systems 1.5 Personal, Distributed and Client/Server Computing 1.6 The Internet and the World Wide Web 1.7 Machine Languages, Assembly Languages and High-Level Languages 1.8 History of C and C C++ Standard Library 1.10 History of Java 1.11 FORTRAN, COBOL, Pascal and Ada 1.12 Basic, Visual Basic, Visual C++, C# and.NET 1.13 Key Software Trend: Object Technology 1.14 Typical C++ Development Environment 1.15 Notes About C++ and C++ How to Program, 5/e 1.16 Test-Driving a C++ Application 1.17 Software Engineering Case Study: Introduction to Object Technology and the UML (Required) 1.18 Wrap-Up 1.19 Web Resources
2006 Pearson Education, Inc. All rights reserved Introduction 2.2 First Program in C++: Printing a Line of Text 2.3 Modifying Our First C++ Program 2.4 Another C++ Program: Adding Integers 2.5 Memory Concepts 2.6 Arithmetic 2.7 Decision Making: Equality and Relational Operators 2.8 (Optional) Software Engineering Case Study: Examining the ATM Requirements Document 2.9 Wrap-Up
2006 Pearson Education, Inc. All rights reserved Introduction 3.2 Classes, Objects, Member Functions and Data Members 3.3 Overview of the Chapter Examples 3.4 Defining a Class with a Member Function 3.5 Defining a Member Function with a Parameter 3.6 Data Members, set Functions and get Functions 3.7 Initializing Objects with Constructors 3.8 Placing a Class in a Separate File for Reusability 3.9 Separating Interface from Implementation 3.10 Validating Data with set Functions 3.11 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document 3.12 Wrap-Up
2006 Pearson Education, Inc. All rights reserved Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Selection Statement 4.6 if...else Double-Selection Statement 4.7 while Repetition Statement 4.8 Formulating Algorithms: Counter-Controlled Repetition 4.9 Formulating Algorithms: Sentinel-Controlled Repetition 4.10 Formulating Algorithms: Nested Control Statements 4.11 Assignment Operators 4.12 Increment and Decrement Operators 4.13 (Optional) Software Engineering Case Study: Identifying Class Attributes in the ATM System 4.14 Wrap-Up
2006 Pearson Education, Inc. All rights reserved Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 for Repetition Statement 5.4 Examples Using the for Statement 5.5 do … while Repetition Statement 5.6 switch Multiple-Selection Statement 5.7 break and continue Statements 5.8 Logical Operators 5.9 Confusing Equality ( == ) and Assignment ( = ) Operators 5.10 Structured Programming Summary 5.11 (Optional) Software Engineering Case Study: Identifying Objects’ States and Activities in the ATM System 5.12 Wrap-Up
06/27/07 Chung-Yang (Ric) Huang Practice Prog-3.1 Similar to Prog-2.1, but create a class called “ Student ” and store the scores and average as its data members. Use member functions to set and get. > Enter the first score : 80 > Enter the second score: 90 > Enter the third score : > The average is : 85
06/27/07 Chung-Yang (Ric) Huang Practice Prog-4.1 Guess Number: > Enter the range [1 ~ ?]: 100 > Please guess a number: 50 > => Too small!! > Please guess a number: 75 > => Too large!! > Please guess a number: 63 > => You are right!! > Play again? (Y/N) N > *** Good-Bye!! Have a nice day!! ***
06/27/07 Chung-Yang (Ric) Huang Tips & Issues about Prog. 4-1 How to generate random numbers? Calling function “ rand() ” (for Windows) How to generate random numbers between 1 and 100 ? y = rand() % ; Is the random number the same every time you play? How to fix it? (Homework) Try to google this problem. Do you check whether the input number is within the legal range? What if the player makes a stupid guess? For “ Play again? (Y/N) ”, can we accept “ default value ” (i.e. no enter)? Do you create any “ class ” for this program? Why yes and why not?
06/27/07 Chung-Yang (Ric) Huang Admin Info 07/18/07 A class website will be created ramming/S07-tl ramming/S07-tl Practice programs This file Announcements Final homework/project will be announced online next week You are encouraged to think about the problem as early as possible Tentative due date: 08/13 (Monday) Submission method: TBA