#ifndef TV_H_
#define TV_H_
/*一个类 电视 */
class Tv
{
public:
friend class Remote; //Remote类可以访问Tv Privite 的私有部分
enum {
off,on //开关
};
enum
{
MinVal,MaxVal=20 //音量
};
enum {
Antena,Cable //使用的天线、还是电缆
};
enum
{
TV ,DVD //工作模式
};
Tv(int s = off, int mc = 125) :state(s), volume(5), maxchannel(mc),
channel(5), mode(Cable), input(TV) {}
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 == Antena) ? Cable : Antena; }
void set_input() { input = (input == TV) ? DVD : TV; }
void settings()const; //显示所有设置
private:
int state; // 开或者 关
int volume; // 音量
int maxchannel; //最大
int channel; //当前频道
int mode; // 广播还是 电缆
int input; //Tv 或者 DVD
};
/*Remote 的定义 (遥控器) */
class Remote {
private :
int mode; // 控制 TV 或 DVD
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) { return t.onoff(); }
void chanup(Tv & t) { return t.chanup(); }
void chandown(Tv & t) { return t.chandown(); }
void set_chan(Tv &t, int c) { t.channel = c; } //访问了Tv的私有成员
void set_mode(Tv &t) { t.set_mode(); }
void set_input(Tv &t) { t.set_input(); }
};
#endif // TV_H_
#include "stdafx.h"
#include "Tv.h"
#include <iostream>
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 == Antena ? "antenna" : "cable") << endl;
cout << "Input = " << (input == TV ? "TV" : "DVD") << endl;
}
}
#include "stdafx.h"
#include "tv.h"
#include <iostream>
int main()
{
using std::cout;
Tv s42;
cout << "Initial settings for 42 \" Tv: \n";
s42.settings();
s42.onoff();
s42.chanup();
cout << " \n Adjusted settings for 42 \" Tv: \n";
s42.chanup();
cout << "\n Adjusted settings for 42 \" Tv: \n";
s42.settings();
Remote grey;
grey.set_chan(s42, 10);
grey.volup(s42);
grey.volup(s42);
cout << " \n s42 \" settings after using remote: \n";
s42.settings();
Tv s58(Tv::on);
s58.set_mode();
grey.set_chan(s58, 58);
cout << " \n s58 \" setting: \n";
s58.settings();
system("pause");
return 0;
}
Initial settings for 42 " Tv: TV is off Adjusted settings for 42 " Tv: Adjusted settings for 42 " Tv: TV is on Volume setting =5 Channel setting = 7 Mode = cable Input = TV s42 " settings after using remote: TV is on Volume setting =7 Channel setting = 10 Mode = cable Input = TV s58 " setting: TV is on Volume setting =5 Channel setting = 58 Mode = antenna Input = TV 请按任意键继续. . .
clas Tv
{
friend void Remote::set_chan(Tv & t,int c ) ;
}
class Tv ; //前向声明
class Remote{...}
class Tv {...}
class Tv; //前向声明
class Remote {...} //如要用到Tv 只能是方法声明
class Tv{...}
//接着写Remote的定义
#ifndef TV_H_
#define TV_H_
class Tv; //前向声明
class Remote {
public:
enum {
off, on //开关
};
enum
{
MinVal, MaxVal = 20 //音量
};
enum {
Antena, Cable //使用的天线、还是电缆
};
enum
{
TV, DVD //工作模式
};
private:
int mode; // 控制 TV 或 DVD
public:
Remote(int m = TV) :mode(m) {}
//用到了Tv 只能是声明
bool volup(Tv & t);
bool voldown(Tv & t);
void onoff(Tv & t);
void chanup(Tv & t);
void chandown(Tv & t);
void set_chan(Tv &t, int c);
void set_mode(Tv &t);
void set_input(Tv &t);
};
class Tv
{
public:
friend void Remote::set_chan(Tv & t,int c); //友元成员函数
enum {
off, on //开关
};
enum
{
MinVal, MaxVal = 20 //音量
};
enum {
Antena, Cable //使用的天线、还是电缆
};
enum
{
TV, DVD //工作模式
};
Tv(int s = off, int mc = 125) :state(s), volume(5), maxchannel(mc),
channel(5), mode(Cable), input(TV) {}
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 == Antena) ? Cable : Antena; }
void set_input() { input = (input == TV) ? DVD : TV; }
void settings()const; //显示所有设置
private:
int state; // 开或者 关
int volume; // 音量
int maxchannel; //最大
int channel; //当前频道
int mode; // 广播还是 电缆
int input; //Tv 或者 DVD
};
inline bool Remote::volup(Tv & t) { return t.volup(); }
inline bool Remote::voldown(Tv & t) { return t.voldown(); }
inline void Remote::onoff(Tv & t) { return t.onoff(); }
inline void Remote::chanup(Tv & t) { return t.chanup(); }
inline void Remote::chandown(Tv & t) { return t.chandown(); }
inline void Remote::set_chan(Tv &t, int c) { t.channel = c; }
inline void Remote::set_mode(Tv &t) { return t.set_mode(); }
inline void Remote::set_input(Tv &t) { return t.set_input(); }
#endif // TV_H_
class Remote
class Tv
{
friend clas Remote
public:
void buzz(Remote & r) ;
...
}
class Remote
{
friend class Tv;
public:
void Bool volup(Tv & t){t.volup();}
...
}
inline void Tv::buzz(Remote & r)
{
...
}
class Analyzer;
class Probe
{
friend void sync (Analyzer & a,const Probe & p) ;
friend void sync (Probe & p,const Analyzer & a);
...
};
class Analyzer
{
friend void sync (Analyzer & a,const Probe & p) ;
friend void sync (Probe & p,const Analyzer & a);
}
inline void sync (Analyzer & a,const Probe & p)
{
...
}
inline void sync (Probe & p,const Analyzer & a)
{
...
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有