入门教程:实例详解C 友元3
来源:百度文库 编辑:神马文学网 时间:2024/04/29 14:35:00
示例代码如下:
#include
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。
在编程中,我们使用友元的另外一个重要原因是为了方便重载操作符的使用,这些内容我们将在后面的教程着重讨论!
#include
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。
在编程中,我们使用友元的另外一个重要原因是为了方便重载操作符的使用,这些内容我们将在后面的教程着重讨论!
入门教程:实例详解C 友元3
入门教程:实例详解C 友元2
入门教程:实例详解C 1
c 友元的入门教程
初学者.唱歌入门教程详解
视频: 初学者.唱歌入门教程详解
视频: 初学者.唱歌入门教程详解
实例详解头肩顶形态
通过实例详解Photoshop通道
《千字文》详解C
L/C操作详解
Struts2.0+ibatis2.3整合实例实现服务层及Struts - 51CTO.C...
二级C语言实例解答 - 软件屋
C 程序开发经典实例之一 - 软件屋
C 程序开发经典实例之一 - 软件屋
围棋入门教程3
FLASH8入门教程3
入门教程
入门教程
BIOS标准设置故障实例详解123
BIOS标准设置故障实例详解2
PS图层混合模式实例详解
max的材质详解及实例
PS图层混合模式实例详解