C++笔记(6)友元.
下面我们来看一段代码,看看我们是如何利用友元来访问类的一切成员的。
#include <iostream>
using namespace std;
class Internet


{
public:
Internet(char *name,char *address)

{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void ShowN(Internet &obj);//友元函数的声明
public:
char name[20];
char address[20];
};
void ShowN(Internet &obj)//函数定义,不能写成,void Internet::ShowN(Internet &obj)


{
cout<<obj.name<<endl;
}
void main()


{
Internet a("中国软件开发实验室","www.cndev-lab.com");
ShowN(a);
cin.get();
}
1
#include <iostream>
2
using namespace std;
3
class Internet
4

{
5
public:
6
Internet(char *name,char *address)
7
{
8
strcpy(Internet::name,name);
9
strcpy(Internet::address,address);
10
}
11
friend void ShowN(Internet &obj);//友元函数的声明
12
public:
13
char name[20];
14
char address[20];
15
};
16
17
18
void ShowN(Internet &obj)//函数定义,不能写成,void Internet::ShowN(Internet &obj)
19

{
20
cout<<obj.name<<endl;
21
}
22
void main()
23

{
24
Internet a("中国软件开发实验室","www.cndev-lab.com");
25
ShowN(a);
26
cin.get();
27
} 一个普通函数可以是多个类的友元函数,对上面的代码我们进行修改,注意观察变化:

#include <iostream>
using namespace std;
class Country;
class Internet


{
public:
Internet(char *name,char *address)

{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void ShowN(Internet &obj,Country &cn);//注意这里
public:
char name[20];
char address[20];
};
class Country


{
public:
Country()

{
strcpy(cname,"中国");
}
friend void ShowN(Internet &obj,Country &cn);//注意这里
protected:
char cname[30];
};
void ShowN(Internet &obj,Country &cn)


{
cout<<cn.cname<<"|"<<obj.name<<endl;
}
void main()


{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Country b;
ShowN(a,b);
cin.get();
} 一个类的成员函数函数也可以是另一个类的友元,从而可以使得一个类的成员函数可以操作另一个类的数据成员,我们在下面的代码中增加一类Country,注意观察:

#include <iostream>
using namespace std;
class Internet;
class Country


{
public:
Country()

{
strcpy(cname,"中国");
}
void Editurl(Internet &temp);//成员函数的声明
protected:
char cname[30];
};
class Internet


{
public:
Internet(char *name,char *address)

{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void Country::Editurl(Internet &temp);//友元函数的声明
protected:
char name[20];
char address[20];
};
void Country::Editurl(Internet &temp)//成员函数的外部定义


{
strcpy(temp.address,"edu.cndev-lab.com");
cout<<temp.name<<"|"<<temp.address<<endl;
}
void main()


{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Country b;
b.Editurl(a);
cin.get();
}

整个类也可以是另一个类的友元,该友元也可以称做为友类。友类的每个成员函数都可以访问另一个类的所有成员。
示例代码如下:

#include <iostream>
using namespace std;
class Internet;
class Country


{
public:
Country()

{
strcpy(cname,"中国");
}
friend class Internet;//友类的声明
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("中国软件开发实验室","www.cndev-lab.com");
Country b;
a.Editcname(b);
cin.get();
} 在上面的代码中我们成功的通过Internet类Editcname成员函数操作了Country类的保护成员cname。
在编程中,我们使用友元的另外一个重要原因是为了方便重载操作符的使用,这些内容我们将在后面的教程着重讨论!
参考:
http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/571623_1.html