Skip to main content

Fundamentals

Note on Documentation:

In the list of topics, if it has a "🤘" emoji next to it, that means it's being worked on. If it has NO emoji, that means it's blank/empty.

Four Pillars of OOP​

  1. Encapsulation - Setting stuff as public/private in Class/Objects
  2. Abstraction - Hiding implementation details (_int main()_)
  3. Inheritance - A class/object inherits stuff defined in a diff class/object (child : parent)
  4. Polymorphism - Allowing children to override stuff defined in parent object, with their own definition

  1. Encapsulation - Public/Private
    • The action of enclosing something in or as if in a capsule. Pretty much it's when you create an object, all of the stuff within that object, has a specific state. This can vary from being public or private.
  1. Abstraction - hide implementation details, setters/getters
    • To abstract something away means to hide away the implementation details inside something - sometimes a prototype, sometimes a function. So when you call the function you don't have to understand what it's doing.
      • Example: Let's say that you have a C++ program that reads and writes to a file. Rather than doing all of this within the main function, we create a readFile & writeFile function.
      • Setters & Getters
  1. Inheritance - Inherit stuff from parent function
    • This let's one object aquire the properties & methods of another object.
    • Main reason you would use inheritance is when you have an object that you know has many subtypes. Aka if you have an object type with several branches underneath it.
    • Examples:
      • Allowing dog & cat to access data & functions from animal (see below).
      • Pieces in chess. The parent object would include stuff like the current location, where as the child objects would include things such as where the piece can move to, who it belongs to, how much points is it worth, etc.
    • Other Notes
      • don't just make an object a child of a parent object just because you need to use a specific function defined within it
        • Liskov Substitution principle states that if you can use a parent class anywhere you use a child class - and child inherits from the parent, then it passes the test.
class Animal {
public:
Animal(int age){ this->age = age; }
int getAge(){ return age; }

// a pure virtual function
// Animal is now an abstract class
virtual void speak() = 0;

private:
int age;
};

// INHERITANCE - Cat Inherits from Animal
class Cat : public Animal {
public:
Cat(int age, int numPaws) : Animal(age) {
this->numPaws = numPaws;
}

int getNumPaws(){ return numPaws; }
void speak(){ cout << "Meow." << endl; }

private:
int numPaws;
};

// INHERITANCE - Dog Inherits from Animal
class Dog : public Animal {
public:
Dog(int age, bool hasTail) : Animal(age) {
this->hasTail = hasTail;
}

int getHasTail(){ return hasTail; }
void speak(){ cout << "Woof." << endl; }

private:
int hasTail;
};

  1. Polymorphism - Allowing Children to override stuff defined in parent object, with their own defintion
    • The condition of occuring in several different forms.
    • Pretty much its when the parent object has specified a specific data or function, that can be overwritten by children.
    • The power of polymorphism is sharing behaviours & allowing custom overrides
// POLYMORPHISM - Creating 2 "types" of animals in the same data structure
int main() {
// Create an array with two animal pointers
Animal* myPets[2];
myPets[0] = new Cat(5,4);
myPets[1] = new Dog(3,true);

// myPets[0]->speak() --> "Meow"
// myPets[1]->speak() --> "Woof"

return 0;
}