Skip to content

Instantly share code, notes, and snippets.

View kateolenya's full-sized avatar

Kateryna kateolenya

View GitHub Profile
@kateolenya
kateolenya / python_enc.py
Last active March 4, 2019 13:30
Testing encapsulation in Python3
#!/usr/bin/python3
class Phone:
username = "Kate" # public variable
__serial_number = "11.22.33" # private variable
__how_many_times_turned_on = 0 # private variable
def __init__(self): # constructor
print( "The Phone object was created" )
self.__turn_on()
@kateolenya
kateolenya / python_basic_class.py
Last active March 4, 2019 13:30
Basic class in Python3
#!/usr/bin/python3
class Phone:
number = "111-11-11"
def print_number(self):
print( "Phone number is: ", self.number )
my_phone = Phone()
my_phone.print_number()
@kateolenya
kateolenya / python_basic_enc.py
Last active March 5, 2019 10:40
Basic encapsulation in Python3
#!/usr/bin/python3
class Phone:
username = "Kate" # public variable
__how_many_times_turned_on = 0 # private variable
def call(self): # public method
print( "Ring-ring!" )
def __turn_on(self): # private method
@kateolenya
kateolenya / python_break_basic_enc.py
Last active March 4, 2019 13:31
Breaking basic encapsulation in Python 3
#!/usr/bin/python3
class Phone:
username = "Kate" # public variable
__serial_number = "11.22.33" # private variable
__how_many_times_turned_on = 0 # private variable
def call(self): # public method
print( "Ring-ring!" )
@kateolenya
kateolenya / python_magic_enc.py
Last active March 5, 2019 12:51
Encapsulation with magic methods in Python 3
#!/usr/bin/python3
class Phone:
def __init__(self, number): # magic method / inititalizer
print( "The Phone object was created" )
self.number = number
def __lt__(self, other): # magic method / rich comparison
return self.number < other.number
@kateolenya
kateolenya / cpp_friend_enc.cpp
Last active March 13, 2019 14:24
Bypassing encapsulation (legal way)
#include <iostream>
using namespace std;
class Contact
{
private:
int mobile_number; // private variable
int home_number; // private variable
public:
Contact() // constructor
@kateolenya
kateolenya / cpp_pointer_enc.cpp
Last active March 13, 2019 14:28
Bypassing encapsulation with pointers
#include <iostream>
using namespace std;
class Contact
{
private:
int mobile_number; // private variable
int home_number; // private variable
public:
Contact() // constructor
@kateolenya
kateolenya / cpp_enc.cpp
Last active March 14, 2019 10:13
Basic encapsulation example
#include <iostream>
using namespace std;
class Contact
{
private:
int mobile_number; // private variable
int home_number; // private variable
public:
Contact() // constructor
@kateolenya
kateolenya / cpp_break_struct.cpp
Last active March 13, 2019 14:33
Bypassing encapsulation with typecast to struct
#include <iostream>
using namespace std;
class Contact
{
private:
int mobile_number; // private variable
int home_number; // private variable
public:
Contact() // constructor
@kateolenya
kateolenya / main.c
Last active March 14, 2019 10:51
Private variables in struct in C
#include "private_var.h"
#include <stdio.h>
int main()
{
struct Contact * Tony;
Tony = create_contact();
// printf( "Mobile number: %d\n", Tony->mobile_number);
// will cause compile time error
delete_contact( Tony );