C++初级问题,类和友元类

2024-12-12 01:56:39
推荐回答(2个)
回答1:

#include 
using namespace std;
enum _INPUTSRC{
    VGA, HDMI, AV
};
const char* label[] = {"VGA", "HDMI", "AV"};
class Remote;
class Television{
    _INPUTSRC m_inputsrc;
    int       m_lightintense;
public:
    Television(){
        m_inputsrc = VGA;
        m_lightintense = 1;
    }
    ~Television(){
    }
    void Print(void){
         cout << "输入源:" << label[m_inputsrc] << endl;
         cout << "亮    度:" << m_lightintense << endl;
         cout << "-------------------------------------" << endl;
    }
    friend class Remote;
};
class Remote{
public:
    void SetTvInputSrc(Television& tv, _INPUTSRC src){
        tv.m_inputsrc = src;
    }
    bool SetTvLightInstense(Television& tv, int intense){
        if (intense < 1 || intense > 10)
            return false;
        tv.m_lightintense = intense;
        return true;
    }
};
int main(void)
{
    Television tv;
    tv.Print();
    Remote rt;
    rt.SetTvInputSrc(tv, HDMI);
    rt.SetTvLightInstense(tv, 5);
    tv.Print();
    rt.SetTvInputSrc(tv, AV);
    rt.SetTvLightInstense(tv, 8);
    tv.Print();
    return 0;
}

回答2:

看过你提问的不少,不是没有人能解答你的问题。
只要你自己确实在努力,肯定会有人愿意帮你。