友元,友元類

來源:互聯網
上載者:User

友元,友元類


1友元:C++控制類對象私人部分的訪問,但有時候需要在類的外部存取類的私人成員,這種情況下C++提供了友元機制。

 建立友元函數步驟:

 A:將函式宣告放到類的聲明中,並在原型前加上關鍵字friend

friend返回值函數名稱(參數列表);

friend classclassname;

 B:編寫友元函數定義,不需要再定義中使用關鍵字friend.

   friend int友元函數名(const classname &a);

#include<iostream>

 

usingnamespacestd;

 

classdemo

{

private:

   inti;//只能在類的內部訪問

public:

   demo()//建構函式

   {

       cout<< "demo"<< this<< endl;

   }

   demo(constdemo&it)//拷貝構造

   {

       //一旦自己實現了拷貝建構函式,類成員之間的賦值就需要自己完成,編譯器不管了

       this->i=it.i;

       cout<< "copydemo" <<this <<endl;

   }

   ~demo()

   {

       cout<< "~demo"<< this<< endl;

   }

   voidset_i(inti)

   {

       this->i=i;

   }

   intget_i()

   {

       returni;

   }

   //意思是聲明一個該類的友元函數,該函數可以調用本類的資訊

   friendvoidtest();

   //意思是聲明一個該類的友元,該類的資訊可以被下面的類調用

   friendclassdemo1;

   //demo和demo0互為友元

   friendclassdemo0;

};

 

voidtest()

{

   //因為這個函數時上面的

   demod;

   //這裡d.i就是demo中的私人的變數,由於使用了友元,所以test可以使用了

   d.i=100;

   printf("%d\n",d.i);

}

 

classdemo0

{

private:

   inti;

public:

   demo0()

   {

       demod;

       d.i=123;

       //列印出了123,說明友元中的類可以訪問類中變數

       printf("%d\n",d.i);

   }

};

 

classdemo1

{

public:

   demo1()

   {

       demod;

       d.i=144;

       printf("%d\n",d.i);

   }

   friendclassdemo2;

};

 

//通過下面的例子得出:我的朋友的朋友,不會是我的朋友

classdemo2

{

public:

   demo2()

   {

       demod;

       //這一句不能放開,如果放開了報錯

       //d.i=155;

   }

};

 

intmain(void)

{

   test();

 

   demo0d0;

 

   demo1d1;

 

   //下面啟動並執行時候報錯了,說明友元的一個特點:我的朋友的朋友,不會是我的朋友

   //demo2d2;

 

   return0;

}

2.運行:

3.友元關係說明圖

 


怎使用友元函數?

友元函數可以訪問類的對象的私人成員變數,成員函數,代碼如下:
class TestClass
{
private:
int length;
int width;

public:
TestClass(int len,int wide){ length=len; width=wide;};
friend int CaculateArea(TestClass test); //注意要加關鍵字friend
friend int CaculateCircle(TestClass test);
};
int CaculateArea(TestClass test) //友元函數可以訪問test對象的私人成員變數length,width
{
return test.length * test.width;
}
int CaculateCircle(TestClass test)
{
return 2*(test.length + test.width);
}

int main()
{
TestClass testClass(2,3);
cout<<CaculateArea(testClass)<<endl;
cout<<CaculateCircle(testClass)<<endl;
return 0;

}
 
怎使用友元函數?

友元函數可以訪問類的對象的私人成員變數,成員函數,代碼如下:
class TestClass
{
private:
int length;
int width;

public:
TestClass(int len,int wide){ length=len; width=wide;};
friend int CaculateArea(TestClass test); //注意要加關鍵字friend
friend int CaculateCircle(TestClass test);
};
int CaculateArea(TestClass test) //友元函數可以訪問test對象的私人成員變數length,width
{
return test.length * test.width;
}
int CaculateCircle(TestClass test)
{
return 2*(test.length + test.width);
}

int main()
{
TestClass testClass(2,3);
cout<<CaculateArea(testClass)<<endl;
cout<<CaculateCircle(testClass)<<endl;
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.