Skip to content

Instantly share code, notes, and snippets.

@ivanleoncz
Last active February 28, 2022 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanleoncz/1e1922e779483dec8c90ad6b4c9e5c78 to your computer and use it in GitHub Desktop.
Save ivanleoncz/1e1922e779483dec8c90ad6b4c9e5c78 to your computer and use it in GitHub Desktop.
OOP Pillars (Encapsulation, and how Python is not good on that).
/* C++ demonstration of OOP Encapsulation principle. */
#include <iostream>
using namespace std;
class Car {
public:
int number_of_wheels = 0;
int door_angle_opening_degrees = 85;
protected:
string safe_belt_material = "polyester";
private:
int taillights = 2;
int rearlights = 2;
};
class Ferrari: public Car {
public:
void info() {
number_of_wheels = 4;
door_angle_opening_degrees = 90;
cout << "Number or Wheels: " << number_of_wheels << endl;
cout << "Door angle opening (degrees): " << door_angle_opening_degrees << endl;
cout << "Safe Belt material: " << safe_belt_material << endl;
};
void default_car_info() {
// This function will stop program compilation,
// for it's trying to access Private members from Car class.
cout << "Tailights: " << taillights << endl;
cout << "Rearlights: " << rearlights << endl;
}
};
int main()
{
Ferrari ferrari;
ferrari.info();
// Protected members are only accessible inside the Class and Subclasses.}
// Again, this code will not compile...
cout << "Safe belt material: " << ferrari.safe_belt_material << endl;
return 0;
}
g++ -o encapsulation encapsulation.cpp
# encapsulation.cpp: In member function ‘void Ferrari::default_car_info()’:
# encapsulation.cpp:30:39: error: ‘int Car::taillights’ is private within this context
# 30 | cout << "Tailights: " << taillights << endl;
# | ^~~~~~~~~~
# encapsulation.cpp:12:9: note: declared private here
# 12 | int taillights = 2;
# | ^~~~~~~~~~
# encapsulation.cpp:31:39: error: ‘int Car::rearlights’ is private within this context
# 31 | cout << "Rearlights: " << rearlights << endl;
# | ^~~~~~~~~~
# encapsulation.cpp:13:9: note: declared private here
# 13 | int rearlights = 2;
# | ^~~~~~~~~~
# encapsulation.cpp: In function ‘int main()’:
# encapsulation.cpp:42:47: error: ‘std::string Car::safe_belt_material’ is protected within this context
# 42 | cout << "Safe belt material: " << ferrari.safe_belt_material << endl;
# | ^~~~~~~~~~~~~~~~~~
# encapsulation.cpp:10:12: note: declared protected here
# 10 | string safe_belt_material = "polyester";
# | ^~~~~~~~~~~~~~~~~~
class Car:
# Public
number_of_wheels = 0
door_angle_opening_degrees = 85
# Protected
_safe_belt_material = "polyester"
# Private
__tail_lights = 2
__rear_lights = 2
class Ferrari(Car):
def info(self):
print("Number or Wheels (before): ", Car.number_of_wheels)
Car.number_of_wheels = 4;
print("Number or Wheels (after): ", Car.number_of_wheels)
print("Door angle opening (before): ", Car.door_angle_opening_degrees)
Car.door_angle_opening_degrees = 90;
print("Door angle opening (after): ", Car.door_angle_opening_degrees)
# Protected members are only accesible inside Classes and their Subclasses !!!
print("Safe Belt material (before): ", Car._safe_belt_material)
Car._safe_belt_material = "viscose"
print("Safe Belt material (after): ", Car._safe_belt_material)
# These Private members are only accesible inside Classes !!!
print("Number of Taillights (before): ", Car._Car__tail_lights)
print("Number of Rearlights (before): ", Car._Car__rear_lights)
Car._Car__tail_lights = 4
Car._Car__rear_lights = 4
print("Number of Taillights (after): ", Car._Car__tail_lights)
print("Number of Rearlights (after): ", Car._Car__rear_lights)
fer = Ferrari()
fer.info()
# Protected member being accessed outside Class and their Subclasses...
print("Safe belt material: ", fer._safe_belt_material)
# Again, Private members being accessed outside their Class!!!
print("Tail lights: ", fer._Car__tail_lights)
print("Rear lights: ", fer._Car__rear_lights)
# $ python3 encapsulation.py
# Number or Wheels (before): 0
# Number or Wheels (after): 4
# Door angle opening (before): 85
# Door angle opening (after): 90
# Safe Belt material (before): polyester
# Safe Belt material (after): viscose
# Number of Taillights (before): 2
# Number of Rearlights (before): 2
# Number of Taillights (after): 4
# Number of Rearlights (after): 4
# Tail lights: 4
# Rear lights: 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment