Object-Based Programming Part IObject-Based Programming Part I
(( 以物件為基礎的程式設計以物件為基礎的程式設計 ))
Lecturer: Liao Ping-Lun (Lecturer: Liao Ping-Lun ( 廖柄㷍廖柄㷍 ))
EMail:EMail: pinglunliao@gmail.compinglunliao@gmail.com
AgendaAgenda
Overloaded Functions (Overloaded Functions ( 多載化函式多載化函式 ))
Function Templates (Function Templates ( 函式範本函式範本 ))
Class Definition (Class Definition ( 類別定義類別定義 ))
Class Objects (Class Objects ( 類別物件類別物件 ))
Class Members (Class Members ( 類別成員類別成員 ))
Overloaded Operators (Overloaded Operators ( 運算子重載運算子重載 ))
Function OverloadingFunction Overloading
Parameter listParameter list
Ex:Ex: 底下兩個函式有符合重載的定義嗎?底下兩個函式有符合重載的定義嗎?
int max(int, int);int max(int, int);
float max(int, int);float max(int, int);
Overload ResolutionOverload Resolution
Overload.cppOverload.cpp
Function templateFunction template
實作演算法而不管資料的型態。實作演算法而不管資料的型態。
syntax:syntax:
template<typename Data>template<typename Data>
template<class Data>template<class Data>
FunctionTemplate.cppFunctionTemplate.cpp
PracticePractice
寫一個有重載的加總函數。寫一個有重載的加總函數。
double sum( double data[]);double sum( double data[]);
int sum(int data[]);int sum(int data[]);
templatetemplate 版的加總函式。版的加總函式。
主程式主程式 mainmain 在在 PracticePractice 資料夾資料夾
參考答案參考答案
Q & AQ & A
What is OOP?What is OOP?
OObject-bject-OOrientedriented PProgrammingrogramming
EncapsulationEncapsulation
ModularityModularity
PolymorphismPolymorphism
InheritanceInheritance
Object-Oriented Programming LanguageObject-Oriented Programming Language
C++C++
JavaJava
C#C#
Etc.Etc.
What Is Object?What Is Object?
東西東西
An instance of an object.An instance of an object.
An object is a software bundle of related stAn object is a software bundle of related st
ate and behavior.ate and behavior.
What Is Object?What Is Object?
讓我們把世界看成是㆒個由物件( objec
t )所組成的大環境。物件是什麼?白一點
說,「東西」是也!任何實際的物體你都可
以說它是物件。為了描述物件,我們應該先
把物件的屬性描述出來。好,給「物件的屬
性」一個比較學術的名詞,就是「類別」
( class )。
What Is Class?What Is Class?
A class is a blueprint or prototype from whiA class is a blueprint or prototype from whi
ch objects are created.ch objects are created.
SampleSample
BicycleBicycle
AttributeAttribute
BehaviorBehavior
What Is Inheritance?What Is Inheritance?
Inheritance provides a powerful and naturaInheritance provides a powerful and natura
l mechanism for organizing and structuringl mechanism for organizing and structuring
your software.your software.
Class DeclarationClass Declaration
class class_name;class class_name;
class CShape;class CShape;
class Box;class Box;
class Person;class Person;
Class DefinitionClass Definition
class classNameclass className
{{
Member variables;Member variables;
Member functions;Member functions;
};};
Example:Example:
Bicycle Class DiagramBicycle Class Diagram
Member variablesMember variables
Member functionsMember functions
class nameclass name
Visual C++ 6.0Visual C++ 6.0 的使用的使用
將類別的實做檔和主程式分開將類別的實做檔和主程式分開
Project BicycleProject Bicycle
建立建立 classclass
建立建立 class member functionsclass member functions
建立建立 class member variablesclass member variables
除錯除錯 (Debug)(Debug) 方式方式
F5F5 除錯模式除錯模式
F9F9 中斷點中斷點
Exercise 1Exercise 1
請設計一個請設計一個 PointPoint 類別。類別。
參考參考 Bicycle.cppBicycle.cpp
參考答案參考答案
Q & AQ & A
Class ObjectClass Object
C++C++ 中沒有中沒有 ObjectObject 類別。類別。
Java, C#Java, C# 中有中有
這代表什麼?這代表什麼? C++ isn't a pure OOPL.C++ isn't a pure OOPL.
Everything is an Object.Everything is an Object.
ObjectObject
Automatic objects.Automatic objects.
Const objects.Const objects.
Dynamically allocated objects.Dynamically allocated objects.
Exception objects.Exception objects.
Function Objects.Function Objects.
Global objects.Global objects.
Local objects.Local objects.
Object ExampleObject Example
Project: ObjectProject: Object
Exception objects. (Exception Handling)Exception objects. (Exception Handling)
Function Objects. (Standard Template LibrFunction Objects. (Standard Template Libr
ary)ary)
Class MembersClass Members
private (private ( 私有的私有的 ))
protected (protected ( 受保護的受保護的 ))
public (public ( 公開的公開的 ))
public
protected
Access Level ModifiersAccess Level Modifiers
publicpublic
每個人都看得到每個人都看得到
protectedprotected
子類別也可以看到子類別也可以看到
privateprivate
除了自己以外,別人都不能看除了自己以外,別人都不能看
friendfriend
ProjectProject: AccessLevel: AccessLevel
private
Example: PersonExample: Person
在在 PersonPerson 類別裡,成員函式類別裡,成員函式 (member func(member func
tion) void Age() const;tion) void Age() const; 中的中的 constconst 是表示是表示
此函式不得修改此函式不得修改 PersonPerson 類別的成員變數類別的成員變數 (m(m
ember variable)ember variable) 。。
物件陣列物件陣列 (Object Array)(Object Array)
QuestionsQuestions
Your first C++ program.Your first C++ program.
HelloWorld.cppHelloWorld.cpp
private vs publicprivate vs public
Private VS Public.cppPrivate VS Public.cpp
Overloaded Operators (Overloaded Operators ( 運算子重運算子重
載載 ))
Project: PointProject: Point
operator<operator<
friend versionfriend version
friend bool operator<(const Point &p1, const Pfriend bool operator<(const Point &p1, const P
oint &p2)oint &p2) { return p1.x < p2.x && p1.y < p2.y; }{ return p1.x < p2.x && p1.y < p2.y; }
member function versionmember function version
bool Point::operator <(const Point &p) const { rbool Point::operator <(const Point &p) const { r
eturn x < p.x && y < p.y;}eturn x < p.x && y < p.y;}
error C2593: 'operator <' is ambiguouserror C2593: 'operator <' is ambiguous
Overloaded OperatorsOverloaded Operators 練習練習
重載重載 PointPoint 的大於的大於 >> 運算子運算子
friend versionfriend version
member function versionmember function version
有必要兩個都寫嗎?有必要兩個都寫嗎?
Overloaded Operators (Overloaded Operators ( 運算子重運算子重
載載 ))
operator<<operator<<
operator>>operator>>
Why both are friend function?Why both are friend function?
Boundary alignmentBoundary alignment
提示:提示: 44 的倍數、的倍數、 88 的倍數、成員變數順的倍數、成員變數順
序有關序有關
指標指標 (Pointer)(Pointer) 的大小為的大小為 4 Bytes4 Bytes 。。
11 11 11 11 44 44 44
11 11 11 44 44 44 44 44
11 88 11 11 44 88 88 88 44 44
HomeWorkHomeWork
請實作資料結構的堆疊請實作資料結構的堆疊 StackStack 。。
參考參考 CC 版本的版本的 StackStack (在(在 HWHW 資料夾)資料夾)
Data Structures(Stacks).pdfData Structures(Stacks).pdf :觀念講解:觀念講解
Q & AQ & A
ReferencesReferences
C++ How To ProgramC++ How To Program
C++ PrimerC++ Primer
Beginning Visual C++ 6Beginning Visual C++ 6
The JavaThe JavaTMTM
TutorialTutorial
物件導向程式設計物件導向程式設計 hh
ttp://vr.me.ncku.edu.tw/courses/index-oop.htmttp://vr.me.ncku.edu.tw/courses/index-oop.htm
See you next Time.See you next Time.

