[源碼下載]
作者: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 = 10;
return int2string(t._hour) + ":" + int2string(t._minute) + ":" + int2string(t._second);
}
// 全局函數作友元函數的演示
void cppclass4_demo1()
{
CppClass4Demo1Time t(8, 30, 15);
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 = 2016;
return int2string(d._year) + "-" + int2string(d._month) + "-" + int2string(d._day) + " " + int2string(_hour) + ":" + int2string(_minute) + ":" + int2string(_second);
}
// 其它類的成員函數作友元函數的演示
void cppclass4_demo2()
{
CppClass4Demo2Time t(8, 30, 15);
CppClass4Demo2Date d(2015, 4, 3);
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 = 2016;
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(8, 30, 15);
CppClass4Demo3Date d(2015, 4, 3);
string result = t.ShowTime(d); // 2016-4-3 8:30:15
}
OK
[源碼下載]