In this tutorial we shall learn about below different types of Constructors:
- Default Constructor
- Parameterized constructors
- Overloading constructors
- Constructor with default arguments
1. Default constructor:
- A constructor that does not take any arguments is called as default constructor.
- When an object is created default constructor is called.
- Default constructor cannot be overloaded, as it takes no parameters.
- If there is no default constructor defined in the class, the compiler will crete one.
- Default constructors are useful when we want to initialize all the data member with same values when objects are created.
- Constructors should be always public.
Example:
#include<iostream> using namespace std; class Ex { public: Ex() { cout<<"Default Constructor is called"<<endl; } }; int main() { Ex e; return 0; }
Output:
Default Constructor is called
2. Parameterized constructors
As we discussed earlier, we can overload a constructor. Hence we can create constructor that accepts parameters. Hence while creating objects we should pass the parameters to those objects.
Parameterized constructors:
- Constructors that accepts parameter are called as parameterized constructors.
- They can be overloaded.
- They are used when we need to initialize different objects with different values.
- They can have default values.
- If we define parameterized constructor, default constructor will not be created by compiler.
Parameterized constructor can be called by 2 types.
- Implicit call
- Explicit Call
#include<iostream> using namespace std; class Ex { private: int a; int b; public: Ex(int x, int y) { a = x; b = y; cout<<"Parameterized Constructor is called"<<endl; } void display() { cout<<"The value of a = "<<a<<" The value of b = "<<b<<endl; } }; int main() { Ex e(10, 20); //Implicit call e.display(); Ex e1 = Ex(20, 30); //Explicit call e1.display(); return 0; }
Output:
Parameterized Constructor is called The value of a = 10 The value of b = 20 Parameterized Constructor is called The value of a = 20 The value of b = 30
3. Overloading constructors
Like functions constructors can also be overloaded. Below is an example for the same.
/* * File : constructor_overloading.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> using namespace std; class Example { int num; public: Example(); Example(int a); void display(); }; Example :: Example() { cout<<"Default Constructor"<<endl; } Example :: Example(int a) { num = a; cout<<"Parameterized Constructor num value is = "<<num<<endl; } int main() { Example e1; Example e2(10); return 0; }
Output:
Default Constructor Parameterized Constructor num value is = 10
4. Constructor with default arguments.
Like function, constructors can also have default arguments. Same rules apply as function default arguments.
Example:
/* * File : constructor_Default_arguments.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> using namespace std; class Example { int num_1; int num_2; public: Example(int a, int b = 30); void display(); }; Example :: Example(int a, int b) { num_1 = a; num_2 = b; } void Example :: display() { cout<<"num_1 value is = "<<num_1 <<" and num_2 is = " << num_2<<endl; } int main() { Example e2(10); e2.display(); return 0; }
Output:
num_1 value is = 10 and num_2 is = 30
6. Constructor Initializer List in C++
Initializer List is used to initialize the members of a class.
Below example shows a simple way to do it:
#include<iostream> using namespace std; class MyClass{ int num1; int num2; public: MyClass(int a, int b):num1(a), num2(b){ } /* MyClass(int a, int b) { num1 = a; num2 = b; } */ }; int main(){ MyClass obj(10, 20); }