Indispensable Windows Native (20), windowsnative
[Download source code]
Indispensable Windows Native (20)-C ++: youyuan function, youyuan class
Author: webabcd
Introduction
Windows Native C ++ is indispensable
- Youyuan Function
- Youyuan class
Example
Demo of friend functions, friend class
CppClass4.h
#pragma once #include <string>using namespace std;namespace NativeDll{ class CppClass4 { public: string Demo(); };}
CppClass4.cpp
/** Friend (friend) function, friend class ** friend function: C ++ adds a declaration called "friend (friend)" function, it is used to assign "Privilege" to some functions in this class (it can be a global function or a member function of other classes ), enable access to the private and protected members of this class * youyuan class: if Class B is A youyuan class of Class A, all functions in Class B are the youyuan function of Class, that is, B can access all the private and protected members of A */# include "pch. h "# include" CppClass4.h "# include" cppHelper. h "using namespace NativeDll; void cppclass4_demo1 (); void Merge (); string CppClass4: Demo () {// global function is a friend function cppclass4_demo1 (); // membership functions of other classes as membership functions cppclass4_demo2 (); // youyuan class cppclass4_demo3 (); return "view code and comments";} class CppClass4Demo1Time {public: cppClass4Demo1Time (int h, int m, int s) {_ hour = h; _ minute = m; _ second = s;} // youyuan function declaration: declare the global function string ShowTime (CppClass4Demo1Time &); is my friend function friend string ShowTime (CppClass4Demo1Time &); // Add friendprivate: int _ hour; int _ minute; int _ second ;}; // previously declared. I am a friend function of the CppClass4Demo1Time class. I can use the private and protected attributes and function string ShowTime (CppClass4Demo1Time & t) in the CppClass4Demo1Time object) {t. _ hour = 10; return int2string (t. _ hour) + ":" + int2string (t. _ minute) + ":" + int2string (t. _ second);} // The demonstration void cppclass4_demo1 () {CppClass4Demo1Time t (8, 30, 15); string result = ShowTime (t ); // 10:30:15} class CppClass4Demo2Date; // declare class CppClass4Demo2Time {public: CppClass4Demo2Time (int h, int m, int s) {_ hour = h; _ minute = m; _ second = s;} // I used the CppClass4Demo2Date class here, but it is defined later. Therefore, we need to use "class CppClass4Demo2Date; "do advance reference declaration 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;} // you can declare the string ShowTime (CppClass4Demo2Date &) in the CppClass4Demo2Time class &); is my friend function friend string CppClass4Demo2Time: ShowTime (CppClass4Demo2Date &); private: int _ year; int _ month; int _ day ;}; // previously declared, I am a friend function of the CppClass4Demo2Date class. I can use the private and protected attributes and function string CppClass4Demo2Time: ShowTime (CppClass4Demo2Date & d) {d. _ year = 2016; return int2string (d. _ year) + "-" + int2string (d. _ month) + "-" + int2string (d. _ day) + "" + int2string (_ hour) + ":" + int2string (_ minute) + ":" + int2string (_ second );} // demo void cppclass4_demo2 () {CppClass4Demo2Time t (8, 30, 15); CppClass4Demo2Date d (2015, 4, 3 ); string result = t. showTime (d); // 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 {// youyuan class declaration: declare CppClass4Demo3Time class is my friend Meta class (member functions in CppClass4Demo3Time class can access my private and protected attributes and functions) friend CppClass4Demo3Time; public: CppClass4Demo3Date (int y, int m, int d) {_ year = y; _ month = m; _ day = d;} private: int _ year; int _ month; int _ day ;}; // consumer is a member function in CppClass4Demo3Date. // member functions in CppClass4Demo3Time class can access the private and protected attributes of consumer and function string CppClass4Demo3Time: ShowTime (CppClass4Demo3Date & d) {d. _ year = 2016; return int2string (d. _ year) + "-" + int2string (d. _ month) + "-" + int2string (d. _ day) + "" + int2string (_ hour) + ":" + int2string (_ minute) + ":" + int2string (_ second );} // demo of the youyuan class void cppclass4_demo3 () {// Note: // 1. If B is A youyuan class, then A is not necessarily B's youyuan class // 2. If B is A's youyuan class, C is B's youyuan class, then C is not necessarily A's friend class CppClass4Demo3Time t (8, 30, 15); CppClass4Demo3Date d (2015, 4, 3); string result = t. showTime (d); // 8:30:15}
OK
[Download source code]