Skip to content

Instantly share code, notes, and snippets.

@ivanleoncz
Last active February 28, 2022 02:53
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/b185ae38751b55d07684af183081e475 to your computer and use it in GitHub Desktop.
Save ivanleoncz/b185ae38751b55d07684af183081e475 to your computer and use it in GitHub Desktop.
OOP Pillars (Inheritance)
#include <iostream>
using namespace std;
class Vehicle {
public:
int number_of_wheels = 0;
public:
string turn_on_engine() {
return "Turning ON the Vehicle engine.";
};
string turn_off_engine() {
return "Turning OFF the Vehicle engine.";
};
};
/*
Virtual Inheritance maintains a single copy of all members from a Base Class to second-level derivatives,
providing a solution for the Diamod Problem (A -> B, C -> D), for both B and C will have inherit members from A,
and D should inherit A member from B or C (and not both), besides normally inheriting members that belong to B or C.
*/
class MotorCycle: public virtual Vehicle {
public:
string hit_the_breaks() {
return "Breaks activated; stopping wheels rotation.";
};
};
class RotorCraft: public virtual Vehicle {
public:
int number_of_rear_wings = 2;
int number_of_tail_wings = 2;
string rotate_rear_wings() {
return "Rotating Rear wings!";
};
string rotate_tail_wings() {
return "Rotating Tail wings!";
};
};
class PalVone: public MotorCycle, public RotorCraft {
private:
bool road_mode = false;
bool air_mode = false;
public:
string type;
PalVone(string pal_type="prototype") {
type = pal_type;
cout << "Building PAL V-One of type " << pal_type << endl;
};
string turn_on_road_mode() {
air_mode = false;
road_mode = true;
return "Turning ON 'road' mode.";
};
string turn_off_road_mode() {
air_mode = false;
road_mode = false;
return "Turning OFF 'road' mode.";
};
string turn_on_air_mode() {
air_mode = true;
road_mode = false;
return "Turning ON 'air' mode.";
};
string turn_off_air_mode() {
air_mode = false;
road_mode = false;
return "Turning OFF 'air' mode.";
};
string take_off() {
if (air_mode == true) {
return "Take off!";
} else {
return "Air Mode is disabled. Please, enable Air Mode (turn_on_air_mode method)";
};
};
string land() {
if (air_mode == true) {
return "Landing...";
} else {
return "Air Mode is disabled. Please, enable Air Mode (turn_on_air_mode method)";
};
};
string move_forward() {
return "Move Forward!";
};
string move_backward() {
if (road_mode == true) {
return "Move Backward!";
} else {
return "Road Mode is disabled. Please, enable Road Mode (turn_on_road_mode method)";
};
return "Move Backward!";
};
string turn_left() {
return "Turn left!";
};
string turn_right() {
return "Turn right!";
};
string start_acceleration() {
return "Accelerating...";
};
string stop_acceleration() {
return "Stopping acceleration...";
};
};
int main() {
PalVone pal("production");
cout << pal.turn_on_engine() << endl;
cout << pal.turn_on_air_mode() << endl;
cout << pal.rotate_rear_wings() << endl;
cout << pal.rotate_tail_wings() << endl;
cout << pal.take_off() << endl;
cout << pal.start_acceleration() << endl;
cout << pal.move_forward() << endl;
cout << pal.turn_left() << endl;
cout << pal.turn_right() << endl;
cout << pal.stop_acceleration() << endl;
cout << pal.hit_the_breaks() << endl;
cout << pal.land() << endl;
cout << pal.turn_off_engine() << endl;
};
class Vehicle:
def turn_on_engine(self):
return "Turning ON the Vehicle engine."
def turn_off_engine(self):
return "Turning OFF the Vehicle engine."
class MotorCycle(Vehicle):
def hit_the_breaks(self):
return "Breaks activated; stopping wheels rotation."
class RotorCraft(Vehicle):
number_of_rear_wings = 2
number_of_tail_wings = 2
def rotate_rear_wings(self):
return "Rotating Rear wings!"
def rotate_tail_wings(self):
return "Rotating Tail wings!"
# C3 linearization algorithm is the base of the MRO (Method Resolution Order) mechanism,
# that allows member resolution when using Multiple Inheritance in Python, using depth-first
# lookup order (searches for member on first inherited class, moving to the next one, and so forth).
class PalVone(MotorCycle, RotorCraft):
__road_mode = False
__air_mode = False
def __init__(self, pal_type="prototype"):
self.type = pal_type
def turn_on_road_mode(self):
PalVone.__road_mode = True
PalVone.__air_mode = False
def turn_off_road_mode(self):
PalVone.__road_mode = False
PalVone.__air_mode = False
def turn_on_air_mode(self):
PalVone.__road_mode = False
PalVone.__air_mode = True
def turn_on_air_mode(self):
PalVone.__road_mode = False
PalVone.__air_mode = False
def take_off(self):
if PalVone.__air_mode:
return "Take off!"
else:
return "Air Mode is disabled. Please, enable Air Mode (turn_on_air_mode method)"
def land(self):
if PalVone.__air_mode:
return "Landing..."
else:
return "Air Mode is disabled. Please, enable Air Mode (turn_on_air_mode method)"
def move_forward(self):
return "Move forward!"
def move_backward(self):
if PalVone.__road_mode:
return "Move backward!"
else:
return "Road Mode is disabled. Please, enable Air Mode (turn_on_air_mode method)"
return "Move forward!"
def turn_left(self):
return "Turn left!"
def turn_right(self):
return "Turn right!"
def start_acceleration(self):
return "Accelerating..."
def stop_acceleration(self):
return "Stopping acceleration..."
pal = PalVone("production")
print(pal.turn_on_engine())
print(pal.turn_on_air_mode())
print(pal.rotate_rear_wings())
print(pal.rotate_tail_wings())
print(pal.take_off())
print(pal.start_acceleration())
print(pal.move_forward())
print(pal.turn_left())
print(pal.turn_right())
print(pal.stop_acceleration())
print(pal.turn_off_engine())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment