0

What are constructors and destructors?

what r constructors and destructors used for in cpp.... can anyone xplain with examples plz....thank u

19th Dec 2016, 4:20 PM
DEF4LT_
DEF4LT_ - avatar
1 Answer
+ 1
***When an object is created, constructors are used for proper initialisation of object variables. There can be multiple constructors for class depending on type of arguments, Number of arguments. ***when the usage of object is terminated, destructors are called to free up the memory used by object. ***The name of the constructor should be same as class name and name of destrutor will be same as class name preceeded by symbol ~ ***Constructor can be many types: default constructor, copy construtor, assignment constructor. ***Constructors and destructors do not have any return type(not even void) *** Here is a small example Class A { int a,b; public: A(){ a=10,b=20;} // default constructor A(int x, int y) { a=x;b=y;} // parameter changed };
19th Dec 2016, 5:11 PM
Manikanta Nallamalli (Mittu)
Manikanta Nallamalli (Mittu) - avatar