Object-Oriented Programming#

https://www.unionsquaredesign.com/wp-content/uploads/2016/10/CPT-OOP-objects_and_classes_-_attmeth.svg.png

Overview#

Definition

Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects, which are instances of classes. Classes are user-defined data types that encapsulate data and behavior. They allow us to model real-world entities and their interactions through the use of attributes (data) and methods (functions).

Note: not all languages are built for OOP. But OOP can be injected into your code through a little creative programming

Use Cases

OOP is widely used in various domains, such as software development, game development, simulation, and more. It helps in creating modular, maintainable, and extensible code.

Pros & Cons

👍
Modularity

Code can be organized into self-contained objects, making it easier to understand and maintain.

Reusability

Classes and objects promote code reuse, reducing redundant implementations.

Encapsulation

Data hiding ensures that the internal representation of an object is hidden, and access to it is restricted to methods, promoting security and data integrity.

Inheritance

Inheritance enables the creation of hierarchies and facilitates code sharing and extension.

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass, promoting flexibility and generality.

👎
Complexity

OOP can introduce additional complexity, especially for smaller projects where simpler paradigms might suffice.

Overhead

It can lead to performance overhead due to dynamic dispatch and object creation.

Learning Curve

Understanding and mastering OOP concepts can be challenging for beginners.

class / struct Syntax#

CLASS Animal:
    ATTRIBUTES:
        name
        age
    METHODS:
        constructor(name, age)
        make_sound()
    END CLASS

FUNCTION main():
    animal1 = Animal("Buddy", 3)
    animal1.make_sound()
    animal2 = Animal("Charlie", 5)
    animal2.make_sound()
END FUNCTION

 1#include <iostream>
 2using namespace std;
 3
 4class Animal {
 5public:
 6    string name;
 7    int age;
 8
 9    Animal(string name, int age) {
10        this->name = name;
11        this->age = age;
12    }
13
14    void make_sound() {
15        cout << "Animal sound!" << endl;
16    }
17};
18
19int main() {
20    Animal animal1("Buddy", 3);
21    animal1.make_sound();
22    Animal animal2("Charlie", 5);
23    animal2.make_sound();
24    return 0;
25}
 1class Animal:
 2    def __init__(self, name, age):
 3        self.name = name
 4        self.age = age
 5
 6    def make_sound(self):
 7        print("Animal sound!")
 8
 9def main():
10    animal1 = Animal("Buddy", 3)
11    animal1.make_sound()
12    animal2 = Animal("Charlie", 5)
13    animal2.make_sound()
14
15if __name__ == "__main__":
16    main()
 1class Animal {
 2    String name;
 3    int age;
 4
 5    Animal(String name, int age) {
 6        this.name = name;
 7        this.age = age;
 8    }
 9
10    void makeSound() {
11        System.out.println("Animal sound!");
12    }
13}
14
15public class Main {
16    public static void main(String[] args) {
17        Animal animal1 = new Animal("Buddy", 3);
18        animal1.makeSound();
19        Animal animal2 = new Animal("Charlie", 5);
20        animal2.makeSound();
21    }
22}
 1struct Animal {
 2    name: String,
 3    age: i32,
 4}
 5
 6impl Animal {
 7    fn new(name: &str, age: i32) -> Animal {
 8        Animal {
 9            name: name.to_string(),
10            age,
11        }
12    }
13
14    fn make_sound(&self) {
15        println!("Animal sound!");
16    }
17}
18
19fn main() {
20    let animal1 = Animal::new("Buddy", 3);
21    animal1.make_sound();
22    let animal2 = Animal::new("Charlie", 5);
23    animal2.make_sound();
24}
 1package main
 2
 3import "fmt"
 4
 5type Animal struct {
 6    name string
 7    age  int
 8}
 9
10func NewAnimal(name string, age int) *Animal {
11    return &Animal{name: name, age: age}
12}
13
14func (a *Animal) makeSound() {
15    fmt.Println("Animal sound!")
16}
17
18func main() {
19    animal1 := NewAnimal("Buddy", 3)
20    animal1.makeSound()
21    animal2 := NewAnimal("Charlie", 5)
22    animal2.makeSound()
23}