[源码下载]

不可或缺 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++: 友元函数, 友元类的更多相关文章

  1. 不可或缺 Windows Native (23) - C++: 虚函数

    [源码下载] 不可或缺 Windows Native (23) - C++: 虚函数 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 虚函数 示例1.基类CppHuman ...

  2. 不可或缺 Windows Native (6) - C 语言: 函数

    [源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h # ...

  3. 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)

    [源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...

  4. 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类

    [源码下载] 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 多重继承 虚基类 示例1 ...

  5. 不可或缺 Windows Native 系列文章索引

    [源码下载] 不可或缺 Windows Native 系列文章索引 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Window ...

  6. 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板

    [源码下载] 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板 作者:webabcd 介绍不可或缺 Windows Native 之 C++ ...

  7. 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

    [源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...

  8. 不可或缺 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 ...

  9. 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板

    [源码下载] 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板 作者:webabcd 介绍不可或缺 Window ...

随机推荐

  1. How to Change RabbitMQ Queue Parameters in Production?

    RabbitMQ does not allow re-declaring a queue with different values of parameters such as durability, ...

  2. 这里有个坑---entity为null的问题

    这里有个坑,最近加班赶个项目,忽然遇到个这个坑,先记录下来,纯当自己提高.---------每一个遇到的坑总结后都是一比财富. 我们在做项目是会使用ajax返回结果,在返回结果的时候一般选择json数 ...

  3. Java程序员的日常 —— 注册工厂的妙用

    注册工厂是一种很常用的框架书写方法,它适合于快速创建相同类型的对象. 举个栗子 比如一个家具工厂,有沙发.椅子.茶几等等,正常的编程模式是这样的: //创建 class 沙发{} class 椅子{} ...

  4. Lua: 好的, 坏的, 和坑爹的

                                   好的       小巧: 20000行C代码 可以编译进182K的可执行文件 (Linux下).       可移植: 只要是有ANSI ...

  5. Atitit.attilax重要案例 项目与解决方案与成果 v6 qa15

    Atitit.attilax重要案例 项目与解决方案与成果 v6 qa15 1. attilax重要案例的分类(atiuse,auBackTech,bizImp)3 1.1. 两个book3 1.2. ...

  6. DataGridView的Cell事件的先后触发顺序

    最近正在使用“DataGridView”对一个旧的Vs 2003开发的WINDOWS应用程序进行改造. 发现Vs 2003中的"DataGrid"中的一些事件已经在新的控件Data ...

  7. 类的继承和多态性-编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,

    编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 ...

  8. hibernate(六) cascade(级联)和inverse关系详解

    序言 写这篇文章之前,自己也查了很多的资料来搞清楚这两者的关系和各自所做的事情,但是百度一搜,大多数博文感觉说的云里雾里,可能博主自己清楚是怎么一回事,但是给一个不懂的人或者一知半解的人看的话,别人也 ...

  9. angularJS之事件处理

    angularJS的事件不像js一样,会默认有冒泡和捕获,还有angularJS之间的同名控制器之间,也只是同名, 不会让他们关联起来,就是这个名字的控制器的数据的改变不会影响到另一个同名的控制器. ...

  10. struts之动态方法调用使用通配符

    一.DMI动态方法调用的其中一种改变form表单中action属性的方式已经讲过了.还有两种,一种是改变struts.xml配置文件中action标签中的method属性,来指定执行不同的方法处理不同 ...