In software engineering, Creational Design Patterns deal with object creation mechanisms, i.e. try to create objects in a manner suitable to the situation. In addition to this basic or ordinary form of object creation could result in design problems or added complexity to the design.
Factory Design Pattern in C++ helps to mitigate this issue by creating objects using separate methods or polymorphic classes.
By the way, If you haven’t check out my other articles on Creational Design Patterns, then here is the list:
  1. Factory
  2. Builder
  3. Prototype
  4. Singleton
The code snippets you see throughout this series of articles are simplified not sophisticated. So you often see me not using keywords like override, final, public(while inheritance) just to make code compact & consumable (most of the time) in single standard screen size. I also prefer struct instead of class just to save line by not writing “public:” sometimes and also miss virtual destructor, constructor, copy constructor, prefix std::, deleting dynamic memory, intentionally.
I also consider myself a pragmatic person who wants to convey an idea in the simplest way possible rather than the standard way or using Jargons.
Note:

Intent

For the creation of wholesale objects unlike builder(which creates piecewise).

Motivation

struct Point {
    Point(float x, float y){ /*...*/ }         // Cartesian co-ordinates

    // Not OK: Cannot overload with same type of arguments
    // Point(float a, float b){ /*...*/ }      // Polar co-ordinates

    // ... Implementation
};
enum class PointType{ cartesian, polar };

class Point {
    Point(float a, float b, PointTypetype = PointType::cartesian) {
        if (type == PointType::cartesian) {
            x = a; b = y;
        }
        else {
            x = a * cos(b);
            y = a * sin(b);
        }
    }
};

Factory Design Pattern Examples in C++

Factory Method

enum class PointType { cartesian, polar };

class Point {
    float         m_x;
    float         m_y;
    PointType     m_type;

    // Private constructor, so that object can't be created directly
    Point(const float x, const float y, PointType t) : m_x{x}, m_y{y}, m_type{t} {}

  public:
    friend ostream &operator<<(ostream &os, const Point &obj) {
        return os << "x: " << obj.m_x << " y: " << obj.m_y;
    }
    static Point NewCartesian(float x, float y) {
        return {x, y, PointType::cartesian};
    }
    static Point NewPolar(float a, float b) {
        return {a * cos(b), a * sin(b), PointType::polar};
    }
};

int main() {
    // Point p{ 1,2 };  // will not work
    auto p = Point::NewPolar(5, M_PI_4);
    cout << p << endl;  // x: 3.53553 y: 3.53553
    return EXIT_SUCCESS;
}

Classical Factory Design Pattern

class Point {
    // ... as it is from above
    friend class PointFactory;
};

class PointFactory {
public:
    static Point NewCartesian(float x, float y) {
        return { x, y };
    }
    static Point NewPolar(float r, float theta) {
        return { r*cos(theta), r*sin(theta) };
    }
};

Inner Factory

class Point {
    float   m_x;
    float   m_y;

    Point(float x, float y) : m_x(x), m_y(y) {}
public:
    struct Factory {
        static Point NewCartesian(float x, float y) { return { x,y }; }
        static Point NewPolar(float r, float theta) { return{ r*cos(theta), r*sin(theta) }; }
    };
};

int main() {
    auto p = Point::Factory::NewCartesian(2, 3);
    return EXIT_SUCCESS;
}

Abstract Factory

Why do we need an Abstract Factory?

struct Point {
    virtual ~Point(){ cout<<"~Point\n"; }
};

struct Point2D : Point {
    ~Point2D(){ cout<<"~Point2D\n"; }
};

struct Point3D : Point {
    ~Point3D(){ cout<<"~Point3D\n"; }
};

void who_am_i(Point *who) { // Not sure whether Point2D would be passed here or Point3D
    // How to `create` the object of same type i.e. pointed by who ?
    // How to `copy` object of same type i.e. pointed by who ?
    delete who; // you can delete object pointed by who, thanks to virtual destructor
}

Example of Abstract Factory Design Pattern

struct Point {
    virtual ~Point() = default;
    virtual unique_ptr<Point> create() = 0;
    virtual unique_ptr<Point> clone()    = 0;
};

struct Point2D : Point {
    unique_ptr<Point> create() { return make_unique<Point2D>(); }
    unique_ptr<Point> clone() { return make_unique<Point2D>(*this); }
};

struct Point3D : Point {
    unique_ptr<Point> create() { return make_unique<Point3D>(); }
    unique_ptr<Point> clone() { return make_unique<Point3D>(*this); }
};

void who_am_i(Point *who) {
    auto new_who       = who->create(); // `create` the object of same type i.e. pointed by who ?
    auto duplicate_who = who->clone();    // `copy` the object of same type i.e. pointed by who ?
    delete who;
}

Functional Approach to Factory Design Pattern using Modern C++

struct Point { /* . . . */ };
struct Point2D : Point {/* . . . */};
struct Point3D : Point {/* . . . */};

class PointFunctionalFactory {
    map<PointType, function<unique_ptr<Point>() >>      m_factories;

public:
    PointFunctionalFactory() {
        m_factories[PointType::Point2D] = [] { return make_unique<Point2D>(); };
        m_factories[PointType::Point3D] = [] { return make_unique<Point3D>(); };
    }    
    unique_ptr<Point> create(PointType type) { return m_factories[type](); }  
};

int main() {
    PointFunctionalFactory pf;
    auto p2D = pf.create(PointType::Point2D);
    return EXIT_SUCCESS;
}

Benefits of Factory Design Pattern

  1. Single point/class for different object creation. Thus easy to maintain & understand software.
  2. You can create the object without even knowing its type by using Abstract Factory.
  3. It provides great modularity. Imagine programming a video game, where you would like to add new types of enemies in the future, each of which has different AI functions and can update differently. By using a factory method, the controller of the program can call to the factory to create the enemies, without any dependency or knowledge of the actual types of enemies. Now, future developers can create new enemies, with new AI controls and new drawing member functions, add it to the factory, and create a level which calls the factory, asking for the enemies by name. Combine this method with an XML description of levels, and developers could create new levels without having to recompile their program. All this, thanks to the separation of creation of objects from the usage of objects.
  4. Allows you to change the design of your application more readily, this is known as loose coupling.

Summary by FAQs

What is the correct way to implement the Factory Design Pattern in C++?
Abstract Factory & Functional Factory is always a good choice.
Factory vs Abstract Factor vs Functional Factory?
What's the difference between Abstract Factory and Builder Design Pattern?
When to use the Factory Design Pattern?
Employ Factory Design Pattern to create an object of required functionality(s) but type of object will remain undecided or it will be decided ob dynamic parameters being passed.