Constructor creation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Constructor creation

I don't understand, a constructor is called when you create an object or when you declare one?

14th Jan 2019, 6:15 PM
Celumusa Duma
Celumusa Duma - avatar
3 Answers
+ 1
The constructor is the part of the object in which the initial data of this object is declared when it is created.
14th Jan 2019, 6:27 PM
Sequencer
Sequencer - avatar
+ 2
The constructor is called when you create an Object of that class..All its operations are executed right away.. Eg... class sample{ sample(){ cout<<"Hello World"; } } //create object sample object; //outputs "hello world"
14th Jan 2019, 7:09 PM
Mensch
Mensch - avatar
0
If you declare (write) a (non-static) class, nothing is called there. This is a passive definition archived for future use. If you create an object using a class as a rule, by assigning it to the variable, the new object will be initialized with the constructor if the class has it.
15th Jan 2019, 12:03 AM
zemiak