日本一本亚洲最大|日本午夜免费啪视频在|国产自产在线视频一区|亚洲福利精品视频

    <object id="4ihfc"></object>
      
      
    1. <object id="4ihfc"></object>
    2. 我要投稿 投訴建議

      計算機等級考試二級C++考點:私有數(shù)據(jù)成員和友元

      時間:2021-01-10 10:47:47 計算機等級 我要投稿

      計算機等級考試二級C++考點:私有數(shù)據(jù)成員和友元

        面向對象的約定就是保證所有數(shù)據(jù)成員的私有性。一般我們都是通過公有成員函數(shù)來作為公共接口來讀取私有數(shù)據(jù)成員的。某些時候,我們稱這樣的函數(shù)為取值和賦值函數(shù)。下面是小編為大家?guī)淼年P于私有數(shù)據(jù)成員和友元的知識,歡迎閱讀。

      計算機等級考試二級C++考點:私有數(shù)據(jù)成員和友元

        一、私有數(shù)據(jù)成員的使用

        1.取值和賦值成員函數(shù)

        面向對象的約定就是保證所有數(shù)據(jù)成員的私有性。一般我們都是通過公有成員函數(shù)來作為公共接口來讀取私有數(shù)據(jù)成員的。某些時候,我們稱這樣的函數(shù)為取值和賦值函數(shù)。

        取值函數(shù)的返回值和傳遞給賦值函數(shù)的參數(shù)不必一一匹配所有數(shù)據(jù)成員的類型。

        #include iostream.h

        class Date

        {

        int mo, da, yr;

        public:

        Date(int m,int d,int y) { mo=m; da=d; yr=y; }

        int getyear() const { return yr; }

        void setyear(int y) { yr = y; }

        };

        int main()

        {

        Date dt(4,1,89);

        cout< dt.setyear(97);

        cout< return 0;

        }

        上面的例子很簡單,不分析了。要養(yǎng)成這樣的習慣,通過成員函數(shù)來訪問和改變類中的數(shù)據(jù)。這樣有利于軟件的設計和維護。比如,改變Date類內(nèi)部數(shù)據(jù)的形式,但仍然用修改過的getyear()和setyear()來提供訪問接口,那么使用該類就不必修改他們的代碼,僅需要重新編譯程序即可。

        2.常量成員函數(shù)

        注意上面的程序中getyear()被聲明為常量型,這樣可以保證該成員函數(shù)不會修改調(diào)用他的對象。通過加上const修飾符,可以使訪問對象數(shù)據(jù)的成員函數(shù)僅僅完成不會引起數(shù)據(jù)變動的那些操作。

        如果程序聲明某個Date對象為常量的話,那么該對象不得調(diào)用任何非常量型成員函數(shù),不論這些函數(shù)是否真的試圖修改對象的數(shù)據(jù)。只有把那些不會引起數(shù)據(jù)改變的函數(shù)都聲明為常量型,才可以讓常量對象來調(diào)用。

        3.改進的成員轉換函數(shù)

        下面的程序改進了從Date對象到CustomDate對象的成員轉換函數(shù),用取值和賦值函數(shù)取代了使用公有數(shù)據(jù)成員的做法。(以前的程序代碼在上一帖中)

        #include iostream.h

        class CustomDate

        {

        int da,yr;

        public:

        CustomDate() {}

        CustomDate(int d,int y) { da=d; yr=y; }

        void display() const {cout< int getday() const { return da; }

        void setday(int d) { da=d; }

        };

        class Date

        {

        int mo,da,yr;

        public:

        Date(int m,int d,int y) { mo=m; da=d; yr=y; }

        operator CustomDate() const;

        };

        Date::operator CustomDate() const

        {

        static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31};

        CustomDate cd(0,yr);

        int day=da;

        for(int i=0;i cd.setday(day);

        return cd;

        }

        int main()

        {

        Date dt(11,17,89);

        CustomDate cd;

        cd=dt;

        cd.display();

        return 0;

        }

        注意上面的'程序中Date::operator CustomDate()聲明為常量型,因為這個函數(shù)沒有改變調(diào)用它對象的數(shù)據(jù),盡管它修改了一個臨時CustomDate對象并將其作為函數(shù)返回值。

        二、友元

        前面已經(jīng)說過了,私有數(shù)據(jù)成員不能被類外的其他函數(shù)讀取,但是有時候類會允許一些特殊的函數(shù)直接讀寫其私有數(shù)據(jù)成員。

        關鍵字friend可以讓特定的函數(shù)或者別的類的所有成員函數(shù)對私有數(shù)據(jù)成員進行讀寫。這既可以維護數(shù)據(jù)的私有性,有可以保證讓特定的類或函數(shù)能夠直接訪問私有數(shù)據(jù)。

        1.友元類

        一個類可以聲明另一個類為其友元,這個友元的所有成員函數(shù)都可以讀寫它的私有數(shù)據(jù)。

        #include iostream.h

        class Date;

        class CustomDate

        {

        int da,yr;

        public:

        CustomDate(int d=0,int y=0) { da=d; yr=y; }

        void display() const {cout< friend Date; //這兒

        };

        class Date

        {

        int mo,da,yr;

        public:

        Date(int m,int d,int y) { mo=m; da=d; yr=y; }

        operator CustomDate();

        };

        Date::operator CustomDate()

        {

        static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31};

        CustomDate cd(0, yr);

        for (int i=0;i cd.da+=da;

        return cd;

        }

        int main()

        {

        Date dt(11,17,89);

        CustomDate cd(dt);

        cd.display();

        return 0;

        }

        在上面的程序中,有這樣一句 friend Date; 該語句告訴編譯器,Date類的所有成員函數(shù)有權訪問CustomDate類的私有成員。因為Date類的轉換函數(shù)需要知道CustomDate類的每個數(shù)據(jù)成員,所以真?zhèn)Date類都被聲明為CustomDate類的友元。

        2.隱式構造函數(shù)

        上面程序對CustomDate的構造函數(shù)的調(diào)用私有顯示該類需要如下的一個轉換構造函數(shù):

        CustomDate(Date& dt);

        但是唯一的一個構造函數(shù)是:CustomDate(int d=0;int y=0);

        這就出現(xiàn)了問題,編譯器要從Date對象構造一個CustomDate對象,但是CustomDate類中并沒有定義這樣的轉換構造函數(shù)。不過Date類中定義了一個成員轉換函數(shù),它可以把Date對象轉換成CustomDate對象。于是編譯器開始搜索CustomDate類,看其是否有一個構造函數(shù),能從一個已存在的CustomDate的對象創(chuàng)建新的CustomDate對象。這種構造函數(shù)叫拷貝構造函數(shù)?截悩嬙旌瘮(shù)也只有一個參數(shù),該參數(shù)是它所屬的類的一個對象,由于CustomDate類中沒有拷貝構造函數(shù),于是編譯器就會產(chǎn)生一個默認的拷貝構造函數(shù),該函數(shù)簡單地把已存在的對象的每個成員拷貝給新對象,F(xiàn)在我們已經(jīng)知道,編譯器可以把Date對象轉換成CustomDate對象,也可以從已存在的CustomDate對象生成一個新的CustomDate對象。那么上面提出的問題,編譯器就是這樣做的:它首先調(diào)用轉換函數(shù),從Date對象創(chuàng)建一個隱藏的、臨時的、匿名的CustomDate對象,然后用該臨時對象作為參數(shù)調(diào)用默認拷貝構造函數(shù),這就生成了一個新的CustomDate對象。

        3.預引用

        上面的例子中還有這樣一句 class Date;

        這個語句叫做預引用。它告訴編譯器,類Date將在后面定義。編譯器必須知道這個信號,因為CustomDate類中引用了Date類,而Date里也引用了CustomDate類,必須首先聲明其中之一。

        使用了預引用后,就可以聲明未定義的類的友元、指針和引用。但是不可以使用那些需要知道預引用的類的定義細節(jié)的語句,如聲明該類的一個實例或者任何對該類成員的引用。

        4.顯式友元預引用

        也可以不使用預引用,這只要在聲明友元的時候加上關鍵自class就行了。

        #include iostream.h

        class CustomDate

        {

        int da,yr;

        public:

        CustomDate(int d=0,int y=0) { da=d; yr=y; }

        void display() const {cout< friend class Date; //這兒,去掉前面的預引用

        };

        class Date

        {

        ... ...

        };

        Date::operator CustomDate()

        {

        ... ...

        }

        int main()

        {

        ... ...

        }

        5.友元函數(shù)

        通常,除非真的需要,否則并不需要把整個類都設為另一個類的友元,只需挑出需要訪問當前類私有數(shù)據(jù)成員的成員函數(shù),將它們設置為該類的友元即可。這樣的函數(shù)稱為友元函數(shù)。

        下面的程序限制了CustomDate類數(shù)據(jù)成員的訪問,Date類中只有需要這些數(shù)據(jù)的成員函數(shù)才有權讀寫它們。

        #include iostream.h

        class CustomDate;

        class Date

        {

        int mo,da,yr;

        public:

        Date(const CustomDate&);

        void display() const {cout< };

        class CustomDate

        {

        int da,yr;

        public:

        CustomDate(int d=0,int y=0) { da=d; yr=y; }

        friend Date::Date(const CustomDate&);

        };

        Date::Date(const CustomDate& cd)

        {

        static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31};

        yr=cd.yr;

        da=cd.da;

        for(mo=0;mo<11;mo++)

        if(da>dys[mo]) da-=dys[mo];

        else break;

        mo++;

        }

        int main()

        {

        Date dt(CustomDate(123, 89));

        dt.display();

        return 0;

        }

        6.匿名對象

        上面main()函數(shù)中Date對象調(diào)用CustomDate類的構造函數(shù)創(chuàng)建了一個匿名CustomDate對象,然后用該對象創(chuàng)建了一個Date對象。這種用法在C++中是經(jīng)常出現(xiàn)的。

        7.非類成員的友元函數(shù)

        有時候友元函數(shù)未必是某個類的成員。這樣的函數(shù)擁有類對象私有數(shù)據(jù)成員的讀寫權,但它并不是任何類的成員函數(shù)。這個特性在重載運算符時特別有用。

        非類成員的友元函數(shù)通常被用來做為類之間的紐帶。一個函數(shù)如果被兩個類同時聲明為友元,它就可以訪問這兩個類的私有成員。下面的程序說明了一個可以訪問兩個類私有數(shù)據(jù)成員的友元函數(shù)是如何將在兩個類之間架起橋梁的。

        #include iostream.h

        class Time;

        class Date

        {

        int mo,da,yr;

        public:

        Date(int m,int d,int y) { mo=m; da=d; yr=y;}

        friend void display(const Date&, const Time&);

        };

        class Time

        {

        int hr,min,sec;

        public:

        Time(int h,int m,int s) { hr=h; min=m; sec=s;}

        friend void display(const Date&, const Time&);

        };

        void display(const Date& dt, const Time& tm)

        {

        cout << dt.mo << '/' << dt.da << '/' << dt.yr;

        cout << ' ';

        cout << tm.hr << ':' << tm.min << ':' << tm.sec;

        }

        int main()

        {

        Date dt(2,16,97);

        Time tm(10,55,0);

        display(dt, tm);

        return 0;

        }

      【計算機等級考試二級C++考點:私有數(shù)據(jù)成員和友元】相關文章:

      計算機等級考試二級C++考點:類的轉換09-21

      計算機等級考試二級C++考點:析構函數(shù)和this指針09-18

      計算機等級考試二級VB考點:進程條和滑塊08-27

      計算機等級考試二級VB考點:自定義數(shù)據(jù)類型08-28

      計算機等級考試二級VB考點:程序調(diào)試08-29

      計算機等級考試二級VB考點:數(shù)組的概念08-29

      計算機等級考試二級VB考點:排列顯示和分層顯示控件08-27

      計算機等級考試二級VB考點:常用文件操作語句和函數(shù)08-27

      計算機等級考試二級VB考點:VB的過程設計08-28