Object-Based Programming Part One

  • 1.
    Object-Based Programming PartIObject-Based Programming Part I (( 以物件為基礎的程式設計以物件為基礎的程式設計 )) Lecturer: Liao Ping-Lun (Lecturer: Liao Ping-Lun ( 廖柄㷍廖柄㷍 )) EMail:EMail: pinglunliao@gmail.compinglunliao@gmail.com
  • 2.
    AgendaAgenda Overloaded Functions (OverloadedFunctions ( 多載化函式多載化函式 )) Function Templates (Function Templates ( 函式範本函式範本 )) Class Definition (Class Definition ( 類別定義類別定義 )) Class Objects (Class Objects ( 類別物件類別物件 )) Class Members (Class Members ( 類別成員類別成員 )) Overloaded Operators (Overloaded Operators ( 運算子重載運算子重載 ))
  • 3.
    Function OverloadingFunction Overloading ParameterlistParameter list Ex:Ex: 底下兩個函式有符合重載的定義嗎?底下兩個函式有符合重載的定義嗎? int max(int, int);int max(int, int); float max(int, int);float max(int, int); Overload ResolutionOverload Resolution Overload.cppOverload.cpp
  • 4.
    Function templateFunction template 實作演算法而不管資料的型態。實作演算法而不管資料的型態。 syntax:syntax: template<typenameData>template<typename Data> template<class Data>template<class Data> FunctionTemplate.cppFunctionTemplate.cpp
  • 5.
    PracticePractice 寫一個有重載的加總函數。寫一個有重載的加總函數。 double sum( doubledata[]);double sum( double data[]); int sum(int data[]);int sum(int data[]); templatetemplate 版的加總函式。版的加總函式。 主程式主程式 mainmain 在在 PracticePractice 資料夾資料夾
  • 6.
  • 7.
    What is OOP?Whatis OOP? OObject-bject-OOrientedriented PProgrammingrogramming EncapsulationEncapsulation ModularityModularity PolymorphismPolymorphism InheritanceInheritance Object-Oriented Programming LanguageObject-Oriented Programming Language C++C++ JavaJava C#C# Etc.Etc.
  • 8.
    What Is Object?WhatIs Object? 東西東西 An instance of an object.An instance of an object. An object is a software bundle of related stAn object is a software bundle of related st ate and behavior.ate and behavior.
  • 9.
    What Is Object?WhatIs Object? 讓我們把世界看成是㆒個由物件( objec t )所組成的大環境。物件是什麼?白一點 說,「東西」是也!任何實際的物體你都可 以說它是物件。為了描述物件,我們應該先 把物件的屬性描述出來。好,給「物件的屬 性」一個比較學術的名詞,就是「類別」 ( class )。
  • 10.
    What Is Class?WhatIs Class? A class is a blueprint or prototype from whiA class is a blueprint or prototype from whi ch objects are created.ch objects are created. SampleSample BicycleBicycle AttributeAttribute BehaviorBehavior
  • 11.
    What Is Inheritance?WhatIs Inheritance? Inheritance provides a powerful and naturaInheritance provides a powerful and natura l mechanism for organizing and structuringl mechanism for organizing and structuring your software.your software.
  • 12.
    Class DeclarationClass Declaration classclass_name;class class_name; class CShape;class CShape; class Box;class Box; class Person;class Person;
  • 13.
    Class DefinitionClass Definition classclassNameclass className {{ Member variables;Member variables; Member functions;Member functions; };};
  • 14.
    Example:Example: Bicycle Class DiagramBicycleClass Diagram Member variablesMember variables Member functionsMember functions class nameclass name
  • 15.
    Visual C++ 6.0VisualC++ 6.0 的使用的使用 將類別的實做檔和主程式分開將類別的實做檔和主程式分開 Project BicycleProject Bicycle 建立建立 classclass 建立建立 class member functionsclass member functions 建立建立 class member variablesclass member variables 除錯除錯 (Debug)(Debug) 方式方式 F5F5 除錯模式除錯模式 F9F9 中斷點中斷點
  • 16.
    Exercise 1Exercise 1 請設計一個請設計一個PointPoint 類別。類別。 參考參考 Bicycle.cppBicycle.cpp
  • 17.
  • 18.
    Class ObjectClass Object C++C++中沒有中沒有 ObjectObject 類別。類別。 Java, C#Java, C# 中有中有 這代表什麼?這代表什麼? C++ isn't a pure OOPL.C++ isn't a pure OOPL. Everything is an Object.Everything is an Object.
  • 19.
    ObjectObject Automatic objects.Automatic objects. Constobjects.Const objects. Dynamically allocated objects.Dynamically allocated objects. Exception objects.Exception objects. Function Objects.Function Objects. Global objects.Global objects. Local objects.Local objects.
  • 20.
    Object ExampleObject Example Project:ObjectProject: Object Exception objects. (Exception Handling)Exception objects. (Exception Handling) Function Objects. (Standard Template LibrFunction Objects. (Standard Template Libr ary)ary)
  • 21.
    Class MembersClass Members private(private ( 私有的私有的 )) protected (protected ( 受保護的受保護的 )) public (public ( 公開的公開的 ))
  • 22.
    public protected Access Level ModifiersAccessLevel Modifiers publicpublic 每個人都看得到每個人都看得到 protectedprotected 子類別也可以看到子類別也可以看到 privateprivate 除了自己以外,別人都不能看除了自己以外,別人都不能看 friendfriend ProjectProject: AccessLevel: AccessLevel private
  • 23.
    Example: PersonExample: Person 在在PersonPerson 類別裡,成員函式類別裡,成員函式 (member func(member func tion) void Age() const;tion) void Age() const; 中的中的 constconst 是表示是表示 此函式不得修改此函式不得修改 PersonPerson 類別的成員變數類別的成員變數 (m(m ember variable)ember variable) 。。 物件陣列物件陣列 (Object Array)(Object Array)
  • 24.
    QuestionsQuestions Your first C++program.Your first C++ program. HelloWorld.cppHelloWorld.cpp private vs publicprivate vs public Private VS Public.cppPrivate VS Public.cpp
  • 25.
    Overloaded Operators (OverloadedOperators ( 運算子重運算子重 載載 )) Project: PointProject: Point operator<operator< friend versionfriend version friend bool operator<(const Point &p1, const Pfriend bool operator<(const Point &p1, const P oint &p2)oint &p2) { return p1.x < p2.x && p1.y < p2.y; }{ return p1.x < p2.x && p1.y < p2.y; } member function versionmember function version bool Point::operator <(const Point &p) const { rbool Point::operator <(const Point &p) const { r eturn x < p.x && y < p.y;}eturn x < p.x && y < p.y;} error C2593: 'operator <' is ambiguouserror C2593: 'operator <' is ambiguous
  • 26.
    Overloaded OperatorsOverloaded Operators練習練習 重載重載 PointPoint 的大於的大於 >> 運算子運算子 friend versionfriend version member function versionmember function version 有必要兩個都寫嗎?有必要兩個都寫嗎?
  • 27.
    Overloaded Operators (OverloadedOperators ( 運算子重運算子重 載載 )) operator<<operator<< operator>>operator>> Why both are friend function?Why both are friend function?
  • 28.
    Boundary alignmentBoundary alignment 提示:提示:44 的倍數、的倍數、 88 的倍數、成員變數順的倍數、成員變數順 序有關序有關 指標指標 (Pointer)(Pointer) 的大小為的大小為 4 Bytes4 Bytes 。。 11 11 11 11 44 44 44 11 11 11 44 44 44 44 44 11 88 11 11 44 88 88 88 44 44
  • 29.
    HomeWorkHomeWork 請實作資料結構的堆疊請實作資料結構的堆疊 StackStack 。。 參考參考CC 版本的版本的 StackStack (在(在 HWHW 資料夾)資料夾) Data Structures(Stacks).pdfData Structures(Stacks).pdf :觀念講解:觀念講解
  • 30.
    Q & AQ& A
  • 31.
    ReferencesReferences C++ How ToProgramC++ How To Program C++ PrimerC++ Primer Beginning Visual C++ 6Beginning Visual C++ 6 The JavaThe JavaTMTM TutorialTutorial 物件導向程式設計物件導向程式設計 hh ttp://vr.me.ncku.edu.tw/courses/index-oop.htmttp://vr.me.ncku.edu.tw/courses/index-oop.htm
  • 32.
    See you nextTime.See you next Time.