Constructor & Destructors | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Constructor & Destructors

can anyone please help me out

14th Apr 2018, 12:49 PM
Dark Villain
Dark Villain - avatar
18 Answers
+ 3
//C++ example class Cat { int age; int weight; Cat (int a, int w) { //this is constructor age = a ; weight = w; } ~Cat() { // this is destructor; cout << “delete an instance” ; } } Cat ob = new Cat(3, 25); P.S. new Cat calls Constructor and passing 2 arguments to him like any other functions
14th Apr 2018, 1:16 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 3
simply you can think of constructor &destructors like special functions (piece of code ) than runs when an object of that class is created & destoryed respectivly
14th Apr 2018, 1:18 PM
Ashraf Al_Absi
Ashraf Al_Absi - avatar
+ 3
Thank you for your code.. It helpd me
14th Apr 2018, 1:19 PM
Dark Villain
Dark Villain - avatar
+ 3
in c++ constructors and destructors have the same name as class and haven’t return type, there is ~ symbol before destructor
14th Apr 2018, 1:21 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 3
okay
14th Apr 2018, 1:26 PM
Dark Villain
Dark Villain - avatar
+ 2
can you please teach me with an example for both. it will be helpful for me
14th Apr 2018, 1:01 PM
Dark Villain
Dark Villain - avatar
+ 2
thanks buddy
14th Apr 2018, 1:06 PM
Dark Villain
Dark Villain - avatar
+ 2
yea, i have learn class and objects in cpp but constructors and destructors are daunting me
14th Apr 2018, 1:08 PM
Dark Villain
Dark Villain - avatar
+ 2
does constructor always has the same name as the class?
14th Apr 2018, 1:23 PM
Dark Villain
Dark Villain - avatar
+ 2
no, it is in c++ , it’s about my c++ example.
14th Apr 2018, 1:25 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 2
for example in python it calls __init__
14th Apr 2018, 1:25 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 1
constructors,destructors are like special functions. constructors get called when an object is created, destructors get called when an object is destroyed.
14th Apr 2018, 12:59 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
thank you Ashraf Al_Absi
14th Apr 2018, 1:20 PM
Dark Villain
Dark Villain - avatar
+ 1
Okey
20th Apr 2018, 11:17 PM
Isaiah
Isaiah - avatar
+ 1
Working : Constructor is a special member function whose task is to initialise an object of it's class. & Destructor is a special member function whose task is to destroy the object created by constructor.
24th Apr 2018, 12:42 PM
Jayesh Patil
Jayesh Patil - avatar
0
well they exist in several languages.. in js class Obj{ constructor(){ alert("hi");} } man=new Obj(); //i created an object "man" from class Obj here so the constructor is called then the code alerts "hi" . As i said they exist in several languages and i dont know if this js examole is helpful. desteuctors are called by default in js so no need declaring them
14th Apr 2018, 1:04 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
0
I don't understand very well
20th Apr 2018, 11:17 PM
Isaiah
Isaiah - avatar
- 1
if you want to understand constructors and destructors you might need to learn classes and objects first
14th Apr 2018, 1:06 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar