3. Review: Pass-By-Value
• A value parameter in a function becomes a copy of the argument
that is passed in
• Changing the value of a value parameter:
– Does change the memory associated with the parameter
– Does not change the memory associated with the argument passed in
void valFunc(float val)
{
val = 50; This assignment changes the
} memory associated with
this variable (only)!
int main() (main's mainVal is unaffected)
{
int mainVal = 9;
valFunc(mainVal); mainVal: 9
cout << "mainVal: " << mainVal << endl;
return (0);
}
3
4. Review: Pass-By-Reference
• A reference parameter in a function "references" the same
physical memory location as the argument passed in
• Changing the value of a reference parameter:
– Does change the memory associated with the argument passed in
– Therefore – argument's memory must not be constant or literal
void refFunc(float &val)
{ This assignment changes the memory
val = 50;
associated with this variable!
}
int main()
{
int mainVal = 9;
refFunc(mainVal);
mainVal: 50
cout << "mainVal: " << mainVal << endl;
return (0);
}
4
5. Review: Encapsulation
• Hide the object's nucleus from other objects in the program.
– The implementation details can change at any time without
affecting other parts of the program.
實做細節隱藏
• It is an ideal representation of an object, but
– For implementation or efficiency reasons, an object may wish to
expose some of its variables or hide some of its methods.
• Benefits: 存取控制
– Modularity (模組化程式設計)
• The source code for an object can be written and maintained
independently of the source code for other objects.
– Information hiding (提供外界標準的溝通介面)
• An object has a public interface that other objects can use to
communicate with it.
5
6. 用 array 來做 STACK (1/2)
• void push (int y) {
Initialization: ++sptr;
x[sptr] = y;
int x[99];
sptr = -1; /*empty*/ }
98
push (456) ;
sptr push (3388) ;
Stack Pointer int ans = pop( );
int sptr; int pop ( ) {
int tmp = x[sptr];
3388 1
456 0 --sptr;
-1 return tmp;
需要一個array 和一個整數 }
6
7. 用 array 來做 STACK (2/2)
static int x[99]; /* static 使得別的檔案看不到這 */
static int sptr = -1; /* empty stack */
void push(int y) {
++sptr; /* move stack pointer */
x[sptr] = y; /* put the data y there */
}
int pop( ) {
return x[sptr--]; /* 注意 -- 寫後面 */
}
/* 其它相關 functions 例如 isEmpty( ), isFull() */
7
喔~ 模仿class的封裝功能 (encapsulation)
16. Information hiding (資訊隱藏 1/3)
使用 Stack 時不必知道如何 push 如何 pop 等細節!
• Stack 只能用 Array 實作嗎?
就是說不必知道 Stack 是如何實作的!
==> can be implemented by Linked List
(鏈結串列)
How to do it?
Hint: 使用 struct/class 配合 pointer
16
17. Information hiding (資訊隱藏 2/3)
使用 Stack 時不必知道如何 push 如何 pop 等細節!
class Animal { // private: private 的 data 與 functions 也都不可在
long height; class 以外的 functions 中直接用!
double haha; 只有 class 內宣告的 member function 才
可以直接存取 private 的 data 與 function
public:
Animal( );
int main( ) {
float weight;
Animal x; /* local variable */
void talk( ) { /***/ }
/* … */ 自己try try看
};
cout << sizeof(x) << endl;
Animal::Animal( ) {
cout << x.weight << endl; /* OK */
height = 170; haha = 38.49;
cout << x.height << endl; /* error */
weight = 59.66;
}
}
17
18. friend 關鍵字的用法
Information hiding (資訊隱藏 3/3)
class Animal {
俗話說: What is the friend for?
long height;
double haha; 子路曰:「願車馬衣裘與朋友共,
public: 敝之而無憾。」
Animal( );
float weight; int main( ) {
void talk( ) { /***/ } Animal x;
friend int main ( ); /* … */
}; cout << sizeof(x) << endl;
Animal::Animal( ) { cout << x.weight << endl;
height = 170; haha = 38.49; cout << x.height << endl; /* OK now */
weight = 59.66; }
}
C++ Joke: Friend is the only one who can touch your private ! 18
19. Information Hiding Summary
3 access control clauses for class members
private: clause for hidden entities
public: clause for interface entities
protected: clause - for inheritance (extension)
friend functions - to provide access to private
members in some unrelated program units or
functions
friend functions are not member functions of class
– Defined outside of class scope
if B a friend of A, A not necessarily a friend of B
你宣告我為你朋友, 並不代表我也認你
19
為朋友 (一定要我正式宣告才算)
20. 認真看 class
事實上C++裡 struct 也可包含 method/function
• C語言的 struct 目的是把相關資料 group 在一起
• class (類別) 就是原來的 struct with some regulations
• class is an extension of user-defined data type, 自訂的, 抽象的
• 所以 class 是 Abstract Data Type (ADT)
• ADT 就是把 data 以及對這些 data有關的動作 (method, 就是
function) 一起封藏 (Encapsulate) 在一個程式單元 (program
unit) 之內, 例如 C++ 用 class 來封藏
• Class 可用來設計軟體元件 (Software Component)
• Class 內的 data member/method member 存取控制:
– private, protected, public (C++ 與 Java 寫法不同)
• Constructor? Destructor? Default Constructor? …
• Java 沒有 Destructor, 但有 finalize 函數
20
21. ADT --- Data Abstraction
• An Abstract Data Type (ADT) is a user-defined
data type that satisfies the following two
conditions: (Encapsulation + Information Hiding)
– The representation of, and operations on, objects of
the type are defined in a single syntactic unit; also,
other program units can create objects of this type.
– The representation of objects of this type is
hidden from the program units that use these
objects, so the only operations (methods) possible
are those provided in the type's definition which are
known as interfaces. class 裡開放為 public 的 functions
21
22. ADT in C++
class / struct can be used as the encapsulation
device (只有一開始 default access 屬性不同)
All of the class instances of a class share a
single copy of the member functions
Each instance of a class has its own copy of
the class data members
class instance of a class = object
object is an instance (variable) of some class (type)
22
23. OO features
Encapsulation
Information Hiding The concept of abstraction is
fundamental in programming
抽象化的概念以前
就有: 函數/副程式 Subprogram / function
增加: Process abstraction
Inheritance ADT
Polymorphism Data abstraction
Software Reuse
23
24. Class vs. Object
• Object is an instance of some class
– int x; float yy;
• Object (物件, 個體, 東西) -- 就是以前所謂的變數
#include <iostream> #include <iostream>
class Student { struct Student {
long sid; long sid;
char name[9]; char name[9];
}; };
Student x, y[99], tempstu; Student x, y[99], tempstu;
int main( ) { int main( ) {
x.sid = 38; // Error! x.sid = 38; // OK!
cout << x.sid; // Error! cout << x.sid; // OK!
} } 24
25. Struct vs. Class (1/3)
重要觀念
#include <iostream> 考!!
class Student { // 若在此寫 public: ?
long sid; // default is private for class
}; // default is public for struct
int main( ) {
Student x;
x.sid = 123; // compile 錯, access not allowed
cout << "== " << x.sid << endl; // compile 錯
return 0;
}
若改為 struct Student 則變沒錯! Why? 25
26. Struct vs. Class (2/3)
#include <iostream>
class Student {
long sid; // default is private for class
friend int main( );
}; // default is public for struct
int main( ) {
Student x;
x.sid = 123; // 變對了 Why?
cout << "== " << x.sid << endl; // compile OK
return 0;
}
C++ Joke: Friend is the only one who can touch your private ! 26
27. 開放 data members
Struct vs. Class (3/3) 違反資料封裝原則
#include <iostream> 正確概念是透過
class Student { // private: member functions
long sid;
public: long showNumber( ) { return sid; }
void setId( long xx ) { sid = xx; }
};
int main( ) {
Student x, y;
x.setId(123); // OK, 透過 public function setId( )
cout << "== " << x.showNumber( ) << endl; // OK!
return 0;
}
27
28. Class (Object) Constructor (1/4)
#include <iostream>
class Student { // private:
long sid;
public: long showNumber( ) { return sid; }
void setId( long xx ) { sid = xx; }
};
使用 default constructor
int main( ) {
沒有參數
Student x, m, n;
Student y(456); // Error!
x.setId(123); Student y(456) ; 有問題!
return 0; 因為 default constructor
} 就是不能有參數
28