Last active
May 4, 2019 18:50
-
-
Save leimao/8d56017740defa9fc13b61b90b106726 to your computer and use it in GitHub Desktop.
Friend Class and Function Usages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
/* | |
* Modified from | |
* https://www.geeksforgeeks.org/friend-class-function-cpp/ | |
*/ | |
class A; | |
class B; | |
class A | |
{ | |
public: | |
A():a(0) {}; | |
void showB(B& x); | |
// Friend class | |
// Grant class B access to the private members of class A | |
friend class B; | |
private: | |
int a; | |
}; | |
class B | |
{ | |
public: | |
B():b(0) {}; | |
void showA(A& x); | |
// Friend function | |
// Grant A::showB1 function to the private members of class A | |
friend void A::showB(B& x); | |
friend void globalShowB(B& x); | |
private: | |
int b; | |
}; | |
void A::showB(B& x) | |
{ | |
std::cout << "B::b=" << x.b << std::endl; | |
} | |
void B::showA(A& x) | |
{ | |
std::cout << "A::a=" << x.a << std::endl; | |
} | |
void globalShowB(B& x) | |
{ | |
std::cout << "B::b=" << x.b << std::endl; | |
} | |
int main() | |
{ | |
A a; | |
B b; | |
b.showA(a); | |
a.showB(b); | |
globalShowB(b); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment