• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++技術(shù)交流

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              6 隨筆 :: 8 文章 :: 1 評論 :: 0 Trackbacks

            類的友元

            一、友元函數(shù)
            一個類的友元函數(shù)是定義在類外部的一個函數(shù),它不是類的成員函數(shù),但可以訪問類的私有成員和公有成員。

            定義格式如下:
            friend?? 返回值類型 友元函數(shù)名<參數(shù)表>

            例:
            #include<iostream.h>
            class add
            {
            private:
            ?int a,b,c;
            public:
            ?void inst(int x,int y);
            ?friend int sum(add &m);
            };
            void add::inst(int x, int y)
            {
            ?a=x;
            ?b=y;
            }
            int sum(add &m)
            {
            ?m.c=m.a+m.b;
            ?return m.c;
            }
            void main()
            {
            ?add t;
            ?t.inst(20,30);
            ?cout<<"sum="<<sum(t)<<endl;
            }


            二、友元類

            一個函數(shù)可以作為一個類的友元,一個類也可以作為另一個類的友元,這種類稱為友元類。

            例:

            #include<iostream.h>
            class A
            {
            private:
            ?int x,y;
            ?friend class B;
            public:
            ?void inst(int a,int b)
            ?{
            ??x=a;
            ??y=b;
            ?}
            ?void show();
            };
            void A::show()
            {
            ?cout<<"x="<<x<<endl;
            ?cout<<"y="<<y<<endl;
            }
            class B
            {
            private:
            ?int z;
            ?A m;
            public:
            ?void add(int a,int b);
            };
            void B::add(int a,int b)
            {
            ?m.x=a;
            ?m.y=b;
            ?z=m.x+m.y;
            ?cout<<"z="<<z<<endl;
            }
            void main()
            {
            ?A m;
            ?B t;
            ?m.inst(3,8);
            ?m.show();
            ?t.add(30,19);
            }


            友元成員


            一個類的成員函數(shù)可以作為另一個類的友元,這種成員函數(shù)稱為友元函數(shù)成員。
            友元成員函數(shù)不僅可以訪問自己所在類中的私有成員,還可以訪問friend聲明所在類中的私有成員。


            #include<iostream.h>
            class B;
            class A
            {
            private:
            ?int x;
            ?int y;
            public:
            ?void inst(int a,int b)
            ?{
            ??x=a;
            ??y=b;
            ?}
            ?void show(B &);
            };
            class B
            {
            private:
            ?int z;
            public:
            ?friend void A::show(B &);
            };
            void A::show(B &m)
            {
            ?m.z=x+y;
            ?cout<<"x="<<x<<endl;
            ?cout<<"y="<<y<<endl;
            ?cout<<"m.z="<<m.z<<endl;
            }
            void main()
            {
            ?A m;
            ?B t;
            ?m.inst(12,18);
            ?m.show(t);
            }

            posted on 2006-06-23 23:18 啊嵩 閱讀(117) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国内精品久久久久影院日本| 久久综合综合久久狠狠狠97色88| 青青青伊人色综合久久| 亚洲?V乱码久久精品蜜桃 | 午夜视频久久久久一区 | 2022年国产精品久久久久| 人人狠狠综合久久亚洲婷婷| 99久久国产宗和精品1上映| 久久综合成人网| 青青青国产精品国产精品久久久久 | 国产精品久久久久9999| 久久久久国色AV免费看图片| 久久精品国产99久久无毒不卡| 亚洲国产成人精品91久久久 | 久久久一本精品99久久精品66| 久久精品人人做人人爽电影| 久久综合九色综合久99| 久久精品麻豆日日躁夜夜躁| 99久久er这里只有精品18| 久久伊人亚洲AV无码网站| 久久婷婷激情综合色综合俺也去| 亚洲午夜久久久久妓女影院| 久久久中文字幕| 国内精品久久久久伊人av | 婷婷国产天堂久久综合五月| 久久伊人亚洲AV无码网站| 久久国产精品久久| 午夜欧美精品久久久久久久| 伊人久久大香线蕉AV色婷婷色| 青春久久| 久久精品国产欧美日韩| 91精品国产91久久| 狠狠色丁香久久综合五月| 亚洲AV日韩精品久久久久| 91精品国产91久久久久久青草 | 久久久久亚洲av无码专区| 久久91亚洲人成电影网站| 91久久精品无码一区二区毛片| 一级A毛片免费观看久久精品| 99久久精品九九亚洲精品| 中文成人久久久久影院免费观看|