#include <iostream>
using namespace std;
class Internet;
class Country
{
public:
Country()
{
strcpy(cname,"中國");
}
friend class Internet;//友類(lèi)的聲明
protected:
char cname[30];
};
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
void Editcname(Country &temp);
protected:
char name[20];
char address[20];
};
void Internet::Editcname(Country &temp)
{
strcpy(temp.cname,"中華人民共和國");
}
void main()
{
Internet a("中國軟件開(kāi)發(fā)實(shí)驗室","www.cndev-lab.com");
Country b;
a.Editcname(b);
cin.get();
}
在上面的代碼中我們成功的通過(guò)Internet類(lèi)Editcname成員函數操作了Country類(lèi)的保護成員cname。
在編程中,我們使用友元的另外一個(gè)重要原因是為了方便重載操作符的使用,這些內容我們將在后面的教程著(zhù)重討論!
聯(lián)系客服