2. More emphasis is given on data rather than procedure
Programs are divided into objects
Classes are designed such that they characterize the objects
The data and the functions that can operate on the data are
tied together.
Data is hidden and cannot be accessed by external functions.
New data and functions can be added whenever required.
Follows bottom-up Design approach.
Object Oriented Programming
2
3. C++ language was developed by Bjarne Strousstrup at
AT & T Bells Laboratories in early 1980’s
He thought of a language which can have object
oriented features as well as it can retain the simplicity
of C language.
3
Bjarne Strousstrup
4. Initially the language was names as ‘C with classes’.
However, later in 1983 the name was changed to C+
+.
C++ almost supports all the C functionalities with
some new functionalities like classes, inheritance,
function overloading, operator overloading. These
features of C++ enable creating abstract data types,
inherit properties from existing data types etc.
4
12. Class public Inheritance
12
1. code re-usable : data, function 出現在
base class, 則 derived class 也會有一份 。
2. 若 derived class 的 data 使用了和 base
class
相同的名稱 , 則 derived class 存取 base
class 的同名變數,需要使用 :: 明確指定是存
取 base class 的變數
3. 若 derived class 的 function 使用了和 base
class 相同的名稱 , 則 derived class 呼叫
base class 的同名函式,需要使用 :: 明確指
定是呼叫 base class 的函數
4. derived class 的 function 使用了和 base
class 相同的名稱 , 表示要進行 function
overloading
private:
data
function
public:
data A
function
public :
private :
data B
function
data A
function
visible to
derived class object
13. Class private Inheritance
13
1. 繼承方式不同 , 使得 derived class 有
不同的 visibility, 產生出不同的存取權
限
2. 以 private 繼承 , 繼承的東成為
Private, 便不能直接存取 / 呼叫 , 只能
藉由成員函式來操作 private 的內容物
Class A *p;
Class B ob;
Class A* p=&ob; --> error
private:
data
function
public:
data
function
public :
private :
data
function
data
function
invisible to
derived class object
class A
class B
17. A virtual function is a member function that is
declared within a base class and redefined by a
derived class.
虛擬函式 (virtual function) 的意義是此函式在父類
別是虛構的,需要子類別提供實作版本
當程式中以父類別的指標使用子類別的 instance
的函式成員的時候,會喚起的會是這些子類別自行
提供的版本。換句話說,這些在父類別被宣告為虛
擬 (virtual) 的函式,在父類別中的原型宣告可說是
一種程式介面 (interface) 的定義,而子類別所提供
的實作版本則稱為這些函式成員的實現 (realize)
虛擬函式
17
19. Virtual function call is resolved at run-time
(dynamic binding) whereas the non-virtual member
functions are resolved at compile time (static
binding)
Every entry in the virtual table is a pointer that
points to the derived function that is accessible by
that class. A hidden pointer is added by a compiler
to the base class which in turn calls *_vptr which is
automatically set when an instance of the class is
created and it points to the virtual table for that
class.
19