博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
课程设计__友元
阅读量:7038 次
发布时间:2019-06-28

本文共 1060 字,大约阅读时间需要 3 分钟。

///友元///友元有利于数据共享,但是破坏了类的封装性#include 
using namespace std;class Time {public: Time (int h=10,int m=10,int s=10):hour(h),minute(m),sec(s) {} ///将普通函数放到类中,普通函数可以访问私有成员 friend void display(Time &t);private: int hour; int minute; int sec;};void display(Time &t) { cout<
<<" "<
<<" "<
<
View Code

 

///友元///友元有利于数据共享,但是破坏了类的封装性#include 
using namespace std;class Date; ///对Date类提前声明class Time {public: Time (int h,int m,int s):hour(h),minute(m),sec(s) {}; void display(Date &d);private: int hour; int minute; int sec;};class Date {public: Date(int m,int d,int y):month(m),day(d),year(y) {} ///声明Time中display函数为本类的友元成员函数,可以访问本类的私有成员 friend void Time::display(Date &d);private: int month; int day; int year;};void Time::display(Date &d) { cout<
<<" "<
<<" "<
<
View Code

 

转载于:https://www.cnblogs.com/TreeDream/p/5449581.html

你可能感兴趣的文章
手动脱NsPacK壳实战
查看>>
【cocos2d-x 3.x 学习笔记】对象内存管理
查看>>
T-SQL编程中的异常处理-异常捕获(try catch)与抛出异常(throw)
查看>>
微信小程序,前端大梦想(六)
查看>>
tomcat设置编码格式utf8
查看>>
sql中一列拆成两列
查看>>
让人恼火的经历——手机H5网页被注入广告
查看>>
LInq之Take Skip TakeWhile SkipWhile Reverse Union Concat 用法
查看>>
spring装配集合
查看>>
mysql高可用架构方案之中的一个(keepalived+主主双活)
查看>>
SpringMVC案例2----基于spring2.5的注解实现
查看>>
SpringBoot编写自定义的starter 专题
查看>>
北京服务业占GDP比重达81.7%
查看>>
2016年深圳市服务业占GDP比重首次突破六成
查看>>
Oracle基础(四)pl/sql
查看>>
Woody的Python学习笔记2
查看>>
虚函数相关问题分析
查看>>
js代码从页面移植到文件里失效或js代码改动后不起作用的解决的方法
查看>>
java点滴之volatilekeyword特性
查看>>
python 回溯法 子集树模板 系列 —— 13、最佳作业调度问题
查看>>