不可或缺 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 ...
随机推荐
- Java对象序列化---转载
1.概念 序列化:把Java对象转换为字节序列的过程. 反序列化:把字节序列恢复为Java对象的过程. 2.用途 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个 ...
- ASP.net的指令
3.2 ASP.NET指令 在基于面向对象思想的.NET平台,可以称之为“万物皆对象”了.在这里,一个页面,一个用户控件,一个母版页等,全都是对象,全都有各自的属性. 在类文件里,我们表示类的属性可以 ...
- react2 react 遍历数组
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
- cmd复制文件
cmd复制文件 复制文件夹,自动覆盖 xcopy /E/I/Y "D:\GitHub\Qriket\lucky\SPA\dist" "D:\GitHub\lucky\ww ...
- Liferay7 BPM门户开发之46: 集成Activiti用户、用户组、成员关系同步
在实际的BPM集成开发过程中,Liferay和Activiti这两个异构的系统之间,用户.组的同步需求非常重要,用来实现签收组的概念,比如指定签收组.会签.抢签都需要用到. Activiti可以通过自 ...
- iOS越狱开发(一)
做越狱开发也有一些时间了,有很多东西想总结一下,希望给他人一些借鉴,也是自己对过去开发经历的一些总结.个人不推荐使用盗版,这里主要以技术介绍为主. 这个系列里面主要介绍怎样进行越狱开发,涉及到以下几个 ...
- sys.dm_db_wait_stats
sys.dm_db_wait_stats 返回在操作期间执行的线程所遇到的所有等待的相关信息. 可以使用此聚合视图来诊断 Azure SQL Database 以及特定查询和批处理的性能问题. 执行查 ...
- JSP网站开发基础总结《十》
经过上一篇的介绍相信大家对JSP提供的过滤器一定有了一个概念,本篇我们就一起再来研究一下关于创建多个过滤器时,如果有两个以上过滤器的过滤规则相同,那么这些过滤器的执行顺序如何呢?答案是根据我们在web ...
- 云计算之路-阿里云上:借助IIS Log Parser Studio分析“黑色30秒”问题
今天下午15:11-15:13间出现了类似“黑色30秒”的状况,我们用强大的IIS日志分析工具——Log Parser Studio进行了进一步的分析. 分析情况如下—— 先看一下Windows性能监 ...
- PHP Log时时查看小工具
以前Log都是打印在一个文档中,然后打开文件夹,最后打开文档查看里面的内容,每次打开文件夹感觉很烦. 前些日子看到同事开发.NET的时候,用他自己的一个小工具能够时时查看到Log的内容,非常方便,所以 ...