Last active
March 13, 2019 14:24
-
-
Save kateolenya/f37f047b0b2a50203b6cefeba6f3eb9a to your computer and use it in GitHub Desktop.
Bypassing encapsulation (legal way)
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> | |
using namespace std; | |
class Contact | |
{ | |
private: | |
int mobile_number; // private variable | |
int home_number; // private variable | |
public: | |
Contact() // constructor | |
{ | |
mobile_number = 12345678; | |
home_number = 87654321; | |
} | |
// Declaring a global 'friend' function | |
friend void print_numbers( Contact some_contact ); | |
}; | |
void print_numbers( Contact some_contact ) | |
{ | |
cout << "Mobile number: " << some_contact.mobile_number; | |
cout << ", home number: " << some_contact.home_number << endl; | |
} | |
int main() | |
{ | |
Contact Tony; | |
print_numbers(Tony); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment