-
Notifications
You must be signed in to change notification settings - Fork 71
Constructor
What is constructor?
A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class. How is a constructor different from a normal member function:
- A Constructor has the same name as the class itself
- Constructors do not have return type
- A constructor is automatically called when an object is created.
- If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).
Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters.
/**
* Example class with a default constructor.
*/
class Rectangle{
float width;
float height;
// Default constructor
Rectangle(){
width = 0;
height = 0;
}
};
**Parameterized Constructors:** It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.
```cpp
/**
* Example class with a default constructor and a parameterized
* (in this case also overloaded) constructor.
*
*/
class Rectangle{
float width;
float height;
Rectangle(){
width = 0;
height = 0;
}
Rectangle(float w, float h){
width = w;
height = h;
}
};
We could require the class be initialized with parameters:
/**
* Example class with a only a parameterized constructor.
*
*/
class Rectangle{
float width;
float height;
Rectangle(float w, float h){
width = w;
height = h;
}
};
Copy Constructor:
Linked list good example. Show copy constructor as well as operator overloading.
In the case of a hierarchy of classes where a derived class inherits from a parent class, the execution sequence of the constructor is a call to the constructor of the parent class first and then that of the derived class. Constructors cannot be inherited.
A constructor can be declared using any of the access modifiers. It is mandatory to have a constructor with the right access modifier. However, the compiler supplies a default if an access modifier is not defined in the class. If a constructor is declared as private, the class cannot be created or derived and hence cannot be instantiated. Such a constructor, however, can be overloaded with different sets of parameters.
The following is recommended in constructor design: Logic involving specific operations that need to be executed at a particular event in an application - such as opening a database connection - should not be written in a constructor. When using derived class constructors, the parent class constructor should be passed the correct parameters. Better code maintainability comes from having the initialization and other related logic in one main constructor and cross-calling this constructor from other overloaded constructors. Because a constructor cannot return a value to the calling code, it is a good practice to throw an exception when a failure is encountered [ 1 ].