SlideShare une entreprise Scribd logo
1  sur  38
Télécharger pour lire hors ligne
Object-Oriented Programming
       (OOP) - Part I

    Intro. to Computer Science (II)
  Dept. of Communication Engineering

              Instructor: Ting-Yu Lin
Agenda

•   Review: parameter passing
•   Review: design and implement a STACK
•   物件導向 (Object-Oriented) 程式設計概念
•   建構元與解構元 (Constructor & Destructor)
•   繼承關係 (Inheritance) 與軟體再利用
•   多型概念 (Polymorphism) 或叫“同名異式”

                       以繼承為基礎

                                           2
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
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
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
用 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
用 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)
使用 STACK
extern void push(int); /* 宣告 */
extern int pop( );
#include <stdio.h>
int main( ) {
    push(543);
    push(881);
    printf( "%dn", pop( ) );
 }
 /* 可以用! 但若要兩個 Stack 呢? */

                                  8
More about STACK (1/3)
/* 若要兩個 Stack 呢? */
解法之一
==> 使用兩個 array, 並把 array 當參數
    push(ary2, x);   ans = pop(ary2);
但是這樣該些 array 必須 開放讓使用者知道
      要讓宣告 extern就看得到, 不可再用 static Global
      違反不必讓使用者知道push到哪去的 Information
    Hiding 原則 !
                         只要看得到就可能不小心改到
(或是該些 array 要定義在使用者程式裡面)

                                        9
More about STACK (2/3)
/* 若要兩個 Stack 呢? */
解法之二
==> 整個檔案複製到另一個 file, 並把各相關函數
名稱都改掉, 例如 push2, pop2, …
==> array 與變數名稱不用改! Why?
      (因為 static Global)
但是這樣 也不是很方便, 函數名稱一大堆
      若要三個 Stack 呢? 四個呢? …
(不過這比前面使用不同 array 的方法好!) Why?

               因為沒違反資料隱藏原則      10
More about STACK (3/3)
/* 若要兩個 Stack 呢? */
/* 若要三個 Stack 呢? 四個? 五個 . . . */
/* 有沒有更方便直接的方法? */
 solution ==> using C++ Class
      to define a Stack as a Software Component
 可否讓堆疊用起來像物件? (軟體元件或零件)
Stack x;
Stack y;
x.push(13579);
y.push(258);
y.push( x.pop( ) );
                                            11
宣告部份

Stack -- class example (1/2) mystk.h
class Stack {     // private:
        long data[99];     /* 直接寫 99 不是好習慣 */
        int sptr;      // 不能寫 int sptr = -1;
     public:
          Stack( ) ; /* constructor */
          void push( long );
          long pop( );
          bool isEmpty( );
}; /* class Stack */


 bool 要新版的 C++ 才有, 如果不能用 bool 就改用 int
                                                12
真正定義

Stack -- class example (2/2) mystk.cpp
  #include "mystk.h"                 Initialization 工作
  #include <iostream.h>               要寫在特殊函數
  Stack::Stack( ) { sptr = -1; } /* constructor */
  void Stack::push(long xx) { /* 注意 Stack:: */
     data[ ++sptr ] = xx;
  }
  long Stack::pop( ) { return data[sptr--]; }
  bool Stack::isEmpty( ) { return sptr == -1; }

 // bool 要新版的 C++ 才有
     這是實作出 Stack, 前面 mystk.h 只是宣告
                                                     13
Stack (之使用) mymain.cpp
 #include "mystk.h"            注意:
 #include <iostream.h>          #include <iostream.h> 是
int main( ) {                  舊的寫法;
   Stack xo, brandy;           新的寫法是:
   xo.push(880);
                                #include <iostream>
   xo.push(770);
                               而且要寫:
   xo.push(53);
   while( !xo.isempty( ) ) {    using namespace std;
      cout << " " <<           因為 C++程式庫在 std
      xo.pop( );               命名空間內
   } cout << endl;
   return 0;
 }                                                  14
Stack (之使用) 如何執行?
#include "mystk.h"
                             g++ mymain.cpp mystk.cpp
#include <iostream.h>
int main( ) {                ./a.out
   Stack xo, brandy;
   xo.push(880);             g++ -c mystk.cpp
   xo.push(770);             g++ mymain.cpp mystk.o
   xo.push(53);
                             ./a.out
   while( !xo.isempty( ) ) {
      cout << " " << xo.pop( );
   } cout << endl;
   return 0;                         53 770 880
 }
                                                    15
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
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
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
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
          為朋友 (一定要我正式宣告才算)
認真看 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
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
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
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
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
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
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
開放 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
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
Class (Object) Constructor                  (2/4)
 #include <iostream>
 class Student {      // private:
     long sid;
   public: long showNumber( ) { return sid; }
           void setId(long xx) { sid = xx; }
           Student(long x) { sid = x; }      加入這constructor
 };
                                      Student x; 有問題!
 int main( ) {
                                      因為 default constructor
    Student x;      // Error..        不能用了 –
    Student y(456); // OK now! 有兩種解決方法:
    x.setId(123);
    return 0;                     Student y(456) ; 現在OK了
 }
                                                         29
Class (Object) Constructor                 (3/4)
 #include <iostream>
 class Student {     // private:
     long sid;
   public: long showNumber( ) { return sid; }
           void setId(long x) { sid = x; }
           Student(long x = 0) { sid = x; }
 };
 int main( ) {
    Student x, y(456); // OK!           Student x; 有問題
    x.setId(123);                        解決方法一
                                          使用 default 參數
    return 0;
 }
                                                          30
Class (Object) Constructor                 (4/4)
 #include <iostream>
 class Student {     // private:
     long sid;
   public: long showNumber( ) { return sid; }
           void setId(long xx) { sid = xx; }
           Student(long x) { sid = x; cout << "Hehehen"; }
           Student( ) { sid = 0; cout << "Hahan"; }
 };
 int main( ) {
    Student x, y(456); // OK!
                                Student x; 有問題
    x.setId(123);
                                 解決方法之二
    return 0;
                                再加一個沒參數的constructor
 }
                                                       31
Summary of Constructor
Constructor 就是與 Class 同名的函數
Constructor 函數沒有 type, 不可以寫 return type
Constructor 將在 object 被安排好記憶體後立即執行, 所
以通常用來設定初值 (initial value)
Student x; // 這樣會幫 x 安排一塊記憶體, 並執行 x.Student( );
Student *p; // 這樣只有指標, 沒有生出 Student, 不會執行
constructor, 要 p = new Student( ); 才會執行 constructor
Object 就是 Class 的一個 instance (案例); 就是 變數 啦
Constructor 可以不寫, 此時等於有一個沒參數且不做事的
Default Constructor
只要寫了一個 Constructor, 則原先的 Default Constructor
就自動消失不能用了
Constructor 也可以寫很多個, 自動依參數決定用哪個
   此即 Function name overloading (函數名稱重複用) 的概念   32
Destructor

在 object 佔用之記憶體還掉之前
   被瞬間執行的程式 (函式)




                      33
Object Destructor                          dtest.cpp
#include <iostream>
using namespace std;
class Student {       // private:
    long sid;
  public: long showNumber( ) { return sid; }
            void setId(long xx) { sid = xx; }
      Student(long x) { sid = x; cout << x << " Hohohon"; }
        Student( ) { sid = 38; cout << "Hahan"; }
       ~Student( ) { cout << sid << " Arrrrh 死了n"; }
};                                                    這是 destructor
void test( ) { Student x; cout << " ==到此一遊== "; }
Student xxx(123);
int main( ) { cout << " *** 開飯了 ***n";
     Student y(456);
     test( ); // 會跳入 test( ) , 注意會做哪些事?
     return 0;
}                                                                34
人體 compiler
告訴我 output
  是啥?




              35
不要偷偷翻頁喔~~
            36
Output of previous program
                dtest.cpp
mytest/> g++ dtest.cpp
mytest/> ./a.out
123 Hohoho
 *** 開飯了***
456 Hohoho
Haha
 ==到此一遊== 38 Arrrrh 死了
456 Arrrrh 死了
123 Arrrrh 死了
mytest/> exit
                                37
Constructor vs. Destructor
                                Compiler 可以分辨的
• Constructor 可以寫很多個                原則之下

  – 必須符合 overloading 的規定, 依照參數決定
  – 在 object 安排好記憶體之後要立刻被執行的程式

• Destructor 最多只能有一個, 也可以不寫
  – 不能有任何參數
  – 就是與 Class 同名且左邊帶有尾巴 ( ~ ) 的函數
  – 在 object 佔用之記憶體還掉之前被瞬間執行的程式

