60.5k views
5 votes
What is the purpose of a constructor?

User Heldt
by
8.3k points

1 Answer

3 votes

Answer:

The main purpose of the constructor in the computer science is to initialize the object of the class. The constructor is basically called after the allocation of the memory in the object. When the object is created, the constructor are basically used to initialize as default value.

Constructor is also known as special type of the member function. The compiler called the constructor whenever the object has been create and the construction has similar name of the given class.

Example:

Class test() {

int p, r; // variable declaration

public:

// constructor

// Assigning value in the constructor

p=5;

r=10;

Cout<<" Constructor value\\";

}

User Shibi
by
7.8k points

No related questions found