Object-Oriented Programming

#folder

Object oriented programming is centred around objects. Classes act as blueprints for these objects, and instances are these blueprints incarnate.

Classes have methods (functions) and fields (variables). Each instance of a class will have its own value for these variables. For example, one balloon might be red while another might be blue.

C++

A class is the exact same as a struct in C++, expect the default access permission is private, while it's public in a struct.

Example

// Class declaration; often in .h file
class Balloon {
    public:
        Balloon();
        Balloon(string colour);
        virtual ~Balloon();
        void speak() const;
    private:
        string colour;
};

// Method definitions; often in a different file (.cc)
Balloon::Balloon() {
    this->colour = "clear";
}

Balloon::Balloon(string colour) {
    this->colour = colour;
}

Balloon::~Balloon() {}
    
void Balloon::speak() const {  
    cout << "I'm a " << this->colour << " balloon!" << endl; 
}