• 一般函數都有 return type
• Constructor 和 Destructor 都沒有 return type
                       連 void 都不能寫           38

Contenu connexe

Tendances

Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Platonov Sergey
 
Коварный code type ITGM #9
Коварный code type ITGM #9Коварный code type ITGM #9
Коварный code type ITGM #9Andrey Zakharevich
 
The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84Mahmoud Samir Fayed
 
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Sungchul Park
 
Python opcodes
Python opcodesPython opcodes
Python opcodesalexgolec
 
Drizzles Approach To Improving Performance Of The Server
Drizzles  Approach To  Improving  Performance Of The  ServerDrizzles  Approach To  Improving  Performance Of The  Server
Drizzles Approach To Improving Performance Of The ServerPerconaPerformance
 
Writable Foreign Data Wrapper (JPUG Unconference 16-Feb-2013)
Writable Foreign Data Wrapper (JPUG Unconference 16-Feb-2013)Writable Foreign Data Wrapper (JPUG Unconference 16-Feb-2013)
Writable Foreign Data Wrapper (JPUG Unconference 16-Feb-2013)Kohei KaiGai
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...corehard_by
 
Metaprogramming in julia
Metaprogramming in juliaMetaprogramming in julia
Metaprogramming in julia岳華 杜
 
OPL best practices - Doing more with less easier
OPL best practices - Doing more with less easierOPL best practices - Doing more with less easier
OPL best practices - Doing more with less easierAlex Fleischer
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptMiao Siyu
 
Exploit exercises.com stack-overflows
Exploit exercises.com stack-overflowsExploit exercises.com stack-overflows
Exploit exercises.com stack-overflowscommiebstrd
 
Planet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance EnhancementPlanet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance Enhancementup2soul
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAndrey Karpov
 
20171127 當julia遇上資料科學
20171127 當julia遇上資料科學20171127 當julia遇上資料科學
20171127 當julia遇上資料科學岳華 杜
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.Russell Childs
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_functiontimotheeg
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notesRajiv Gupta
 

Tendances (20)

Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.
 
Коварный code type ITGM #9
Коварный code type ITGM #9Коварный code type ITGM #9
Коварный code type ITGM #9
 
The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84
 
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
 
Drizzles Approach To Improving Performance Of The Server
Drizzles  Approach To  Improving  Performance Of The  ServerDrizzles  Approach To  Improving  Performance Of The  Server
Drizzles Approach To Improving Performance Of The Server
 
Writable Foreign Data Wrapper (JPUG Unconference 16-Feb-2013)
Writable Foreign Data Wrapper (JPUG Unconference 16-Feb-2013)Writable Foreign Data Wrapper (JPUG Unconference 16-Feb-2013)
Writable Foreign Data Wrapper (JPUG Unconference 16-Feb-2013)
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
 
Metaprogramming in julia
Metaprogramming in juliaMetaprogramming in julia
Metaprogramming in julia
 
OPL best practices - Doing more with less easier
OPL best practices - Doing more with less easierOPL best practices - Doing more with less easier
OPL best practices - Doing more with less easier
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Exploit exercises.com stack-overflows
Exploit exercises.com stack-overflowsExploit exercises.com stack-overflows
Exploit exercises.com stack-overflows
 
Planet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance EnhancementPlanet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance Enhancement
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up Telephony
 
OpenXR 0.90 Overview Guide
OpenXR 0.90 Overview GuideOpenXR 0.90 Overview Guide
OpenXR 0.90 Overview Guide
 
20171127 當julia遇上資料科學
20171127 當julia遇上資料科學20171127 當julia遇上資料科學
20171127 當julia遇上資料科學
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
PathOfMostResistance
PathOfMostResistancePathOfMostResistance
PathOfMostResistance
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
 

Similaire à Oop 1

C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queueAmit Vats
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)CODE BLUE
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxbradburgess22840
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and UtilitiesPramod Kumar
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenPostgresOpen
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfrrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfYodalee
 
C++totural file
C++totural fileC++totural file
C++totural filehalaisumit
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfmohdjakirfb
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
エンタープライズ・クラウドと 並列・分散・非同期処理
エンタープライズ・クラウドと 並列・分散・非同期処理エンタープライズ・クラウドと 並列・分散・非同期処理
エンタープライズ・クラウドと 並列・分散・非同期処理maruyama097
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 ProcessShu-Yu Fu
 

