代码详解c++ 三种友元函数

#include<iostream> 
#include<string>

using namespace std;


class Screen;
class Dog
{
	public:
		int foo(Screen & screen);
	
};
class Screen
{
	public:	
		 friend int calcArea(Screen &);//对一个函数进行友元 
		 friend class Window_Mgr;//对整个类进行友元 
		 //friend class Dog;
		 friend int Dog::foo(Screen & );//指定类当中的某一个函数为友元 
		typedef std::string::size_type index;
		Screen(int ht=0, int wd=0):contents(ht*wd,' '),cursor(0),height(ht),width(wd){
		}
		int area()const
		{
			return height*width;	
		 }

	private:
		std::string contents;
		index cursor;
		int height,width;	
};

class Window_Mgr//窗口管理类 对Screen进行管理 
{
	public:
		void relocate(int r, int c, Screen &s)
		{
			s.height += r;
			s.width += c;	
		}	
};

int Dog::foo(Screen & screen)  
	{
		return screen.height * screen.width; 
	}	

//这个函数不是类的成员函数 
int calcArea(Screen & screen)
{
	return screen.height*screen.width;	
}
int main()
{
	Screen a(60,100);
	cout<<a.area()<<endl;
	
	Window_Mgr wm;
	wm.relocate(20,100,a);
	cout<<"ok"<<endl;
	
	cout<<calcArea(a)<<endl;
	return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值