#include <iostream>
using namespace std;
// 前向声明
class TV;
// 友元类声明(其中的几个函数为友元)
class Remote
{
public:
void volume_to(TV &tv, int x);
void channel_to(TV &tv, int x);
};
// 类声明
class TV
{
public:
enum{off=0, on=1};
// 声明友元函数
friend void Remote::volume_to(TV &tv, int x);
friend void Remote::channel_to(TV &tv, int x);
// 成员函数
TV(int st=on):state(st),channel(5),volume(20){};
void onoff(){state^=1; cout<<"now state is "<<(state?"on":"off")<<endl;}
void chan_next(){cout<<"now channel is "<<++channel<<endl;}
void chan_last(){cout<<"now channel is "<<--channel<<endl;}
void vol_larger(){cout<<"now volume is "<<++volume<<endl;}
void vol_smaller(){cout<<"now volume is "<<--volume<<endl;}
private:
int channel;
int volume;
int state;
};
c++ 友元类 练习题
最新推荐文章于 2025-04-23 08:45:00 发布