Skip to content

Instantly share code, notes, and snippets.

Kateryna kateolenya

View GitHub Profile
@kateolenya
kateolenya / python_enc.py
Last active Mar 4, 2019
Testing encapsulation in Python3
View python_enc.py
#!/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 Mar 4, 2019
Basic class in Python3
View python_basic_class.py
#!/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_break_basic_enc.py
Last active Mar 4, 2019
Breaking basic encapsulation in Python 3
View python_break_basic_enc.py
#!/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_basic_enc.py
Last active Mar 5, 2019
Basic encapsulation in Python3
View python_basic_enc.py
#!/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_magic_enc.py
Last active Mar 5, 2019
Encapsulation with magic methods in Python 3
View python_magic_enc.py
#!/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 Mar 13, 2019
Bypassing encapsulation (legal way)
View cpp_friend_enc.cpp
#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 Mar 13, 2019
Bypassing encapsulation with pointers
View cpp_pointer_enc.cpp
#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 Mar 13, 2019
Bypassing encapsulation with typecast to struct
View cpp_break_struct.cpp
#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 Mar 14, 2019
Basic encapsulation example
View cpp_enc.cpp
#include <iostream>
using namespace std;
class Contact
{
private:
int mobile_number; // private variable
int home_number; // private variable
public:
Contact() // constructor
@kateolenya
kateolenya / private_var.c
Last active Mar 14, 2019
Private variables in struct in C
View private_var.c
#include "private_var.h"
#include <stdio.h>
#include <stdlib.h>
struct Contact
{
int mobile_number;
int home_number;
};
You can’t perform that action at this time.