Skip to content

Instantly share code, notes, and snippets.

Avatar

Kateryna kateolenya

View GitHub Profile
@kateolenya
kateolenya / private_funct.c
Created Mar 14, 2019
Private function in C using static
View private_funct.c
#include "private_funct.h"
#include <stdio.h>
#include <stdlib.h>
struct Contact
{
int mobile_number;
int home_number;
};
@kateolenya
kateolenya / main.c
Created Mar 14, 2019
Private function in C using static
View main.c
#include "private_funct.h"
int main()
{
struct Contact * Tony;
Tony = create_contact();
// print_numbers( Tony );
// will cause compile time error
delete_contact( Tony );
return 0;
@kateolenya
kateolenya / main.c
Last active Mar 14, 2019
Bypassing encapsulation with pointers in C
View main.c
#include "private_var.h"
#include <stdio.h>
int main()
{
struct Contact * Tony;
Tony = create_contact();
int * mobile_number_is_here = (int *)Tony;
printf("Mobile number: %d\n", *mobile_number_is_here);
@kateolenya
kateolenya / private_var.h
Last active Mar 20, 2019
Private variables in struct in C
View private_var.h
#ifndef PRIVATE_VAR
#define PRIVATE_VAR
struct Contact;
struct Contact * create_contact();
void delete_contact( struct Contact * some_contact );
#endif /* PRIVATE_VAR */
@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;
};
@kateolenya
kateolenya / main.c
Last active Mar 14, 2019
Private variables in struct in C
View main.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 );
@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 / 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_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