Similaire à Oop 1 (20)

C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
Unit 4
Unit 4Unit 4
Unit 4
 
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
Templates
TemplatesTemplates
Templates
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
 
justbasics.pdf
justbasics.pdfjustbasics.pdf
justbasics.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfrrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
 
C++totural file
C++totural fileC++totural file
C++totural file
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
 
Stack
StackStack
Stack
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
エンタープライズ・クラウドと 並列・分散・非同期処理
エンタープライズ・クラウドと 並列・分散・非同期処理エンタープライズ・クラウドと 並列・分散・非同期処理
エンタープライズ・クラウドと 並列・分散・非同期処理
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Lecture5
Lecture5Lecture5
Lecture5
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 Process
 
C++ tutorial
C++ tutorialC++ tutorial
C++ tutorial
 

Dernier

Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 

Dernier (20)

Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 

Oop 1

  • 1. Object-Oriented Programming (OOP) - Part I Intro. to Computer Science (II) Dept. of Communication Engineering Instructor: Ting-Yu Lin
  • 2. Agenda • Review: parameter passing • Review: design and implement a STACK • 物件導向 (Object-Oriented) 程式設計概念 • 建構元與解構元 (Constructor & Destructor) • 繼承關係 (Inheritance) 與軟體再利用 • 多型概念 (Polymorphism) 或叫“同名異式” 以繼承為基礎 2
  • 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)
  • 8. 使用 STACK extern void push(int); /* 宣告 */ extern int pop( ); #include <stdio.h> int main( ) { push(543); push(881); printf( "%dn", pop( ) ); } /* 可以用! 但若要兩個 Stack 呢? */ 8
  • 9. More about STACK (1/3) /* 若要兩個 Stack 呢? */ 解法之一 ==> 使用兩個 array, 並把 array 當參數 push(ary2, x); ans = pop(ary2); 但是這樣該些 array 必須 開放讓使用者知道 要讓宣告 extern就看得到, 不可再用 static Global 違反不必讓使用者知道push到哪去的 Information Hiding 原則 ! 只要看得到就可能不小心改到 (或是該些 array 要定義在使用者程式裡面) 9
  • 10. More about STACK (2/3) /* 若要兩個 Stack 呢? */ 解法之二 ==> 整個檔案複製到另一個 file, 並把各相關函數 名稱都改掉, 例如 push2, pop2, … ==> array 與變數名稱不用改! Why? (因為 static Global) 但是這樣 也不是很方便, 函數名稱一大堆 若要三個 Stack 呢? 四個呢? … (不過這比前面使用不同 array 的方法好!) Why? 因為沒違反資料隱藏原則 10
  • 11. More about STACK (3/3) /* 若要兩個 Stack 呢? */ /* 若要三個 Stack 呢? 四個? 五個 . . . */ /* 有沒有更方便直接的方法? */ solution ==> using C++ Class to define a Stack as a Software Component 可否讓堆疊用起來像物件? (軟體元件或零件) Stack x; Stack y; x.push(13579); y.push(258); y.push( x.pop( ) ); 11
  • 12. 宣告部份 Stack -- class example (1/2) mystk.h class Stack { // private: long data[99]; /* 直接寫 99 不是好習慣 */ int sptr; // 不能寫 int sptr = -1; public: Stack( ) ; /* constructor */ void push( long ); long pop( ); bool isEmpty( ); }; /* class Stack */ bool 要新版的 C++ 才有, 如果不能用 bool 就改用 int 12
  • 13. 真正定義 Stack -- class example (2/2) mystk.cpp #include "mystk.h" Initialization 工作 #include <iostream.h> 要寫在特殊函數 Stack::Stack( ) { sptr = -1; } /* constructor */ void Stack::push(long xx) { /* 注意 Stack:: */ data[ ++sptr ] = xx; } long Stack::pop( ) { return data[sptr--]; } bool Stack::isEmpty( ) { return sptr == -1; } // bool 要新版的 C++ 才有 這是實作出 Stack, 前面 mystk.h 只是宣告 13
  • 14. Stack (之使用) mymain.cpp #include "mystk.h" 注意: #include <iostream.h> #include <iostream.h> 是 int main( ) { 舊的寫法; Stack xo, brandy; 新的寫法是: xo.push(880); #include <iostream> xo.push(770); 而且要寫: xo.push(53); while( !xo.isempty( ) ) { using namespace std; cout << " " << 因為 C++程式庫在 std xo.pop( ); 命名空間內 } cout << endl; return 0; } 14
  • 15. Stack (之使用) 如何執行? #include "mystk.h" g++ mymain.cpp mystk.cpp #include <iostream.h> int main( ) { ./a.out Stack xo, brandy; xo.push(880); g++ -c mystk.cpp xo.push(770); g++ mymain.cpp mystk.o xo.push(53); ./a.out while( !xo.isempty( ) ) { cout << " " << xo.pop( ); } cout << endl; return 0; 53 770 880 } 15
  • 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
  • 29. Class (Object) Constructor (2/4) #include <iostream> class Student { // private: long sid; public: long showNumber( ) { return sid; } void setId(long xx) { sid = xx; } Student(long x) { sid = x; } 加入這constructor }; Student x; 有問題! int main( ) { 因為 default constructor Student x; // Error.. 不能用了 – Student y(456); // OK now! 有兩種解決方法: x.setId(123); return 0; Student y(456) ; 現在OK了 } 29
  • 30. Class (Object) Constructor (3/4) #include <iostream> class Student { // private: long sid; public: long showNumber( ) { return sid; } void setId(long x) { sid = x; } Student(long x = 0) { sid = x; } }; int main( ) { Student x, y(456); // OK! Student x; 有問題 x.setId(123); 解決方法一 使用 default 參數 return 0; } 30
  • 31. Class (Object) Constructor (4/4) #include <iostream> class Student { // private: long sid; public: long showNumber( ) { return sid; } void setId(long xx) { sid = xx; } Student(long x) { sid = x; cout << "Hehehen"; } Student( ) { sid = 0; cout << "Hahan"; } }; int main( ) { Student x, y(456); // OK! Student x; 有問題 x.setId(123); 解決方法之二 return 0; 再加一個沒參數的constructor } 31
  • 32. Summary of Constructor Constructor 就是與 Class 同名的函數 Constructor 函數沒有 type, 不可以寫 return type Constructor 將在 object 被安排好記憶體後立即執行, 所 以通常用來設定初值 (initial value) Student x; // 這樣會幫 x 安排一塊記憶體, 並執行 x.Student( ); Student *p; // 這樣只有指標, 沒有生出 Student, 不會執行 constructor, 要 p = new Student( ); 才會執行 constructor Object 就是 Class 的一個 instance (案例); 就是 變數 啦 Constructor 可以不寫, 此時等於有一個沒參數且不做事的 Default Constructor 只要寫了一個 Constructor, 則原先的 Default Constructor 就自動消失不能用了 Constructor 也可以寫很多個, 自動依參數決定用哪個 此即 Function name overloading (函數名稱重複用) 的概念 32
  • 33. Destructor 在 object 佔用之記憶體還掉之前 被瞬間執行的程式 (函式) 33
  • 34. Object Destructor dtest.cpp #include <iostream> using namespace std; class Student { // private: long sid; public: long showNumber( ) { return sid; } void setId(long xx) { sid = xx; } Student(long x) { sid = x; cout << x << " Hohohon"; } Student( ) { sid = 38; cout << "Hahan"; } ~Student( ) { cout << sid << " Arrrrh 死了n"; } }; 這是 destructor void test( ) { Student x; cout << " ==到此一遊== "; } Student xxx(123); int main( ) { cout << " *** 開飯了 ***n"; Student y(456); test( ); // 會跳入 test( ) , 注意會做哪些事? return 0; } 34
  • 37. Output of previous program dtest.cpp mytest/> g++ dtest.cpp mytest/> ./a.out 123 Hohoho *** 開飯了*** 456 Hohoho Haha ==到此一遊== 38 Arrrrh 死了 456 Arrrrh 死了 123 Arrrrh 死了 mytest/> exit 37
  • 38. Constructor vs. Destructor Compiler 可以分辨的 • Constructor 可以寫很多個 原則之下 – 必須符合 overloading 的規定, 依照參數決定 – 在 object 安排好記憶體之後要立刻被執行的程式 • Destructor 最多只能有一個, 也可以不寫 – 不能有任何參數 – 就是與 Class 同名且左邊帶有尾巴 ( ~ ) 的函數 – 在 object 佔用之記憶體還掉之前被瞬間執行的程式 • 一般函數都有 return type • Constructor 和 Destructor 都沒有 return type 連 void 都不能寫 38