Skip to content

Instantly share code, notes, and snippets.

View kateolenya's full-sized avatar

Kateryna kateolenya

View GitHub Profile
@kateolenya
kateolenya / private_funct.h
Created March 14, 2019 22:04
Private function in C using static
#ifndef PRIVATE_FUNCT_H
#define PRIVATE_FUNCT_H
struct Contact;
struct Contact * create_contact();
static void print_numbers( struct Contact * some_contact );
void delete_contact( struct Contact * my_points );
#endif /* PRIVATE_FUNCT_H */
@kateolenya
kateolenya / private_funct.c
Created March 14, 2019 22:03
Private function in C using static
#include "private_funct.h"
#include <stdio.h>
#include <stdlib.h>
struct Contact
{
int mobile_number;
int home_number;
};
@kateolenya
kateolenya / main.c
Created March 14, 2019 22:02
Private function in C using static
#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 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 );
@kateolenya
kateolenya / main.c
Last active March 14, 2019 10:33
Bypassing encapsulation with pointers in 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.c
Last active March 14, 2019 10:32
Private variables in struct in C
#include "private_var.h"
#include <stdio.h>
#include <stdlib.h>
struct Contact
{
int mobile_number;
int home_number;
};
@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 / 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_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