What is constructors and destructors in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is constructors and destructors in c++?

describe in class

4th Mar 2018, 12:53 PM
mahima
3 Answers
+ 3
In object oriented programming, a constructor is basically a function that is called when the object Is created. A destructor is called when the object is being destroyed (like going out of scope) class A { public: A() { std::cout << "A constructor called." << std::endl; } ~A(){ std::cout << "A destructor called." << std::endl; } void foo(){ std::cout << "foo called" << std::endl; } }; int main(){ A a; a.foo(); return 0; } This code will output: A constructor called. foo called A destructor called.
10th Mar 2018, 9:50 PM
Sean Patrick Franklin
Sean Patrick Franklin - 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. Which gets called automatically when program ends.
24th Apr 2018, 12:54 PM
Jayesh Patil
Jayesh Patil - avatar
0
tq
13th Mar 2018, 2:09 PM
mahima