不可或缺 Windows Native (20) - C++: 友元函数, 友元类
作者:webabcd
介绍
不可或缺 Windows Native 之 C++
- 友元函数
- 友元类
示例
演示友元函数, 友元类
CppClass4.h
#pragma once #include <string> using namespace std; namespace NativeDll
{
class CppClass4
{
public:
string Demo();
};
}
CppClass4.cpp
/*
* 友元(friend)函数, 友元类
*
* 友元函数: C++ 增加了一种称之为“友元(friend)”函数的声明,其用于将“特权”在本类中赋给一些函数(可以是全局函数,也可以是其它类的成员函数),使之能够访问本类的私有和保护成员
* 友元类: 如果 B 类是 A 类的友元类,则 B 中的所有函数都是 A 的友元函数,即 B 可以访问 A 的所有私有和保护成员
*/ #include "pch.h"
#include "CppClass4.h"
#include "cppHelper.h" using namespace NativeDll; void cppclass4_demo1();
void cppclass4_demo2();
void cppclass4_demo3(); string CppClass4::Demo()
{
// 全局函数作友元函数
cppclass4_demo1(); // 其它类的成员函数作友元函数
cppclass4_demo2(); // 友元类
cppclass4_demo3(); return "看代码及注释吧";
} class CppClass4Demo1Time
{
public:
CppClass4Demo1Time(int h, int m, int s)
{
_hour = h;
_minute = m;
_second = s;
} // 友元函数声明:声明全局函数 string ShowTime(CppClass4Demo1Time &); 是我的友元函数
friend string ShowTime(CppClass4Demo1Time &); // 前面加 friend private:
int _hour;
int _minute;
int _second;
}; // 之前声明过的,我是 CppClass4Demo1Time 类的友元函数,我可以使用 CppClass4Demo1Time 对象中的 private 和 protected 属性和函数
string ShowTime(CppClass4Demo1Time &t)
{
t._hour = ;
return int2string(t._hour) + ":" + int2string(t._minute) + ":" + int2string(t._second);
} // 全局函数作友元函数的演示
void cppclass4_demo1()
{
CppClass4Demo1Time t(, , );
string result = ShowTime(t); // 10:30:15
} class CppClass4Demo2Date; // 对 CppClass4Demo2Date 类的提前引用声明
class CppClass4Demo2Time
{
public:
CppClass4Demo2Time(int h, int m, int s)
{
_hour = h;
_minute = m;
_second = s;
} // 我这里用到了 CppClass4Demo2Date 类,但是其是在后面定义的,所以在此之前要通过“class CppClass4Demo2Date;”做提前引用声明
string ShowTime(CppClass4Demo2Date &d); private:
int _hour;
int _minute;
int _second;
}; class CppClass4Demo2Date
{
public:
CppClass4Demo2Date(int y, int m, int d)
{
_year = y;
_month = m;
_day = d;
} // 友元函数声明:声明 CppClass4Demo2Time 类中的 string ShowTime(CppClass4Demo2Date &); 是我的友元函数
friend string CppClass4Demo2Time::ShowTime(CppClass4Demo2Date &); private:
int _year;
int _month;
int _day; }; // 之前声明过的,我是 CppClass4Demo2Date 类的友元函数,我可以使用 CppClass4Demo2Date 对象中的 private 和 protected 属性和函数
string CppClass4Demo2Time::ShowTime(CppClass4Demo2Date &d)
{
d._year = ;
return int2string(d._year) + "-" + int2string(d._month) + "-" + int2string(d._day) + " " + int2string(_hour) + ":" + int2string(_minute) + ":" + int2string(_second);
} // 其它类的成员函数作友元函数的演示
void cppclass4_demo2()
{
CppClass4Demo2Time t(, , );
CppClass4Demo2Date d(, , );
string result = t.ShowTime(d); // 2016-4-3 8:30:15
} class CppClass4Demo3Date;
class CppClass4Demo3Time
{
public:
CppClass4Demo3Time(int h, int m, int s)
{
_hour = h;
_minute = m;
_second = s;
} string ShowTime(CppClass4Demo3Date &d); private:
int _hour;
int _minute;
int _second;
}; class CppClass4Demo3Date
{
// 友元类声明:声明 CppClass4Demo3Time 类是我的友元类(CppClass4Demo3Time 类中的成员函数可以访问我的 private 和 protected 属性和函数)
friend CppClass4Demo3Time; public:
CppClass4Demo3Date(int y, int m, int d)
{
_year = y;
_month = m;
_day = d;
} private:
int _year;
int _month;
int _day;
}; // CppClass4Demo3Time 是 CppClass4Demo3Date 的友元类
// CppClass4Demo3Time 类中的成员函数可以访问 CppClass4Demo3Date 的 private 和 protected 属性和函数
string CppClass4Demo3Time::ShowTime(CppClass4Demo3Date &d)
{
d._year = ;
return int2string(d._year) + "-" + int2string(d._month) + "-" + int2string(d._day) + " " + int2string(_hour) + ":" + int2string(_minute) + ":" + int2string(_second);
} // 友元类的演示
void cppclass4_demo3()
{
// 注:
// 1、如果 B 是 A 的友元类,那么 A 不一定是 B 的友元类
// 2、如果 B 是 A 的友元类,C 是 B 的友元类,那么 C 不一定是 A 的友元类 CppClass4Demo3Time t(, , );
CppClass4Demo3Date d(, , );
string result = t.ShowTime(d); // 2016-4-3 8:30:15
}
OK
[源码下载]
不可或缺 Windows Native (20) - C++: 友元函数, 友元类的更多相关文章
- 不可或缺 Windows Native (23) - C++: 虚函数
[源码下载] 不可或缺 Windows Native (23) - C++: 虚函数 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 虚函数 示例1.基类CppHuman ...
- 不可或缺 Windows Native (6) - C 语言: 函数
[源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h # ...
- 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...
- 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类
[源码下载] 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 多重继承 虚基类 示例1 ...
- 不可或缺 Windows Native 系列文章索引
[源码下载] 不可或缺 Windows Native 系列文章索引 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Window ...
- 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板
[源码下载] 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板 作者:webabcd 介绍不可或缺 Windows Native 之 C++ ...
- 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换
[源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...
- 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native
[源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:web ...
- 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板
[源码下载] 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板 作者:webabcd 介绍不可或缺 Window ...
随机推荐
- Setting up SSL for SCM-Manager with Microsoft CA and TortoiseHg
You can configure SSL for SCM-Manager so that the communication of your repositories are encrypted. ...
- Git Day01,仓库,commit,版本切换
1st,创建版本库: 2nd,添加文件: 3rd,修改文件,并提交: 4th,版本切换:git log查看版本:版本回退: 又回到原始版本了: 回到“未来”: 今天就到这里,明天继续.Git确实挺 ...
- Bootstrap~日期控制
回到目录 一个成熟的框架,日期控制是少不了的,在网上也有很多日期控制可以选择,而主框架用了bootstrap,日期控制也当前要用它自己的, 控件地址:http://www.bootcss.com/p/ ...
- struts2学习笔记之五:表单数据收集的几种方式
方法一:struts2对ModelDriven模式的支持(模型驱动模式) Struts2可以采用类似于Struts1中的ActionForm方式收集数据,这样方式叫ModelDriven模式 Acti ...
- JavaScript的陷阱
这本来是翻译Estelle Weyl的<15 JavaScript Gotchas>,里面介绍的都是在JavaScript编程实践中平时容易出错或需要注意的地方,并提供避开这些陷阱的方法, ...
- JAVA实现Excel的读写--poi
上一篇为大家介绍了通过xls.jar的方式生成Excel的方法,本篇就为大家再介绍一下通过poi方式实现Excel文件的读写操作,内容很简单,代码注释很清晰. 1.生成Excel文件: import ...
- 哈夫曼树(二)之 C++详解
上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...
- 必应词典3.2去广告备忘笔记(转摘于roustar31)
下载安装包后,沙盘运行,得到本体BingDict_Setup.msi文件,起作用的就是这个,其他的无视了.使用命令行参数:msiexec /a "d:\BingDict_Setup.msi& ...
- 整理的一些PHP面试题目
1.strlen()和mb_strlen()的作用分别是什么? strlen()和mb_strlen()的作用都是来获取字符串的长度,其中strlen()只针对单字节编码字符,也就是计算字符串的总字节 ...
- 推荐10个 CSS3 制作的创意下拉菜单效果
下拉菜单是一个很常见的效果,在网站设计中被广泛使用.通过使用下拉菜单,设计者不仅可以在网站设计中营造出色的视觉吸引力,但也可以为网站提供了一个有效的导航方案.使用 HTML5 和 CSS3 可以更容易 ...