Top Banner
Computer Programming in C++ 黃黃黃 黃黃 Prof. Chung-Yang (Ric) Huang Department of Electrical Engineering National Taiwan University 2007/06/26
21

Computer Programming in C++

Apr 08, 2015

Download

Documents

zaheeda1984
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Programming in C++

Computer Programming

in C++

黃鐘揚 教授Prof. Chung-Yang (Ric) Huang

Department of Electrical Engineering

National Taiwan University2007/06/26

Page 2: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 2

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 444 02-3366-3644 [email protected]

TA: 葉護熺 [email protected]

Page 3: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 3

Course Outline1. Introduction to Computers, the Internet

and WWW2. Intro to C++ Programming3. Intro to Classes and Objects4. Control Statements: Part I5. Control Statements: Part II6. Functions and an Intro to Recursion7. Array and Vectors8. Pointers and Pointer-Based Strings

Page 4: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 4

Course Outline

9. Classes: A Deeper Look, Part I10. Classes: A Deeper Look, Part II11. Operator Overloading: String and Array

Objects12. Object-Oriented Programming: Inheritance13. Object-Oriented Programming:

Polymorphism14. Templates15. Stream I/O16. Exception Handling

Page 5: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 5

Course Outline

17. File Processing18. Class string and String Stream

Processing19. Web Programming20. Searching and Sorting21. Data Structures22. Standard Template Library (STL)23. Other Topics

Page 6: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 6

Components in C++

1. Keyword2. Function3. Variable (object)4. Data type5. Name space6. Comments

Page 7: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 7

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,…)

Page 8: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 8

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

Page 9: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 9

Components in C++4. Data type

Predefined types (also treated as keywaords) From standard library

(e.g. iostream, fstream, vector<T>…) User defined

5. Name space std User defined

6. Comments Single-line (started with // ) Multiple-line (enclosed by /* */)

Page 10: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 10

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 : 85

------------------------------

> The average is : 85

Page 11: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 11

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: 90------------------------------> Sam’s score is 80> Claire’s score is 90

Page 12: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 12

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 !!

Page 13: Computer Programming in C++

13

2006 Pearson Education, Inc. All rights reserved.

1.1    Introduction 1.2    What Is a Computer?1.3    Computer Organization 1.4    Early Operating Systems 1.5    Personal, Distributed and Client/Server Computing1.6    The Internet and the World Wide Web1.7    Machine Languages, Assembly Languages and High-Level Languages1.8    History of C and C++1.9    C++ Standard Library1.10    History of Java1.11    FORTRAN, COBOL, Pascal and Ada1.12    Basic, Visual Basic, Visual C++, C# and .NET1.13    Key Software Trend: Object Technology1.14    Typical C++ Development Environment1.15    Notes About C++ and C++ How to Program, 5/e1.16    Test-Driving a C++ Application

1.17    Software Engineering Case Study: Introduction to Object Technology and the UML (Required)

1.18    Wrap-Up1.19    Web Resources

Page 14: Computer Programming in C++

14

2006 Pearson Education, Inc. All rights reserved.

2.1 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

Page 15: Computer Programming in C++

15

2006 Pearson Education, Inc. All rights reserved.

3.1 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

Page 16: Computer Programming in C++

16

2006 Pearson Education, Inc. All rights reserved.

4.1 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

Page 17: Computer Programming in C++

17

2006 Pearson Education, Inc. All rights reserved.

5.1    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

Page 18: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 18

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 : 85

------------------------------

> The average is : 85

Page 19: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 19

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!! ***

Page 20: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 20

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() % 100 + 1; 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?

Page 21: Computer Programming in C++

06/27/07Chung-Yang (Ric) Huang [email protected] +886-2-3366-3644 21

Admin Info 07/18/07 A class website will be created

http://cc.ee.ntu.edu.tw/~ric/teaching/ComputerProgramming/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