OOP Week 2
OOP Week 2
Engineering
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 的長度印出來
• 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!