25 Constructors in C++
1. Constructors
Reason: When we instantiate the Entity
class and allocate memory, the memory is not actually initialized yet.
Common Practice:
Constructor
A constructor is a special type of method that is primarily used here. It is called whenever you create an object ^0156ab.
class Entity
{
public:
float X, Y;
Entity() // Constructor
{
X = 0.0f;
Y = 0.0f;
}
void Print()
{
std::cout << X << "," << Y << std::endl;
}
};
int main()
{
Entity e;
std::cout << e.X << std::endl; // 0
e.Print(); // 0,0
std::cin.get();
};
Default Constructor
The default constructor is provided by default, which is essentially like an empty function body that does nothing.
No variables are initialized.
In C++, you must manually initialize all primitive types, otherwise they will be set to whatever value was previously left in memory.
2. Function Overloading
You can write multiple constructors with the same name but provide different parameters.
When creating an object instance using the new keyword, the constructor is also called.
I only want others to use this class like Log::Write
, and I don't want others to create instances.
a. Setting the Constructor to Private
Or use
b. Delete
Deletes the default constructor.