Presentation is loading. Please wait.

Presentation is loading. Please wait.

Speaker: Liu Yu-Jiun Date: 2009/4/15

Similar presentations


Presentation on theme: "Speaker: Liu Yu-Jiun Date: 2009/4/15"— Presentation transcript:

1 Speaker: Liu Yu-Jiun Date: 2009/4/15
Recitation Course 0415 Speaker: Liu Yu-Jiun Date: 2009/4/15

2 Outline review exercise

3 Chapter 3 – Class and Objects

4 // rational.h #include <iostream> using namespace std; class RationalNumber{ public: RationalNumber(int = 0, int = 1); RationalNumber operator+(const RationalNumber&); RationalNumber operator-(const RationalNumber&); RationalNumber operator*(const RationalNumber&); RationalNumber operator%(const RationalNumber&); private: int numerator; int denominator; bool sign; void reduction(void); int gcd(int, int); }; Class內所有的成員與成員函式都預設為private。若要讓class外部的程式使用,就得設定為public。 如果沒有定義建構子,系統也會給一個預設的建構子和解構子。

5 Chapter 4 & 5 – control statement

6 Control Structure Three control structures Sequence structure
Programs executed sequentially by default Selection structures if, if...else, switch Repetition structures while, do...while, for

7 Fig. 4.2 | Sequence-structure activity diagram.
total = total + grade; counter = counter + 1; Fig. 4.2 | Sequence-structure activity diagram.

8 if

9 Fig. 4.4 | if single-selection statement activity diagram.
if (grade >= 60) cout << “Passed”; Fig. 4.4 | if single-selection statement activity diagram.

10 if...else

11 Fig. 4.5 | if...else double-selection statement activity diagram.
if (grade >= 60) cout << “Passed”; else cout << “Failed”; cout << (grade >= 60 ? “Passed” : “Failed”) Fig | if...else double-selection statement activity diagram.

12 4.6 if…else Double-Selection Statement (Cont.)
Ternary conditional operator (?:) Three arguments (condition, value if true, value if false) Code could be written: cout << ( grade >= 60 ? “Passed” : “Failed” ); Condition Value if true Value if false

13 while loop

14 Fig. 4.6 | while repetition statement UML activity diagram.
int product = 3; while (product <= 100){ product = 3 * product } Fig. 4.6 | while repetition statement UML activity diagram.

15 for loop

16 Fig. 5.3 | for statement header components.

17 Fig. 5.4 | UML activity diagram for the for statement in Fig. 5.2.
for (int c = 1 ; c <= 10 ; c++){ cout << c << “ ”; } Fig. 5.4 | UML activity diagram for the for statement in Fig. 5.2.

18 5.3 for Repetition Statement
General form of the for statement for ( initialization; loopContinuationCondition; increment ) statement; Can usually be rewritten as: initialization; while ( loopContinuationCondition ) { statement; increment; }

19 do...while

20 Fig. 5. 8 | UML activity diagram for the do
Fig. 5.8 | UML activity diagram for the do...while repetition statement of Fig. 5.7.

21 switch

22 5.6 switch Multiple-Selection Statement
switch statement Used for multiple selections Tests a variable or expression Compared against constant integral expressions to decide on action to take Any combination of character constants and integer constants that evaluates to a constant integer value

23 5.6 switch Multiple-Selection Statement (Cont.)
switch statement (Cont.) default case Executes if no matching case label is found Is optional If no match and no default case Control simply continues after the switch

24 Fig. 5.12 | switch multiple-selection statement UML activity diagram with break statements.

25 Chapter 6 – Function and Template

26 Components of Function (L4.2)
Function prototype int compare(int, int); Function call int main(){ int a = 3, b=4; int bigger; bigger = compare(a, b); } Function definition int compare(int v1, int v2){ if (v1 < v2) return v2; else return v1;

27 Function Call Call by value Call by reference Call by pointer(address)

28 Call by Value 不影響原本的值 需要較多的記憶體空間 void swap(int a, int b){ ...... }
int main(){ int c = 3, d = 5; swap(c, d); a 3 0x23ff50 5 b 0x23ff54 ... c 3 0x23ff70 5 0x23ff74 d 不影響原本的值 需要較多的記憶體空間

29 Call by Reference 會影響原本的值 節省記憶體空間 void swap(int &a, int &b){ ...... }
int main(){ int c = 3, d = 5; swap(c, d); a c 3 0x23ff50 5 b d 0x23ff54 會影響原本的值 節省記憶體空間

30 Call by Pointer(address)
void swap(int *a, int *b){ ...... } int main(){ int c = 3, d = 5; swap(&c, &d); a 0x23ff70 0x23ff50 0x23ff74 b 0x23ff54 ... c 3 0x23ff70 5 0x23ff74 d 會影響原本的值 需要較多的記憶體空間

31 Function Function overloading return_value_type Name (parameters);
Closely related tasks Same name Different sets of parameters (number or type) 利用傳入不一樣的參數做類似,但又不完全相同的工作。

32 Function Overloading Allow function overloading in C++
different parameter number different parameter data types (different order) Different const Disallow function overloading Different return parameter Different reference Default argument Different const只是表示參數列使用的是const或非const也能區別

33 Different Return Parameter
void nothing(int a){} int nothing(int a){ return a;} void nothing(int a){} int nothing(int a, int b){ return a+b;}

34 Different Reference Function signature: @nothing$qi @nothing$qri
void nothing(int a){} void nothing(int &a){} int main(){ int b = 3; nothing(b); return 0; } @nothing$qi @nothing$qri

35 Function Templates More compact and convenient form of overloading
Identical program logic and operations for each data type Function template definition Written by programmer once Begins with the template keyword Contains template parameter list of formal type parameters for the function template enclosed in angle brackets (<>) Formal type parameters Preceded by keyword typename or class Placeholders for fundamental types or user-defined types

36 Function Templates template <class T, class S>
T test(T v1, T v2, S v3){ if (v1 > v2){ cout << v3 << endl; return v1; } else { return v2; int main(){ int a = 3, b = 5; string s1 = "int compare"; cout << test(a, b, s1) << endl; double c = 4.67, d = 4.15; string s2 = "double compare"; cout << test(c, d, s2) << endl;


Download ppt "Speaker: Liu Yu-Jiun Date: 2009/4/15"

Similar presentations


Ads by Google