C++ Friend class
来源:互联网 发布:程序员培训机构哪个好 编辑:程序博客网 时间:2024/05/16 04:37
Here again, to learn the friend class in C++. And here is a demo for a better understanding.
The demo is mainly to summary the working days till defined date in a year, and using the friend class to realize it.
/*Author: Charles PanDate: 4/14/2014TODO: For the study of friend class in C++*/#include <iostream>using namespace std;class Date;class CustomDate{ private: int da,yr; public: CustomDate(int d = 0,int y =0) {da = d; yr = y;} void Display() const {std::cout<<std::endl<<yr<<da;} friend Date;};class Date{ private: int da,mo,yr; public: Date(int m,int d,int y) { da = d; mo = m; yr = y; } operator CustomDate();};Date::operator CustomDate(){ ///assume this is the working days of this year. static int dys[]= {23,12,21,22,14,23,12,21,22,14,11,18}; CustomDate cd(0,yr); ///summay the total work days till defined date of this year. ///here, in the class date, we access the private memeber "da" of class CustomDate. Coz friend, so we can. ///This is the kernal part of this code!!! for(int i=0;i<mo;i++) cd.da += dys[i]; cd.da+=da; return cd;}int main(){ Date dt(4,13,2014); ///convert date to customdate. CustomDate cd(dt); cd.Display(); char *tt; cin >>tt; return 0;}
More, if we don't want to pre-define the class date, we can do like the following, seems a more better way.
//class Date; //coment this lineclass CustomDate{ private: int da,yr; public: CustomDate(int d = 0,int y =0) {da = d; yr = y;} void Display() const {std::cout<<std::endl<<yr<<da;} friend class Date; //different with friend Date};
0 0
- [C++] friend class
- C++::Class Friend
- friend class
- friend class
- friend class
- [C++] C++中friend的使用(friend function and friend class)
- [C++]friend
- 实验: friend class
- C++ class friend
- 地基工friend class
- About "friend class"
- C++ Friend class
- friend class & function
- 7.3.4 friend class
- C++中的friend class
- friend class className
- c++中friend class
- C++ friend function and friend class
- 可重入函数
- Google Code Jam 2014-Qualification Round-Problem C. Minesweeper Master
- OpenStack Dashboard Load Balance
- autoit——制作自动安装软件脚本
- boost::asio学习之[五]运行udp tcp服务
- C++ Friend class
- OnEraseBkGnd()与OnPaint()的区别与联系,醍醐灌顶般的学习。。
- sql 联合查询
- 工具下载网址 (官网)
- 远程速度过慢解决问题
- 基于CodeIgniter的事件驱动扩展和开发规范
- FarPoint Spread和DataGridView中定义单元格的区别
- Qt中实现启动画面
- modelsim openrisc 开始片