友元类 友元成员函数
友元类
/////////////////////////////////////////
//
// tv.h
//
/////////////////////////////////////////
#ifndef TV_H_
#define TV_H_
class Tv
{
public:
friend class Remote;
enum {off,on};
enum {MinVal,MaxVal=20};
enum {Antenna,Cable};
enum {TV,VCR};
Tv(int s=off,int mc=100);
void onoff(){state=(state==on)?off:on;}
bool ison()const {return state==on;}
bool volup();
bool voldown();
void chanup();
void chandown();
void set_mode() {mode=(mode==Antenna)?Cable:Antenna;}
void set_input() {input=(input==TV)?VCR:TV;}
void settings() const;
private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
};
class Remote
{
private:
int mode; // controls TV or VCR
public:
Remote(int m = Tv::TV) : mode(m) {}
bool volup(Tv & t) { return t.volup();}
bool voldown(Tv & t) { return t.voldown();}
void onoff(Tv & t) { t.onoff(); }
void chanup(Tv & t) {t.chanup();}
void chandown(Tv & t) {t.chandown();}
void set_chan(Tv & t, int c) {t.channel = c;}
void set_mode(Tv & t) {t.set_mode();}
void set_input(Tv & t) {t.set_input();}
};
#endif
////////////////////////////////////////////////
//
//tv.cpp
//
////////////////////////////////////////////////
#include <iostream>
#include "tv.h"
Tv::Tv(int s,int mc):state(s),volume(5),maxchannel(mc),channel(2),mode(Cable),input(TV)
{
}
bool Tv::volup()
{
if (volume<MaxVal)
{
volume++;
return true;
}
else
return false;
}
bool Tv::voldown()
{
if (volume>MinVal)
{
volume--;
return true;
}
else
return false;
}
void Tv::chanup()
{
if (channel<maxchannel)
channel++;
else channel=1;
}
void Tv::chandown()
{
if (channel>1)
channel--;
else
channel=maxchannel;
}
void Tv::settings() const
{
using std::cout;
using std::endl;
cout<<"TV is "<<(state==off?"off":"on")<<endl;
if (state==on)
{
cout<<"volume setting = "<<volume<<endl;
cout<<"channel setting = "<<channel<<endl;
cout<<"mode = "<<(mode==Antenna?"antenna":"cable")<<endl;
cout<<"input = "<<(input==TV?"TV":"VCR")<<endl;
}
}
//////////////////////////////////////////
//
//main.cpp
//
//////////////////////////////////////////
//use_tv.cpp -- using the Tv and Remote classes
#include <iostream>
#include "tv.h"
int main()
{
using std::cout;
Tv s27;
cout << "Initial settings for 27\" TV:\n";
s27.settings();
s27.onoff();
s27.chanup();
cout << "\nAdjusted settings for 27\" TV:\n";
s27.settings();
Remote grey;
grey.set_chan(s27, 10);
grey.volup(s27);
grey.volup(s27);
cout << "\n27\" settings after using remote:\n";
s27.settings();
Tv s32(Tv::on);
s32.set_mode();
grey.set_chan(s32,28);
cout << "\n32\" settings:\n";
s32.settings();
return 0;
}
友元成员函数
排序为:
class Tv; // 3>Remote中需要使用Tv类 所以要在Remote前声明
class Remote {……}; // 2>声明在Tv之前以让Tv中知道Remote是个类 并知道相应用作友元的函数
class Tv {……}; // 1>要使用Remote的成员函数为Tv的友元
Remote中需要用到 Tv 的成员函数 所以因该把Remote调用Tv成员函数的接口定义放在Tv成员函数定义后面定义
//////////////////////////////////////
//
//tv.h
//
//////////////////////////////////////
// tvfm.h -- Tv and Remote classes using a friend member
#ifndef TVFM_H_
#define TVFM_H_
class Tv; // forward declaration
class Remote
{
public:
enum State{off, on};
enum {MinVal,MaxVal = 20};
enum {Antenna, Cable};
enum {TV, VCR};
private:
int mode;
public:
Remote(int m = TV) : mode(m) {}
bool volup(Tv & t); // prototype only
bool voldown(Tv & t);
void onoff(Tv & t) ;
void chanup(Tv & t) ;
void chandown(Tv & t) ;
void set_mode(Tv & t) ;
void set_input(Tv & t);
void set_chan(Tv & t, int c);
};
class Tv
{
public:
friend void Remote::set_chan(Tv & t, int c);
enum State{off, on};
enum {MinVal,MaxVal = 20};
enum {Antenna, Cable};
enum {TV, VCR};
Tv(int s = off, int mc = 100);
void onoff() {state = (state == on)? off : on;}
bool ison() const {return state == on;}
bool volup();
bool voldown();
void chanup();
void chandown();
void set_mode() {mode = (mode == Antenna)? Cable : Antenna;}
void set_input() {input = (input == TV)? VCR : TV;}
void settings() const;
private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
};
// Remote methods as inline functions
inline bool Remote::volup(Tv & t) { return t.volup();}
inline bool Remote::voldown(Tv & t) { return t.voldown();}
inline void Remote::onoff(Tv & t) { t.onoff(); }
inline void Remote::chanup(Tv & t) {t.chanup();}
inline void Remote::chandown(Tv & t) {t.chandown();}
inline void Remote::set_mode(Tv & t) {t.set_mode();}
inline void Remote::set_input(Tv & t) {t.set_input();}
inline void Remote::set_chan(Tv & t, int c) {t.channel = c;}
#endif
///////////////////////////////////////////
//
//tv.cpp
//
///////////////////////////////////////////
与上例同
/////////////////////////////////////////////
//
//main.cpp
//
/////////////////////////////////////////////
与上例同
互为友元类
class Tv
{
friend class Remote;
public:
void buzz (Remote & r);
…
};
class Remote
{
friend class Tv;
public:
bool volup (Tv & t) {t.volup();}
…
};
inline void Tv::buzz (Remote & r) //其中要用到Remote的方法所以必须定义在
{ //Remote后
……
}
共拥有友元函数
class cla1
{
friend void time();
……
};
class cla2
{
friend void time();
……
};
inline void time()
{
……
}


浙公网安备 33010602011771号