Why do we need a private constructor?
Suppose we’re designing a special type of car, SuperCar. We want to ensure that every SuperCar that rolls out from the factory is top-notch, with all the advanced features and safety measures properly implemented. But we don’t want just anyone to be able to build a SuperCar without following the right procedures or meeting certain standards.
So, what should we do? We keep the blueprints and assembly instructions for the SuperCar inside the factory, and only trained engineers and technicians can access them. This way, we ensure that every SuperCar is built according to our high standards and specifications.
In programming, a private constructor is like those restricted blueprints and assembly instructions inside the car factory. It’s a special method inside the SuperCar class hidden from the outside world. This private constructor controls how new SuperCar objects are created, ensuring they’re built correctly and meet all the required standards.
Here’s how we can relate this to the concept of a private constructor:
Controlled construction: Just like only trained engineers in the car factory can access the blueprints and build a SuperCar, a private constructor controls how new SuperCar objects are constructed. It ensures that only the right methods or conditions can create new SuperCar instances.
Prevent unauthorized creation: By hiding the constructor, we prevent anyone from creating SuperCar objects without following the proper procedures or meeting certain requirements. This helps maintain the integrity and quality of the SuperCar class.
Encapsulation of design: Keeping the constructor private encapsulates the construction logic inside the SuperCar class. This means the details of how a SuperCar is constructed are hidden from the outside world, making the class easier to manage and maintain.
Coding example
Let’s write down a complete code on the scenario above:
#include <iostream>using namespace std;class SuperCar {private:string model;int year;bool assembled;// Private constructorSuperCar(string model, int year) : model(model), year(year), assembled(false) {}public:// Public method to create a new SuperCarstatic SuperCar* create(string model, int year) {// Only allow creation if model and year are validif (!model.empty() && year > 0) {return new SuperCar(model, year);} else {return nullptr; // Invalid parameters, return nullptr}}// Method to assemble the SuperCarvoid assemble() {if (!assembled) {cout << "Assembling " << model << " " << year << "..." << endl;assembled = true;} else {cout << "Car is already assembled!" << endl;}}// Method to display SuperCar detailsvoid displayDetails() {cout << "SuperCar Details:" << endl;cout << "Model: " << model << endl;cout << "Year: " << year << endl;cout << "Assembled: " << (assembled ? "Yes" : "No") << endl;}};int main() {// Attempt to create a SuperCar using the public create methodSuperCar* myCar = SuperCar::create("Tesla", 2023);// Display car details (should show assembled as false)myCar->displayDetails();// Assemble the carmyCar->assemble();// Display car details after assembling (should show assembled as true)myCar->displayDetails();// Attempt to create another SuperCar directly (should not be allowed)// SuperCar* anotherCar = new SuperCar("Ferrari", 2024); // This line will give a compile-time errordelete myCar; // Clean up the memoryreturn 0;}
Code explanation
Lines 4–8: The
SuperCarclass represents a special type of car. It has private member variablesmodel,year, andassembled.Line 11: The
SuperCarclass has a private constructor, which means that objects of this class cannot be created directly using thenewkeyword. Instead, thecreatestatic method is used to create newSuperCarobjects.Lines 15–22: The
create()static method is used to create a newSuperCarobject. It checks if the provided model is not empty and the year is greater than 0 before creating the object. If the conditions are not met, it returnsnullptr, indicating that the object creation failed.Lines 25–32: The
assemble()method is used to mark the car as assembled. It checks if the car has already been assembled before marking it as assembled.Lines 35–40: The
displayDetails()method is used to display the details of theSuperCar, including the model, year, and whether it has been assembled or not.Lines 45–54: In the
main()function, an attempt is made to create aSuperCarobject using thecreate()method. The car details are displayed before and after assembling the car. An attempt to create anotherSuperCarobject directly using thenewkeyword would result in a compilation error due to the private constructor.
Free Resources