C++友元

        类具有数据封装和隐藏的特性,只有类的成员函数才能访问类的私有成员和保护成员,外部函数只能访问的公有成员。但在某些情况下,需要在类的外部访问类的私有成员和保护成员。这时,如果通过公有成员函数进行访问,由于参数传递、类型检查和安全性检测等需要时间上的开销,将影响程序的运行效率。为解决这个问题,引入了友元。友元可以在类外部直接访问类的私有成员和保护成员,提高了程序的运行效率。
        友元提供了不同类或对象的成员函数,类的成员函数与一般函数之间进行数据共享的机制。对于一个类,可以利用friend关键字将一般函数、其他类的成员函数或者是其他类声明为该类的友元,使得这个类中本来隐藏的信息(包括私有成员和保护成员)可以被友元所访问。
        11.1        友元函数
        友元函数不是当前类的成员函数,而是独立于当前的外部函数(包括普通函数和其他类的成员函数),但它可以访问该类的所有成员,包括私有成员、保护成员和公有成员。
        友元函数要在类定义时声明,声明时要在其函数名前面加上关键字friend。该声明可以放在公有部分,也可以放在私有部分或是保护部分。友元函数的定义通常在类外部。
        例1        分析下列程序的输出结果

#include<iostream>
using namespace std;
class Time;
class Date
{
private:
	int year, month, day;
public:
	Date(int y = 2003, int m = 1, int d = 1) :year(y), month(m), day(d) {}
	Date(const Date& d)
	{
		day = d.day;
	}
private:	
	friend void Datetime(const Date& d, const Time& t);
};
class Time
{
private:
	int hour, minute, second;
public:
	Time(int h = 0, int m = 0, int s = 0) :hour(h), minute(m), second(s) {}
	friend void Datetime(const Date& d,const Time& t);
};
void Datetime(const Date& d,const Time& t)
{
	cout << "现在是" << d.year << "." << d.month << "." << d.day ;
	cout << " " << t.hour << "." << t.minute << "." << t.second << endl;

}
int main()
{
	Date d(2003, 10, 10);
	Time t(10, 10, 10);
	Datetime(d, t);
	return 0;
}

         除了可以将普通函数声明为类的友元函数以外,也可以将另一个类的成员函数声明为一个类的友元函数,有时候也把类的成员函数作为友元的情况称为友元成员。友元成员的使用和一般友元函数的使用基本相同,只是在使用该友元成员时要通过相应的类和对象名进行访问。
        例2        分析下列程序的输出结果

#include<iostream>
using namespace std;
class Time;
class Date
{
private:
	int year, month, day;
public:
	Date(int y = 2003, int m = 1, int d = 1) :year(y), month(m), day(d) {}
	Date(const Date& d)
	{
		day = d.day;
	}
	void Datetime(const Date& d, const Time& t);

};
class Time
{
private:
	int hour, minute, second;
public:
	Time(int h = 0, int m = 0, int s = 0) :hour(h), minute(m), second(s) {}
	friend void Date::Datetime(const Date& d,const Time& t);
};
void Date::Datetime(const Date& d,const Time& t)
{
	cout << "现在是" << d.year << "." << d.month << "." << d.day ;
	cout << " " << t.hour << "." << t.minute << "." << t.second << endl;
}
int main()
{
	Date d(2003, 10, 10);
	Time t(10, 10, 10);
	d.Datetime(d, t);
	return 0;
}

        11.2        友元类
        友元除了可以是函数外,还可以是类,即一个类可以作为另一个的友元,称为友元类。友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。
        友元类的说明方法如下:
                friend<类名>;
        例2        分析下列程序的输出结果

#include<iostream>
using namespace std;
class Time;
class Date
{
private:
	int year, month, day;
public:
	Date(int y = 2003, int m = 1, int d = 1) :year(y), month(m), day(d) {}
	Date(const Date& d)
	{
		day = d.day;
	}
	void Datetime(const Date& d, const Time& t);

};
class Time
{
	friend  Date;
private:
	int hour, minute, second;
public:
	Time(int h = 0, int m = 0, int s = 0) :hour(h), minute(m), second(s) {}
};
void Date::Datetime(const Date& d,const Time& t)
{
	cout << "现在是" << d.year << "." << d.month << "." << d.day ;
	cout << " " << t.hour << "." << t.minute << "." << t.second << endl;
}
int main()
{
	Date d(2003, 10, 10);
	Time t(10, 10, 10);
	d.Datetime(d, t);
	return 0;
}

        上述程序中注意友元仅仅是在类中进行声明,它不属于当前类的作用域,因此友元也不受类中访问控制权限的控制。

参考《全国计算机等级考试二级教程——C++语言程序设计》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值