類的三大特性

來源:互聯網
上載者:User

   2012-7-19

今日上網查了類的三大特性:繼承、多態、封裝。

封裝的概念好比一台電腦,你學電腦的時候只需學會諸如開機,關機,裝軟體等即可,無需去瞭解它的內部是如何發動。只要這些東西封裝起來我們能夠用就行了。

 

繼承,繼承起到了對類在次分類的作用,比如,有個類再次分類的作用,比如,有個類“動物”,“哺乳動物”繼承“動物”,再往下“馬”又繼承了“哺乳動物”這個類。在這裡,我們從下往上講,首先,我們把某種東西劃分出來,叫做“馬”(還有“牛”、“魚”等),接著,我們發現,“馬”,“羊”等還有很多共同的特點,於是,我們再次分類,我們則有了“動物”。但實際開發中,我們一般是從上往下定義的,即先有了“動物”,再有“哺乳動物”,最後有“馬”。

 

多態允許你將父物件設定成為和一個或更多個的他的子物件相等的技術,賦值之後,父物件就可以根據當前賦值給它的子物件的特性以不同的方式運作。簡單的說:允許將子類類型的指標賦給父類類型的指標。

對以上的理解:因為父類中有的,子類中都有,但是子類中有的,父類中不一定有。所以,父親指標指向子類,使用的操作肯定正確但是子類指標指向父類,訪問的方法可能父類中沒有,會出錯。

 

 

關鍵字private
標識只能通過公用成員函數訪問的類成員(資料隱藏)。

關鍵字public
標識組成類的公用介面的類成員(抽象)。

 

 

 

編寫了一個類的程式:

stack.h

#ifndefSTACK_H_

#defineSTACK_H_

 

typedef unsigned
long Item;

 

classStack

{

private:

    enum {MAX =10};   
//constantspecific to class

    Item items[MAX];    //holds stackitems

    inttop;           
//indexfor top stack item

public:

    Stack();

    boolisempty()const;

    boolisfull()const;

    //push()returnsfalse if stack already is full true otherwise

    bool push(const Item & item);     
//add item tostack

    //pop()returnsfalse if stack already is empty,true otherwise

    boolpop(Item & item);            
//pop top into item

 

};

 

#endif

 

stack.cpp

#include "stack.h"

Stack::Stack()    //create an emptystack

{

    top = 0;

}

 

boolStack::isempty()const

{

    return top== 0;

}

 

boolStack::isfull()const

{

    return top== MAX;

}

 

boolStack::push(const Item & item)

{

    if(top <MAX)

    {

       items[top++] = item;

        return true;

    }

    else

        return false;

}

 

boolStack::pop(Item & item)

{

    if(top >0)

    {

       item = items[--top];

        return true;

    }

    else

        return false;

}

 

stacker.cpp

#include<iostream>

#include<cctype>  

#include"stack.h"

intmain()

{

    using namespace std;

    Stack st;   //create an empty stack

    char ch;

    unsigned long po;

    cout << "Pleaseenter A to add a purchase order, \n"

        <<"P to process a PO,or Q to quit.\n";

    while(cin>> ch && toupper(ch) !=
'Q')

    {

        while(cin.get()!='\n')

            continue;

        if(!isalpha(ch))

        {

            cout << '\a';

            continue;

        }

        switch(ch)

        {

            case 'A':

            case 'a': cout <<
"Entera PO number to add: ";

                cin>> po;

                if(st.isfull())

                    cout << "stackalready empty\n";

                else

                    st.push(po);

                break;

            case 'P':

            case 'p':
if(st.isempty())

                          cout << "stackalready empty\n";

                      else

                      {

                          st.pop(po);

                          cout << "PO#" << po <<"popped\n";

                      }

                      break;

 

        }

        cout<< "Please enter A to add a purchaseorder, \n"

            << "Pto process a PO, or Q to quit. \n";

    }

    cout << "Bye\n";

    getchar();

    return 0;

}

 

運行如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.