0% found this document useful (0 votes)
30 views

OOP Week 2

This document provides an overview of an Object Oriented Programming lecture covering topics like flow of control, console I/O, namespaces, strings, problem solving approaches, file I/O, and recaps of control flow statements like if-else, switch, and loops. The lecture is taught by Shuo-Han Chen and takes place on Mondays and Fridays from 15:10-16:00 in room 401 of the Pioneer International R&D building. There is no class scheduled on certain national holidays.

Uploaded by

鄭力愷
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

OOP Week 2

This document provides an overview of an Object Oriented Programming lecture covering topics like flow of control, console I/O, namespaces, strings, problem solving approaches, file I/O, and recaps of control flow statements like if-else, switch, and loops. The lecture is taught by Shuo-Han Chen and takes place on Mondays and Fridays from 15:10-16:00 in room 401 of the Pioneer International R&D building. There is no class scheduled on certain national holidays.

Uploaded by

鄭力愷
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Department of Computer Science and Information

Engineering

Object Oriented Programming


Lecture 01: Flow of Control

Shuo-Han Chen ( 陳碩漢 ),


[email protected]

Pioneer International R&D Building - 401


M 15:10 - 16:00 & F 10:10 - 12:00
Course Schedule Due to national holidays, there will be no class on Oct. 10, Oct. 31, and Jan. 2.
W Date Lecture Readings Homework
1 Sept. 12, 16 Lec01: Introduction & Environment Setup Chapter 1
2 Sept. 19, 23 Lec02: Flow of Control HW 00
3 Sept. 26, 30 Lec03: Function Basics / Exception Handling
4 Oct. 3, 7 Lec04: String
5 Oct. 10, 14 National Day / Lec05: Pointer
6 Oct. 17, 21 Lec06: Parameters and Overloading
7 Oct. 24, 28 Lec07: Structures and Classes
8 Oct. 31, Nov. 4 No class / Lec08: Constructors and Other Tools
9 Nov.7, 11 Physical Hand-Written Midterm / Online Computer-based Midterm
10 Nov. 14, 18 Lec09: Operator Overloading, Friends, and References
11 Nov. 21, 25 Lec10: Inheritance
12 Nov. 28, Dec. 2 Lec11: Inheritance
13 Dec. 5, 9 Lec12: Polymorphism and Virtual Functions
14 Dec. 12, 16 Lec14: Standard Template Library
15 Dec. 19, 23 Lec15: Templates
16 Dec. 26, 30 Physical Hand-Written Final / Lec16: Streams and File I/O / Namespace

17 Jan. 2, 6 No class / Online Computer-based Final


18 Jan. 9, 13 No class & Let’s you guys to get prepared for other finals

2
Console I/O (Text book 57 – 64)
• In C++, the printf and scanf are replaced with cin & cout
• Part of the iostream library and std namespace
• When you do output
• cout <<
• When you do input
• cin >>
• Direction : To where you
want the content to be
• You can put variable in
cin/cout directly, without
• scanf(“%i”, number);
• printf(“%i”, number);
3
Namespace (Text book 521 – 540)
• Namespace is a collection of name definition
• ex. Function / class / variable
• Namespace is used to isolate function/class with the same name but has different
functions
• Std contains all definition from all standard library
• The way we use it
• Includes entire standard library of name definitions
• #include <iostream>
using namespace std;
• Can specify just the objects we want
• #include <iostream>
• using std::cin;
using std::cout;
4
Namespace (Text book 521 – 540)

5
Namespace (Text book 521 – 540)
• Multiple namespace in one code project is allowed
• You can also define namespaces of your own

6
String
• We have less trouble now
when dealing with stream
• Part of the string library and
std namespace
• You do cout and cin directly
• We can also mess the string
with some operators /
methods
• +=
• +
• ==
• .length() / .compare()
7
How does that become possible ?
• As we already discussed, C++ enable objects to have
• State (Data)
• Behavior (Functions)
• See the implementation of string here https://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/api/a00259.html
• https://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/api/a00770_source.html

• Data • Functions

8
Try this out
• http://cplusplus.com/
• Will be very helpful in your future homework

9
How we are going to solve this problem ?
• In this course, we follow the steps of
• “How To Solve It” ( 數學家 George Pólya)
1. 瞭解問題 (understanding the problem)
• Find the number of continuous upper case in a string
2. 規劃解法 (devising a plan)
• 先個別檢查字母的大小寫
• 用 0 1 代表
• 找出把所有的 0 的長度印出來

3. 依規劃解題  (carrying out the plan)


• 大小寫檢查 isUpper()
• 型態 String, int, vector
4. 回顧 (looking back) 10
Assuming you’re familiar with the following
• Comparison Operators

• Logical Operators
• Logical AND (&&)
• Logical OR (||)
11
Assuming you’re familiar with the following
• Truth Table

12
Precedence of Operators

13
Precedence of Operators (Cont’d)

14
Precedence of Operators (Cont’d)

15
Precedence of Operators (Cont’d)

16
Precedence Examples
• Arithmetic before logical
• x + 1 > 2 || x + 1 < -3 means:
• (x + 1) > 2 || (x + 1) < -3
• Short-circuit evaluation
• (x >= 0) && (y > 1)
• Be careful with increment operators!
• (x > 1) && (y++)
• Integers as boolean values
• All non-zero values  true
• Zero value  false

17
Precedence Examples
• Some cases
• cout<<( 6 != 5 ) <<"\n";
• cout<<( 5 + 3 > 4 )<<"\n";
• cout<<( 5 + ( 3 > 4 ))<< "\n";
• cout<<( 0 || 1 && 0 ) <<"\n";
• cout<<( !0 || 0 && 0 ) <<"\n";
• cout<<( 3 + 4 * 7 / 12 % 5 ) <<"\n";

18
Strong Enum
• C++11 introduces strong enums or enum classes
• Does not act like an integer
• Avoid using enums as integer
• Avoid name confliction
• Can be assigned to types other than integer

19
Enum (C)

Legal

20
Strong Enum

Illegal

21
Simple File I/O
• We can use cin to read from a file in a manner very similar to reading from
the keyboard
• Add at the top
#include <fstream>
using namespace std;
• You can then declare an input stream
ifstream inputStream;
• Next you must connect the inputStream variable to a text file on the disk.
inputStream.open("filename.txt");
• The “filename.txt” is the pathname to a file in the current directory

22
Simple File I/O
• Use
inputStream >> var;
• The result is the same as using cin >> var except the input is coming from
the text file and not the keyboard
• When done with the file close it with
inputStream.close();
• Example : Consider a text file named player.txt with the following text

23
Simple File I/O

24
Recap : if - else

25
Recap : if - else

26
Recap : switch

27
Recap : switch

28
Recap : Loops
• 3 Types of loops in C++
• while
• Most flexible
• No "restrictions"
• do-while
• Least flexible
• Always executes loop body at least once
• for
• Natural "counting" loop
29
Recap : Loops

30
Recap : Loops

31
Recap : Loops
• for (count=0;count<3;count++)
{
cout << "Hi "; // Loop Body
}
• How many times does loop body execute?
• Initialization, loop condition and update all
"built into" the for-loop structure!
• A natural "counting" loop

32
Summary
• Console I/O
• Namespace in C++
• Char array become String Class
• 4 steps in solving programming problem
• Simple File I/O
• Recap
• Comparator
• If-else
• switch
• Loops

33
Q&A
Thank you for your attention.
Class-related messages will
be available on MS teams

Access Code:
go4k75s
Exam Time Survey
before 09/23 24:00!

You might also like