# C++ Instantiable or not ## What does "instantiable" mean in C++ - "Instantiable" means: > You can create objects (instances) of a class - Example ```c++ class Dog { // ... }; int main() { Dog d; // ✅ "Dog" is instantiable — this creates an instance } ``` - Dog is instantiable because you can write Dog d; ## When does a class not instantiable? ### Abstract class (with pure virtual function) - A class with at least one pure virtual function is abstract - You cannot create an object from it directly - ➡️ But you can derive from it and instantiate the child if it implements the pure virtual function ```c++ class Animal { public: virtual void makeSound() const = 0; // pure virtual }; Animal a; // ❌ Error: Animal is abstract ``` ### Private constructor (singleton pattern, static factory, etc.) ```c++ class A { private: A() {} }; A a; // ❌ Error: constructor is private ``` ### Deleted constructor ```c++ class B { public: B() = delete; }; B b; // ❌ Error: constructor is deleted ``` ## Why would you want a class to be not instantiable? Under conditions when you create a class not to make objects from it, but rather to - Serve as a base class (abstract interface) - Provide only static utilities (no object needed) - Enforce factory or singleton patterns - Prevent misuse of the class - Model conceptual entities that don't have a direct object form