友元练习——封装电视机的类

题目要求

编写电视机类,电视机有开机和关机状态,有音量,有频道,提供音量操作的方
法,频道操作的方法。由于电视机只能逐一调整频道,不能指定频道,增加遥控
类,遥控类除了拥有电视机已有的功能,再增加根据输入调台功能。

提示:遥控器可作为电视机类的友元类

封装电视机的类

TV.h

#ifndef TV_H
#define TV_H
#include "remote.h"

class TV
{
private:
    friend class Remote;
    enum{On,Off};
    enum{minVol,maxVol=100};
    enum{minChannel=1,maxChannel=255};
private:
    int mState;
    int mVolume;
    int mChannel;
public:
    TV();

    void onOrOff(void);//开关机
    void volUp(void);//音量加
    void volDown(void);//音量减

    void channelUp(void);//频道加
    void channelDowm(void);//频道减
    void showTV(void);
};
#endif // TV_H

tv.cpp

#include "tv.h"
#include <iostream>
using namespace std;

TV::TV()
{
    this->mState = Off;
    this->mVolume=minVol;
    this->mChannel=minChannel;
}
void TV::onOrOff(void)//开关机
{
    this->mState = (this->mState == On ? Off:On);
}
void TV::volUp(void)//音量加
{
    if(this->mVolume >= maxVol)
        return;
    this->mVolume++;
}
void TV::volDown(void)//音量减
{
    if(this->mVolume <=minVol)
        return;
    this->mVolume++;
}
void TV::channelUp(void)//频道加
{
    if(this->mChannel >= maxChannel)
        return;
    this->mChannel++;
}
void TV::channelDowm(void)//频道减
{
    if(this->mChannel <= minChannel)
        return;
    this->mChannel++;
}
void TV::showTV(void)
{
    cout << "电视状态:"<< (this->mState == On ?"开机":"关机")<<endl;
    cout << "音量:"<< this->mVolume<<endl;
    cout << "频道:"<< this->mChannel<<endl;

}

设置遥控器的类

remote.h

#ifndef REMOTE_H
#define REMOTE_H

#include "tv.h"
#include <iostream>
using namespace std;

class Remote
{
private:
    TV *pTv;
public:
    Remote(TV *pTv);
    void volUp(void);
    void volDown(void);
    void channelUp(void);
    void channelDown(void);
    void onOrOff(void);
    void showTV(void);
    
    void setChannel(int num);
};

#endif // REMOTE_H

remote.cpp

#include "remote.h"
#include "tv.h"

Remote::Remote(TV *pTv)
{
    this->pTv = pTv;
}
void Remote::volUp(void)
{
    this->pTv->volUp();
}
void Remote::volDown(void)
{
    this->pTv->volDown();
}
void Remote::channelUp(void)
{
    this->pTv->channelUp();
}
void Remote::channelDown(void)
{
    this->pTv->channelDowm();
}
void Remote::onOrOff(void)
{
    this->pTv->onOrOff();
}

void Remote::setChannel(int num)
{
    if(num >=TV::minChannel && num <=TV::maxChannel)
    {
        this->pTv->mChannel = num;
    }
    else
    {
        cout << "频道无效"<<endl;
    }
}
void Remote::showTV(void)
{
    this->pTv->showTV();
}

main.c

#include <iostream>
#include "remote.h"
#include "tv.h"

using namespace std;
void test1()
{
    TV tv;
    Remote remote(&tv);

    remote.onOrOff();

    remote.channelUp();
    remote.channelUp();
    remote.channelUp();
    remote.channelUp();
    remote.channelUp();

    remote.volUp();
    remote.volUp();
    remote.volUp();

    remote.showTV();

    remote.setChannel(256);
    remote.showTV();

    remote.setChannel(66);
    remote.showTV();
}

int main(int argc, char *argv[])
{
    test1();
    return 0;
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值