友元函数例子代码功能用来显示时间

display函数放在time类中作为date类的友元函数

#include<iostream>
using namespace std;
class date;//类的提前声明
class time
{public:time(int,int,int);void display(date &);private:int hour;int minute;int sec;
} ;
class date
{public:date(int,int,int) ;friend void time::display(date&);private:int month;int day;int year;
};
time::time(int h,int m,int s)//定义类time的成员函数
{hour=h;minute=m;sec=s;
}
void time::display(date&d)
{cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
date::date(int m,int d,int y)
{month=m;day=d;year=y;
}
int main()
{time t1(10,13,56);date d1(12,25,2004);t1.display(d1);return 0;
}

display函数不放在time类中,作为类外的普通函数,然后在time和date类中将display声明为友元函数。在主函数中调用display函数

#include<iostream>
using namespace std;
class date;//类的提前声明
class time
{public:time(int,int,int);friend void display(const date&,const time&);private:int hour;int minute;int sec;
} ;
class date
{public:date(int,int,int) ;friend void display(const date&,const time&);private:int month;int day;int year;
};
time::time(int h,int m,int s)//定义类time的成员函数
{hour=h;minute=m;sec=s;
}
void display(const date&d,const time&t)
{cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
date::date(int m,int d,int y)
{month=m;day=d;year=y;
}
int main()
{time t1(10,13,56);date d1(12,25,2004);display(d1,t1);return 0;
}

将time类声明为date类的友元类,通过time类中的display函数引用date类对象的私有数据,输出年月日和时分秒

#include<iostream>
using namespace std;
class time;
class date;
{public:date(int,int,int);friend time;private:int month;int day;int year;};date::date(int m,int d,int y):month(m),day(d),day(y){}class time{public:time(int,int,int);void display(const date&);private:int hour;int minute;int sec;};time::time(int h,int m,int s):hour(h),minute(m),sec(s){}void time::display(const date&d){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<hour<<":"<<minute<<":"<<sec<<endl;}int main(){time t1(10,13,56);date d1(12,25,2004);t1.display(d1);return 0;}

c++学习笔记之友元函数相关推荐

  1. c++学习笔记内联函数,函数重载,默认参数

    c++学习笔记内联函数,函数重载,默认参数 1 inline内联函数 C++中的const常量可以替代宏常数定义,如: const int A = 3;  #define A 3 C++中是否有解决 ...

  2. c语言中void arrout,c语言学习笔记(数组、函数

    <c语言学习笔记(数组.函数>由会员分享,可在线阅读,更多相关<c语言学习笔记(数组.函数(53页珍藏版)>请在人人文库网上搜索. 1.数组2010-3-29 22:40一维数 ...

  3. IOS学习笔记07---C语言函数-scanf函数

    2013/8/7 IOS学习笔记07---C语言函数-scanf函数 ------------------------------ qq交流群:创梦技术交流群:251572072            ...

  4. IOS学习笔记07---C语言函数-printf函数

    IOS学习笔记07---C语言函数-printf函数 0 7.C语言5-printf函数 ------------------------- ----------------------------- ...

  5. IOS学习笔记06---C语言函数

    IOS学习笔记06---C语言函数 --------------------------------------------  qq交流群:创梦技术交流群:251572072              ...

  6. jquery学习笔记及常用函数封装

    二.JQuery 学习笔记及常用函数封装 https://download.csdn.net/download/weixin_42530002/13087988 1.JQuery入门 (1).css选 ...

  7. STATA学习笔记:egen函数

    STATA学习笔记:egen函数 1. egen (1)mean() webuse egenxmpl,clear egen avg = mean(cholesterol) gen deviation ...

  8. Python学习笔记11:函数修饰符

    Python学习笔记11:函数修饰符 Python有很多有趣的特性,其中函数修饰符就是一个. 我们在之前的那个web应用示例中用过如下写法: @web.route('/log') @符号后边的,就是一 ...

  9. Hive学习笔记三之函数操作

    文章目录 5 函数 5.1 系统内置函数 5.2 常用内置函数 5.2.1 空字段赋值 5.2.2 CASE WHEN THEN ELSE END(类似于java中的switch case) 5.2. ...

最新文章

  1. 新工具:表单/Cookie 验证网站爬网设置工具
  2. Java中的单例模式
  3. python3.x : 安装opencv
  4. 阿里3篇技术论文入选国际顶级会议FAST2020,全球第一!
  5. python常见排序算法解析
  6. 0 null 的区别
  7. 打开网页出现运行脚本错误的解决技巧
  8. 进程同步与互斥:POSIX有名信号量
  9. 微信服务通知消息找回_微信鲜为人知的4个冷知识,小技巧却有大用处
  10. 联想服务器改xp系统,联想win7改xp蓝屏怎么解决
  11. 第三方支付相关知识结构
  12. 最新kali之bulk_extractor
  13. react前端显示图片_react.js - 关于react引用本地静态资源图片的问题
  14. Python:蒙特卡罗方法模拟解决三门问题
  15. 基于ZXing实现个性彩色好看的二维码
  16. Docker:架构分解
  17. 车载网络测试 - 车载以太网 - 网络配置方法
  18. “(CRON) info (No MTA installed, discarding output)”
  19. linux6.5取消屏保,怎么把wps屏保关掉
  20. 解决IDEA 前端返回值乱码问题

热门文章

  1. 如果我要...(研究版)
  2. 在SunOS5.8/solaris7上使用Xerces-C解析器
  3. rejection from Cambridge Machine Learning and Machine Intelligence MPhil
  4. 室内使用酒精消毒的时候一定要注意开窗!!!
  5. 校外导师互选 页面设计
  6. formal method lecture 2: propositional logic
  7. 大数相乘(大数阶乘模板)
  8. 阿里云服务器Svn-Server无法连接,阿里云服务器SVNServer配置
  9. KMP算法的Next数组详解(转)
  10. Linux内核编译与管理