Skip to content

Instantly share code, notes, and snippets.

@kateolenya kateolenya/cpp_enc.cpp
Last active Mar 14, 2019

Embed
What would you like to do?
Basic encapsulation example
#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;
}
void print_numbers()
{
cout << "Mobile number: " << mobile_number;
cout << ", home number: " << home_number << endl;
}
};
int main()
{
Contact Tony;
Tony.print_numbers();
// cout << Tony.mobile_number << endl;
// will cause compile time error
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.