MSDN Documentation

Object-Oriented Programming (OOP) in C++

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of fields (often known as attributes or properties) and code in the form of procedures (often known as methods or behaviors).

C++ is a powerful, multi-paradigm programming language that fully supports object-oriented programming. Understanding OOP principles is crucial for developing complex, maintainable, and scalable software.

Core Concepts of OOP

1. Encapsulation

Encapsulation is the bundling of data (attributes) and the methods that operate on that data into a single unit, called a class. It also involves the concept of data hiding, where the internal state of an object is protected from direct access from outside the class.

Encapsulation helps in modularity and data integrity by controlling access to member variables.

In C++, encapsulation is achieved using classes and access specifiers like public, private, and protected.


class Dog {
private:
    std::string name;
    int age;

public:
    Dog(std::string n, int a) : name(n), age(a) {}

    void bark() {
        std::cout << name << " says Woof!" << std::endl;
    }

    int getAge() const {
        return age;
    }
};
            

2. Abstraction

Abstraction means hiding the complex implementation details and showing only the essential features of an object. It allows you to focus on what an object does rather than how it does it.

In C++, abstraction is often achieved through abstract classes and pure virtual functions, or by designing interfaces that expose minimal functionality.


class Shape {
public:
    virtual double getArea() const = 0; // Pure virtual function
    virtual void draw() const = 0;      // Pure virtual function
};

class Circle : public Shape {
private:
    double radius;
public:
    Circle(double r) : radius(r) {}
    double getArea() const override { return 3.14159 * radius * radius; }
    void draw() const override { std::cout << "Drawing a circle." << std::endl; }
};
            

3. Inheritance

Inheritance is a mechanism that allows a new class (derived class or child class) to inherit properties and behaviors from an existing class (base class or parent class). This promotes code reusability and establishes a hierarchy.

Inheritance models the "is-a" relationship. For example, a 'Car' is a type of 'Vehicle'.

In C++, you can inherit publicly, privately, or protectedly.


class Vehicle {
public:
    void startEngine() { std::cout << "Engine started." << std::endl; }
};

class Car : public Vehicle {
public:
    void drive() { std::cout << "Car is driving." << std::endl; }
};
            

4. Polymorphism

Polymorphism, meaning "many forms," allows objects of different classes to be treated as objects of a common base class. This is typically achieved through virtual functions and function overloading.

Runtime Polymorphism (Dynamic Binding): Achieved using virtual functions. A single function call can perform different actions depending on the actual type of the object being referred to.


Shape* shape1 = new Circle(5.0);
shape1->draw(); // Calls Circle's draw()

Shape* shape2 = new Rectangle(4.0, 6.0);
shape2->draw(); // Calls Rectangle's draw()
            

Compile-time Polymorphism (Static Binding): Achieved through function overloading and operator overloading.

Classes and Objects

A class is a blueprint for creating objects. It defines the properties (data members) and behaviors (member functions) that objects of that class will have.

An object is an instance of a class. It has its own state (values for its data members) and can perform actions defined by its class's member functions.

Constructors and Destructors

Constructors are special member functions that are automatically called when an object of a class is created. They are used to initialize the object's data members.

Destructors are also special member functions that are automatically called when an object is destroyed. They are used to clean up resources allocated by the object.


class MyClass {
public:
    MyClass() { // Constructor
        std::cout << "Object created!" << std::endl;
    }
    ~MyClass() { // Destructor
        std::cout << "Object destroyed!" << std::endl;
    }
};
            

Next Steps

Continue your learning by exploring specific implementations and advanced OOP patterns in C++. You might also be interested in: