資料抽象化:將資料的規格與作用在此型態上的運算描述在一個語法單元中,並提供介面,讓使用者使用這個資料型態。

 

動態繫結:用父類別定義的參數,也可以用子類別物件來傳入,而到底是呼叫誰的方法,要等到執行時才決定。

#include <iostream>
#include <cstdlib>
using namespace std;

class Base
{
      public:
             virtual void iam()
             {
                     cout << "Base" <<endl;
             }
};
class Base1:public Base
{
      public:
             void iam()
             {
                  cout <<"Base1"<<endl;
             }
};
class Base2:public Base
{
      public:
             void iam()
             {
                  cout <<"Base2"<<endl;
             }
};
int main()
{
    Base* a=new Base;
    a->iam();
    delete a;
    
    a=new Base1;
    a->iam();
    delete a;
    
    a=new Base2;
    a->iam();
    delete a;
    
    
    system("pause");
    return 0;
}

若將關鍵字virtual去除,編譯器只知道變數a指向的是基底類別Base,而a真正指向之物件型態則無法得知。

 

繼承:在設計新類別時,使用已經存在的類別來衍生。達成程式碼再利用與減少重複描述,產生可靠度較高的軟體;但過度運用繼承會增加程式的複雜性,也可能使測式工作更為繁重複雜。

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Rinoa 的頭像
    Rinoa

    褪色的世界.斑剝的記憶

    Rinoa 發表在 痞客邦 留言(0) 人氣()