第二十章友元类与嵌套类 1友元类 简单

//第二十章友元类与嵌套类 1友元类
//与函数相同,类也可以作为另一个类的友元类,这样,另一个类的私有及其保护成员都会暴露给友元类,友元类可以通过自己的访问来访问另一个类的所有成员,但是,这并不代表另一个类可以访问它的友元类的所有成员
/*#include <iostream>
using namespace std;

//首先定义TV类,也就是电视机类
class TV
{
public:
	friend class Tele; //定义一个友元函数Tele;
	TV():on_off(off),volume(21),channel(3),mode(tv){}
	~TV(){}
private:
	enum{on,off};  //开关
	enum{tv,av};   //模式
	enum{minve,maxve=100}; //频道
	enum{mincl,maxcl=60};  //音量
	bool on_off; //开关
	int volume;  //频道
	int channel; //音量
	int mode;    //模式
};

class Tele
{
public:
	void SetMode(TV&t);
	void OnOff(TV&t);
	bool VolumeUp(TV&t);
	bool VolumeDown(TV&t);
	bool ChannelUp(TV&t);
	bool ChannelDown(TV&t);
	void Show(TV&t);
};

//显示函数
void Tele::Show(TV&t)
{
	if(t.on_off ==t.on)
	{
		cout<<"电视机现在是"<<(t.on_off==t.on?"开启":"关闭")<<endl;    
		cout<<"音量大小是"<<t.channel<<endl;
		cout<<"信号接收模式是"<<(t.mode==t.av?"AV":"TV")<<endl;
		cout<<"频道是"<<t.volume<<endl;
	}else{
	   cout<<"电视机现在是"<<(t.on_off==t.on?"开启":"关闭")<<endl;    
	}
}

//减少音量
bool Tele::ChannelDown(TV&t)
{
	if(t.channel > t.mincl){
		t.channel --;
		return true;
	}else{
	    return false;
	}
}


//添加音量
bool Tele::ChannelUp(TV&t)
{
	if(t.channel < t.maxcl){
		t.channel++;
		return true;
	}else{
	    return false;
	}
}

//减少频道
bool Tele::VolumeDown(TV&t)
{
	if(t.volume > t.minve){
		t.volume --;
		return true;
	}else{
	    return false;
	}
}


//添加频道
bool Tele::VolumeUp(TV&t)
{
	if(t.volume < t.maxve)
	{
		t.volume ++;
		return true;
	}else{
	    return false;
	}
}


//设置开关
void Tele::OnOff(TV&t)
{
	t.on_off = (t.on_off == t.off) ? t.on : t.off;
}


//设置模式
void Tele::SetMode(TV&t)
{
	t.mode = (t.mode==t.tv) ? t.av : t.tv;
};




int main()
{
	TV tv;
	Tele tele;
	tele.Show(tv);
	tele.OnOff(tv);
	tele.Show(tv);
	cout<<"调大声音"<<endl;
	tele.ChannelUp(tv);
	cout<<"看下一个频道"<<endl;
	tele.VolumeDown(tv);
	cout<<"转换一下模式"<<endl;
	tele.SetMode(tv);
	tele.Show(tv);



    return 0;
}

*/

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2709230.html