|
What are Constructors?
|
We know that all objects that are created on a class must be given initial values. This can be done in three ways -
1. Using the dot operator to access the instance variables and then assigns values to them individually.
2. Take the help of a function like GetData to initialize each object individually using statement like : rect1.GetData(5, 8);
3. Using constructors.
Constructors are special type of methods, that enables an object to initialize itself when it is created.
Constructors have the same name as the class itself. Secondly, they do not specyfy a return type, not even void. This because they do not return any value.
Constructors are usually public because they are provided to create objects. However they can also be declared as private or protected.
|
|
|
|
|
|
|
|