FAQ Part 6: Object-Oriented Programming

What are classes and objects?

A class is a blueprint for creating objects, which are instances of classes.

class Car {
public:
    string model;
    void drive() {
        cout << "Driving " << model;
    }
};

What is inheritance?

Inheritance allows a class to acquire properties of another.

class Animal {
public:
    void speak() { cout << "Animal"; }
};

class Dog : public Animal {
    // Inherits speak()
};

What is polymorphism?

Polymorphism allows the same interface to have different implementations. It comes in two forms:

  • Compile-time: Function/operator overloading
  • Runtime: Virtual functions

What is encapsulation?

Encapsulation hides the internal details and exposes only necessary parts via access specifiers (private, public, protected).