0

Why we need constructor and destructor

in c++ we can create member function instead of constructor. why we creating the. constructor what is difference between members function and constructor? only the name?

14th Oct 2017, 6:19 AM
Ponraj Hbk
Ponraj Hbk - avatar
2 Antworten
+ 2
I would add that a constructor is way less code, because you only type it once and it is automatically called whenever you create an instance, while you would have to call the member functions for every instance seperatly. Furthermore, constructors allow you to assign values also to constant variables (Initilization List) which is not possible with functions. Destructors are important for releasing memory space or performing specific actions upon destruction of an instance, and again, you only have to type the code once.
14th Oct 2017, 9:14 AM
Shadow
Shadow - avatar
+ 1
in the class there can be many methods that can be applied to the object and it is much more convenient than many functions if you have the described class then this can be said as a new type, such as class TIME, you can create a class object in you can do different actions with it eg add another time, change it, etc.
14th Oct 2017, 8:50 AM
Іван Пиляк
Іван Пиляк - avatar