Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Recitation Course Speaker: Liu Yu-Jiun Date: 2009/3/25.

Similar presentations


Presentation on theme: "OOP Recitation Course Speaker: Liu Yu-Jiun Date: 2009/3/25."— Presentation transcript:

1 OOP Recitation Course Speaker: Liu Yu-Jiun Date: 2009/3/25

2 Outline Class and Object Constructor Examples HW2 Announcement
HW1 Consultation Time

3 Class and Objects Typically, you cannot call a member function until you create an object of its class. The dot member selection operator (.) is preceded by an object’s name or by a reference to an object to access the object’s public members. The arrow member selection operator (->) is preceded by a pointer to an object to access that object’s public member. The keyword const can be used to specify that an object is not modifiable. Every object has access to its own address through the this pointer. The this pointer is passed as an implicit argument to each of the object’s non-static member functions.

4 Constructor A constructor must be defined with the same name as the class. A difference between constructors and functions is that constructors cannot return values. Normally, constructors are declared public. C++ requires a constructor call at the time each object is created, which helps ensure that every object is initialized before it is used in a program. For each member function defined outside of its corresponding class definition, the function name must be preceded by the class name and the binary scope resolution operator(::). class_ex.cpp

5 HW2 Assignment (Fraction calculator)
Input Sample Output Sample 1 + 2/3 -5 - 1/2 * 3 -4 % 5/3 -4/0 > 7 5/3 -11/2 3/2 -12/5 error: divide by zero! error: undefined operation!

6 HW2 Assignment Related Chapters: 3、9、10、11
Deadline: 2009/4/8 11:59 p.m. Score: Passing one test data gets one point, total 10 points. HW2 TA: 姜季強

7 HW1 Consultation Time 各式各樣的括號有沒有配對好? 變數名稱有沒有打錯? 陣列索引有沒有打錯? if判斷有沒有下錯?
從螢幕讀入的值有沒有減1 用來當索引值的變數名稱有沒有打錯? if判斷有沒有下錯? 結果有沒有一次印出? 是否用了靜態陣列而造成run time error?

8 Exercise1 新增一個叫 “mana”整數資料成員。 坐著回血時,hea每回復兩點,mana回一點。
新增可以取得mana值的成員函式。(getMana) 角色被攻擊時,mana每次損失攻擊方的agi值。 新增點數分配判斷式,如果初始點數總和不是60,印出請使用者重新輸入的訊息。

9 Exercise2 (Book 6.45) (最大公因數的遞迴解)
求整數x和y的最大公因數: 如果y等於0,則gcd(x, y)的值為x,否則gcd(x, y)的值為gcd(y, x%y),其中%是模數運算子。

10 #include <iostream>
using namespace std; int gcd(int, int); int main (){ int x, y, r; cout << "輸入整數x: "; cin >> x; cout << "輸入整數y: "; cin >> y; r = gcd(x, y); cout << "x和y最大公因數為: " << r << endl; system("PAUSE"); return 0; } int gcd(int x, int y){ if ( y == 0) return x; else return gcd(y, x%y);


Download ppt "OOP Recitation Course Speaker: Liu Yu-Jiun Date: 2009/3/25."

Similar presentations


Ads by Google