+ 1
What is constructors and destructors in c++?
describe in class
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.
+ 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.
0
tq