Constructor of a class, Purpose of Constructor in C++ OOP

Constructor of a class, Purpose of Constructor in C++ OOP

In this tutorial, we will learn about the followings;

  1. What is constructor of a class?
  2. 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;
3 class t4tutorials
4 {
5 public:
6 t4tutorials() {
7 cout<<“welcome to”<<endl;
8 }
9 };
10int main(){
11t4tutorials x;

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

Answer - Click Here:

2. Contructor for a class is represented with tild sign ∼?

Answer - Click Here:

3. Constructor overloading is  very helpful to free the computer memory?

Answer - Click Here: