C++-友元类

C++-友元类友元类 nbsp Afriendclass canaccessthe Asignificant repr

友元类

 

A friend class in C++ can access the private and protected members of the class in which it is declared as a friend. A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure. The friend class mechanism allows to extend the storage and access to the parts, while retaining proper encapsulation as seen by the users of the data structure.

C++中,友元类可以访问将其定义为友元的类中的私有和保护成员。 友元类的一个重要用途是用于数据结构的一部分(由类表示),提供对表示该数据结构的主类的访问途径。  友元类机制允许扩展存储和访问部件,同时保留数据结构的用户所看到的适当封装。

代码示例:

//tv.h -- TV和Remote类 #ifndef TV_H_ #define TV_H_ class Tv { public: friend class Remote; //Remote可以访问Tv的私有部分 enum {Off, On}; enum {MinVal, MaxVal=20}; enum {Antenna, Cable}; enum {TV,DVD}; Tv(int s = Off, int mc = 125) :state(s), volume(5), maxchannel(mc), channel(2), 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 == Antenna) ? Cable : Antenna; } void set_input() { input = (input == TV) ? DVD : TV; } void settings() const; //显示所有的设置 private: int state; //on或者off int volume; int maxchannel; int channel; int mode; int input; }; 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) { 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_H_ 
//tv.cpp -- Tv类方法(遥控器类方法为内联) #include 
  
    #include"tv.h" 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" : "DVD") << endl; } } 
  
//use_tv.cpp -- 使用Tv和遥控器类 #include 
  
    #include"tv.h" int main() { using std::cout; Tv s42; cout << "Initial settings for 42\" TV:\n"; s42.settings(); s42.onoff(); s42.chanup(); cout << "\nAdjusted settings for 42\" TV:\n"; s42.settings(); s42.chanup(); cout << "\nAdjusted settings for 42\" TV:\n"; s42.settings(); Remote grey; grey.set_chan(s42, 10); grey.volup(s42); grey.volup(s42); cout << "\n42\" settings after using remote:\n"; s42.settings(); Tv s58(Tv::On); s58.set_mode(); grey.set_chan(s58, 28); cout << "\n58\" settings:\n"; s58.settings(); std::cin.get(); return 0; } 
  

C++-友元类

程序说明:

Remote类是Tv类的友元类。 通过将Tv类对象作为参数传入Remote类方法来改变Tv类成员变量的值。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216055.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午12:40
下一篇 2026年3月18日 下午12:40


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号