Constructor of a class, Purpose of Constructor in C++ OOP
In this tutorial, we will learn about the followings;
- What is constructor of a class?
- What is the purpose of a class?
What is constructor in C++ OOP?
Constructor is a member function of the class. Constructor have the same name as the name of its class.
- When a new object of the class is executed, constructor also executed automatically.
- Constructor have no data type. Even we can’t use void also.
- Constructor can have arguments.
What is the purpose of constructor of a class?
The main purpose of the constructor is to assign initial values to the elements of the class.
Program of constructor of a class
|2||using namespace std;|
|7||cout<<“welcome to T4Tutorials.com”<<endl;|
Constructor is declared in line 3 with the same name as class name “t4tutorials”. When object in line 11 is created then constructor in line 3 is automatically executed.
|Test Your Understandings|
1.Multiple constructor for a class are possible? YES / NO
2. Contructor for a class is represented with tild sign ∼?
3. Constructor overloading is very helpful to free the computer memory?