{"id":88,"date":"2018-07-10T05:27:36","date_gmt":"2018-07-10T05:27:36","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=88"},"modified":"2019-05-14T11:49:05","modified_gmt":"2019-05-14T11:49:05","slug":"constructor-destructor","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/constructor-destructor\/","title":{"rendered":"Constructor &amp;  Destructor"},"content":{"raw":"\r\n<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/ILZ_ghQ5vb0\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n\r\nIn the last module we have covered the following topics:\r\n<ul>\r\n \t<li>Constructor<\/li>\r\n \t<li>Compiler defined Default Constructor<\/li>\r\n \t<li>User defined Default Constructor<\/li>\r\n \t<li>Parameterized Constructor<\/li>\r\n \t<li>Constructor Overloading<\/li>\r\n \t<li>Destructors<\/li>\r\n<\/ul>\r\n<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe have seen how we can create a parameterized constructor. A parameterized constructor is one which has one or more arguments of any type. But there is one exception to this-\r\n\r\n&nbsp;\r\n\r\n<strong>A single parameterized constructor can have parameter of any type except the object of the same class.<\/strong>\r\n\r\n&nbsp;\r\n\r\nfor example we cannot create the following Constructor in the Time class\r\n\r\n&nbsp;\r\n\r\nTime(Time t)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\n\/\/ code\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nWe may need this constructor if we want to create copy of an object . For example:\r\n\r\nTime t1 (10,30,6) ;\r\n\r\nTime t2 (t1);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ creating the copy of object t1\r\n\r\nHere, to construct object <strong><em>t2<\/em><\/strong> we need the above constructor. But compiler will not allow us to create this constructor.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The reason is very logical: Objects are passed by value in C++ , thus when an object is passed by value , a temporary copy of object is created in the memory. Here, Time object is passed as parameter to constructor thus a temporary Time object is required to be created , to create this temporary Time object the same constructor with Time object as parameter would be invoked and this process would run infinitely. Thus to avoid this situation compiler does not allow to pass a Time object in constructor as parameter.<\/p>\r\n&nbsp;\r\n\r\nIf passing the object of the same class is not possible in the constructor as a parameter then how to create copy of an object?\r\n\r\n&nbsp;\r\n\r\nThe answer is \u2013 <strong>Copy Constructor<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Copy Constructor:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A copy constructor is a constructor which has the object reference as a parameter i.e. instead of passing object by value we pass reference of the object. The syntax of copy constructor is:<\/p>\r\n&nbsp;\r\n\r\n<strong>ClassName ( const ClassName &amp; )<\/strong>\r\n\r\n&nbsp;\r\n\r\nHere the parameter is reference of object and not the copy of object, therefore the object is passed by address.\r\n\r\n&nbsp;\r\n\r\n<strong>(Note: Here the object reference should be passed as const so that the original object is not accidently modified in the copy constructor. )<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Just like constructor and destructor C++ compiler also provides a copy constructor . Whenever copy of an object is created , this copy constructor is invoked . Compiler provided copy constructor creates a shallow copy of the object i.e. it copies the object bit by bit.<\/p>\r\n&nbsp;\r\n\r\nThe below statement will invoke compiler provided copy constructor for creating object <strong>t2<\/strong> which creates the exact copy of object <strong>t1 :<\/strong>\r\n\r\n&nbsp;\r\n\r\nTime t2(t1) ;\r\n\r\n&nbsp;\r\n\r\n<strong>Copy constructor is always invoked in the following cases:<\/strong>\r\n<ol>\r\n \t<li>An object is initialized with the other object of the same class. For example:<\/li>\r\n<\/ol>\r\nTime t1(12,30,30);\r\n\r\nTime t2 = t1 ;\r\n\r\nTime t3(t1) ;\r\n\r\nHere both object t2 and t3 are created using copy constructor\r\n\r\n&nbsp;\r\n\r\n<strong>Note: <\/strong>The statement Time t3(t1) is same as Time t3=t1\r\n\r\nHere the object t1 is created by invoking a usual parameterized constructor having 3 arguments.\r\n\r\n&nbsp;\r\n\r\nBut the object t2 and t3 are\u00a0\u00a0 initialized with object t1 , so here copy constructor is invoked.\r\n\r\n&nbsp;\r\n\r\n<strong>Note <\/strong>that if we first create an object\u00a0 and then assign another object as:\r\n\r\n&nbsp;\r\n\r\n<strong>Time t3 ;<\/strong>\r\n\r\n<strong>t3= t2 ;<\/strong>\r\n\r\n<strong>Then object t3 is created using default constructor and not the copy constructor.<\/strong>\r\n<ol start=\"2\">\r\n \t<li style=\"text-align: justify\">When an object is passed as parameter to a function and when a function returns an object. In both the cases compiler needs to create a temporary object and this temporary object is created using copy constructor.<\/li>\r\n<\/ol>\r\n<strong>Example:<\/strong>\r\n\r\n&nbsp;\r\n\r\nvoiddisplayTime(Time t) ;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The parameter is a Time object\r\n\r\n&nbsp;\r\n\r\nTime calculateTime(int milliseconds ) ;\u00a0\u00a0\u00a0\u00a0 \/\/ Return type is Time object\r\n\r\n&nbsp;\r\n\r\n<strong>User Defined Copy Constructor :<\/strong>\r\n\r\n&nbsp;\r\n\r\nUser can also define a copy constructor . If user defines a copy constructor then compiler will not provide any copy constructor.\r\n\r\n&nbsp;\r\n\r\n<strong>Example:<\/strong>\r\n\r\n&nbsp;\r\n\r\nFor Time class the user defined copy constructor can be written as:\r\n\r\n&nbsp;\r\n\r\nclass Time{\r\n\r\n\/\/ data members and methods\r\n\r\n&nbsp;\r\n\r\nTime (const Time &amp; t)\r\n\r\n{\r\n\r\nseconds = t.seconds ;\r\n\r\nminutes = t.minutes ;\r\n\r\nhours\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = t.hours ;\r\n\r\n}\r\n\r\n};\r\n<div>\r\n\r\n<strong>Need for User Defined Copy Constructor :<\/strong>\r\n\r\n&nbsp;\r\n\r\nIf bit by bit copy i.e. field by field copy is required then there is no need to define a user defined copy constructor as the compiler defined copy constructor does the same job.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the previous example the Time class actually does not require the user defined copy constructor as the compiler defined copy constructor does the same thing i.e. field by field copy.<\/p>\r\n&nbsp;\r\n\r\nUser defined copy constructor is needed when a deep copy is required. Usually it is required if there is dynamic memory allocation in constructor. Consider the following example:\r\n\r\n&nbsp;\r\n\r\nclass Vector\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nint size;\r\n\r\nint *arr ;\r\n\r\npublic:\r\n\r\n<\/div>\r\n<div>\r\n\r\nVector()\r\n\r\n{\r\n\r\n\/\/Default Constructor\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">size=0;<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\narr=null\r\n\r\n}\r\n\r\n<\/div>\r\nVector(int s)\r\n\r\n{\r\n\r\nsize=s;\r\n\r\narr=new int[size] ;\r\n\r\n&nbsp;\r\n\r\nfor(int i=0 ; i&lt;size ;i++)\r\n\r\n{\r\n\r\narr[i] = 0 ;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\n~Vector()\r\n\r\n{\r\n\r\ncout&lt;&lt;\u201ddestructor invoked \u201d ;\r\n\r\ndelete [ ] arr ;\r\n\r\n}\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nVector v1(5);\r\n\r\nVector v2=v1;\r\n\r\n&nbsp;\r\n\r\n}\r\n<p style=\"text-align: justify\">Here object <strong><em>v2<\/em><\/strong> is created using compiler provided copy constructor therefore the data members of object <strong><em>v2<\/em><\/strong> will be the exact copy of data members of object <strong><em>v1<\/em><\/strong>. Thus the pointer variable <strong><em>*arr<\/em><\/strong> will also be copied as it is. The address pointed by <strong><em>v1.arr<\/em><\/strong> is copied to <strong><em>v2.arr<\/em><\/strong> and no separate dynamic memory is allocated to the pointer <strong><em>v2.arr<\/em><\/strong><\/p>\r\n&nbsp;\r\n\r\nThus <strong><em>v1.arr<\/em><\/strong> and <strong><em>v2.arr<\/em><\/strong> both will point to the same memory area.\r\n\r\n&nbsp;\r\n\r\nLets understand this point with the help of a diagram:\r\n\r\n<img class=\"alignnone size-full wp-image-89 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-24.png\" alt=\"\" width=\"702\" height=\"846\" \/>\r\n\r\nHere both object are sharing the same address in <strong><em>arr<\/em><\/strong> which can creates problem in the following cases:\r\n\r\n&nbsp;\r\n<ol>\r\n \t<li>When one object modify this <strong><em>arr<\/em><\/strong> pointer, the changes are reflected in the other object also.<\/li>\r\n<\/ol>\r\nSuppose if we write the following statements:\r\n\r\n&nbsp;\r\n\r\nv1.arr[0] = 10 ;\r\n\r\ncout&lt;&lt;v1.arr[0] &lt;&lt; \u201c\\n\u201d;\r\n\r\ncout&lt;&lt;v2.arr[0] ;\r\n\r\n&nbsp;\r\n\r\noutput: 10\r\n\r\n&nbsp;\r\n\r\n10\r\n\r\n&nbsp;\r\n\r\nThis is happening because both <strong><em>v1.arr<\/em><\/strong> and <strong><em>v2.arr<\/em><\/strong> are sharing the same memory and no separate dynamic memory is allocated for <strong><em>v2.arr<\/em><\/strong>.\r\n<ol start=\"2\">\r\n \t<li style=\"text-align: justify\">Here if one of the object goes out of scope then the user defined destructor is invoked which will deallocate the dynamic memory while the other object is still pointing to the same memory which is being deallocated by compiler.<\/li>\r\n<\/ol>\r\nconsider the following example:\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nTime t1(10,20,30) ;\r\n\r\n{\r\n\r\nTime t2(t1) ;\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li>\/\/ some code<\/li>\r\n<\/ul>\r\n} \/\/ object t2 goes out of scope\r\n\r\n&nbsp;\r\n\r\n}\r\n<div>\r\n\r\nIn the code above when object <strong><em>t2<\/em><\/strong> goes out of scope , dynamic memory is deallocated by destructor. But object <strong><em>t1<\/em><\/strong> is still pointing to same memory. See the diagram:\r\n\r\n<img class=\"alignnone size-full wp-image-90\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-25.png\" alt=\"\" width=\"942\" height=\"462\" \/>\r\n\r\nTo avoid this kind of behavior, user defined copy constructor is needed. Let\u2019s see how to define a copy constructor in the above example:\r\n\r\n&nbsp;\r\n\r\nclass Vector\r\n\r\n{\r\n\r\nint size;\r\n\r\nint *arr ;\r\n\r\npublic:\r\n\r\n\/\/Default Constructor\r\n\r\nVector()\r\n\r\n{\r\n\r\nsize=0;\r\n\r\narr=null\r\n\r\n}\r\n\r\nVector(int s)\r\n\r\n{\r\n\r\nsize=s;\r\n\r\narr=new int[size] ;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n\/\/copy constructor\r\n\r\n&nbsp;\r\n\r\nVector(const Vector &amp;v)\r\n\r\n{\r\n\r\nsize= v.size;\r\n\r\narr= new int[v.size] ; \/\/ allocate seperate memory\r\n\r\nfor(int i=0 ; i&lt;v.size ;i++)\r\n\r\n{\r\n\r\narr[i] = v.arr[i] ;\r\n\r\n}\r\n\r\n}\r\n\r\n~Vector()\r\n\r\n{\r\n\r\ncout&lt;&lt;\u201ddestructor invoked \u201d ;\r\n\r\ndelete [ ] arr ;\r\n\r\n}\r\n\r\n} ;\r\n\r\n&nbsp;\r\n\r\nNow if we create objects :\r\n\r\n&nbsp;\r\n\r\nVector v1 (5) ;\r\n\r\nVector v2=v1;\r\n\r\nHere for creating object <strong><em>v2<\/em><\/strong> , user defined copy constructor is invoked which allocate separate memory to <strong><em>v2.arr<\/em><\/strong> .<img class=\"alignnone size-full wp-image-91\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-26.png\" alt=\"\" width=\"770\" height=\"455\" \/>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<strong>Destructors<\/strong>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In C++ objects are destroyed using Destructor function. Destructors are the counterpart of constructor. Destructor function is automatically invoked by compiler when the object goes out of scope . Just like constructor , destructor function have the same name as the class, preceded by tild (~) .<\/p>\r\n&nbsp;\r\n\r\n<strong>Syntax:<\/strong>\r\n\r\n&nbsp;\r\n\r\n~classname()\r\n\r\n{\r\n\r\n\/\/ Definition of Destructor function\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nSo the Destructor function for Time class can be written as:\r\n\r\n~Time()\r\n\r\n{\r\n\r\ncout&lt;&lt;\u201cobject destroyed\u201d ;\r\n\r\n}\r\n\r\nThe code written in the body of the above destructor is executed when the object of Time goes out of scope.\r\n\r\n&nbsp;\r\n\r\nObjects are destroyed in the reverse order of their construction. The reason is local objects are stored in stack , so the object which is defined last , destroyed first.\r\n\r\n&nbsp;\r\n\r\nThere are certain rules for destructor function:\r\n<ul>\r\n \t<li>- Does not have a return type.<\/li>\r\n \t<li>- Does not accept any arguments, therefore a class can have at most one destructor ; destructor overloading is not possible.<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">If we do not specify any destructor , compiler will generate a destructor that does nothing.\u00a0Compiler will invoke the destructor in the following cases:- An object goes out of scope<\/p>\r\n\r\n<ul>\r\n \t<li>- A dynamically allocated object is explicitly deleted using the delete keyword<\/li>\r\n<\/ul>\r\n<strong>Note: <\/strong>Although constructors and destructors are invoked automatically , calling them explicitly is also possible.\r\n\r\n&nbsp;\r\n\r\nLet\u2019s see the complete example which keep track of the number of Time objects :\r\n\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nprivate:\r\n\r\nint seconds;\r\n\r\nint minutes;\r\n\r\nint hours;\r\n\r\nstatic int count ;\r\n\r\npublic:\r\n\r\nTime() {\r\n\r\ncount++ ;\r\n\r\ncout&lt;&lt; \u201cno. of objects \u201c&lt;&lt; count ;\r\n\r\n}\r\n\r\n\/\/ Destructor for Time class\r\n\r\n&nbsp;\r\n\r\n~Time() {\r\n\r\ncout&lt;&lt; \u201c object destroyed \u201d ;\r\n\r\ncount- - ;\r\n\r\ncout&lt;&lt; \u201cno. of objects \u201c&lt;&lt; count ;\r\n\r\n}\r\n\r\n};\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nTime t1 ;\r\n\r\nTime t2 ;\r\n\r\n} \/\/ object t1 and t2 goes of scope here so destructor is invoked\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the above example the static <strong><em>count<\/em><\/strong> variable keeps track of number of Time objects . The value of this variable is incremented when the object is created ( in the constructor) and when the object is destroyed the value is decremented (in the destructor ) .<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In most of the cases we donot require to write destructor function , the default destructor is good enough. It is useful for the classes which require to:<\/p>\r\n- Write clean up code , for example \u2013 closing a file , releasing some resources occupied by the object.\r\n\r\n-Deallocate the dynamically allocated memory .\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Example: Destructor and Dynamic memory Allocation<\/strong>\r\n\r\n&nbsp;\r\n\r\nLet\u2019s see the example of a class where user defined destructor function is required .\r\n\r\n&nbsp;\r\n\r\nclass Vector\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint size;\r\n\r\nint *arr ;\r\n\r\npublic:\r\n\r\nVector()\r\n\r\n{\r\n\r\nsize=0;\r\n\r\narr=null\r\n\r\n}\r\n\r\nVector(int s)\r\n\r\n{\r\n\r\nsize=s;\r\n\r\narr=new int[size] ;\r\n\r\nfor(int i=0 ; i&lt;size ;i++)\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\narr[i] = 0 ;\r\n\r\n}\r\n\r\n}\r\n\r\nvoid display()\r\n\r\n{\r\n\r\nfor(int i=0;i&lt;size;i++)\r\n\r\n{\r\n\r\ncout&lt;&lt;arr[i] ;\r\n\r\n}\r\n\r\n}\r\n\r\n\/\/Destructor\r\n\r\n~Vector()\r\n\r\n{\r\n\r\ncout&lt;&lt;\u201ddestructor invoked \u201d ;\r\n\r\ndelete [ ] arr ;\r\n\r\n}\r\n\r\n};\r\n\r\nvoidfun()\r\n\r\n{\r\n\r\nVector v(5) ; \/\/ calls the constructor and allocates dynamic memory in variable \u201carr\u201d v.display();\r\n\r\n&nbsp;\r\n\r\n} \/\/ the object v goes out of scope , destructor invoked\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the above example the object \u201cv\u201d goes out of scope at the end of function fun() . The destructor function ~Vector() is invoked which deletes the dynamic memory allocated to \u201carr\u201d .\u00a0In above example destructor function is required to free the dynamically allocated memory. In absence of the destructor the memory occupied by the object will be free when the function ends but thedynamic memory allocated to pointer variable *<strong><em>arr<\/em><\/strong>will remain occupied . Let\u2019s understand this with the help of diagram:<\/p>\r\n&nbsp;\r\n\r\n<strong>Without the user defined destructor function :<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe statement in function fun()\r\n\r\n&nbsp;\r\n\r\nVector v(5) ;\u00a0\u00a0\u00a0\u00a0 will allocate the memory :\r\n<table class=\"aligncenter\" style=\"height: 224px;width: 532px\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 114px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 283px;height: 28px\"><strong>dynamically allocated<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 114px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 283px;height: 28px\"><strong>memory<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 114px;height: 28px\"><strong>Object v<\/strong><\/td>\r\n<td style=\"width: 27px;height: 28px\">0<\/td>\r\n<td style=\"width: 27px;height: 28px\">0<\/td>\r\n<td style=\"width: 27px;height: 28px\">0<\/td>\r\n<td style=\"width: 27px;height: 28px\">0<\/td>\r\n<td style=\"width: 27px;height: 28px\">0<\/td>\r\n<td style=\"height: 28px;width: 283px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 114px;height: 28px\"><strong>size=5<\/strong><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 283px;height: 28px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 114px;height: 28px\">*<strong>arr<\/strong> =<\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 27px;height: 28px\"><\/td>\r\n<td style=\"width: 283px;height: 28px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When function ends then the object is destroyed using compiler provided destructor which destroy the object and release the memory occupied by the object, but it will not release the dynamic memory :<\/p>\r\n<p style=\"text-align: justify\">When function ends then the object is destroyed using compiler provided destructor which destroy the object and release the memory occupied by the object, but it will not release the dynamic memory :<\/p>\r\n<img class=\"alignnone size-full wp-image-92 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-27.png\" alt=\"\" width=\"797\" height=\"606\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-93 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-28.png\" alt=\"\" width=\"883\" height=\"662\" \/>\r\n\r\n<strong>Summary:<\/strong>\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li>In C++ a copy of an object is created using copy constructor .<\/li>\r\n \t<li>In case a class does not define any copy constructor, compiler provides one.<\/li>\r\n \t<li>Copy constructor does a shallow copy of object.<\/li>\r\n \t<li>A class must define a copy constructor if it involves dynamic memory allocation ( for deep copy of object).<\/li>\r\n \t<li>At the time of object destruction the Destructor function is invoked.<\/li>\r\n \t<li>Just like constructor, compiler will also provide a default destructor in case a class does not define any destructor.<\/li>\r\n \t<li>Destructor are invoked implicitly by complier at the time of object creation and destruction respectively.<\/li>\r\n \t<li>Destructor overloading is not possible.<\/li>\r\n \t<li>A class must contain a Destructor function if it involves dynamic memory allocation.<\/li>\r\n<\/ul>\r\n\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Constructor & Destructor<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/ILZ_ghQ5vb0\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<strong>Additional References<\/strong>\r\n\r\n&nbsp;\r\n\r\n1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.\r\n\r\n2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.\r\n\r\n3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.\r\n\r\n4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.\r\n\r\n5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.\r\n\r\n6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill\r\n\r\n7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html\r\n\r\n8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.\r\n\r\n9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education\r\n\r\n10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill\r\n\r\n\u201cC++ FAQs\u201d, Pearson Education\r\n\r\n&nbsp;","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/ILZ_ghQ5vb0\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<p>In the last module we have covered the following topics:<\/p>\n<ul>\n<li>Constructor<\/li>\n<li>Compiler defined Default Constructor<\/li>\n<li>User defined Default Constructor<\/li>\n<li>Parameterized Constructor<\/li>\n<li>Constructor Overloading<\/li>\n<li>Destructors<\/li>\n<\/ul>\n<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We have seen how we can create a parameterized constructor. A parameterized constructor is one which has one or more arguments of any type. But there is one exception to this-<\/p>\n<p>&nbsp;<\/p>\n<p><strong>A single parameterized constructor can have parameter of any type except the object of the same class.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>for example we cannot create the following Constructor in the Time class<\/p>\n<p>&nbsp;<\/p>\n<p>Time(Time t)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ code<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>We may need this constructor if we want to create copy of an object . For example:<\/p>\n<p>Time t1 (10,30,6) ;<\/p>\n<p>Time t2 (t1);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ creating the copy of object t1<\/p>\n<p>Here, to construct object <strong><em>t2<\/em><\/strong> we need the above constructor. But compiler will not allow us to create this constructor.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The reason is very logical: Objects are passed by value in C++ , thus when an object is passed by value , a temporary copy of object is created in the memory. Here, Time object is passed as parameter to constructor thus a temporary Time object is required to be created , to create this temporary Time object the same constructor with Time object as parameter would be invoked and this process would run infinitely. Thus to avoid this situation compiler does not allow to pass a Time object in constructor as parameter.<\/p>\n<p>&nbsp;<\/p>\n<p>If passing the object of the same class is not possible in the constructor as a parameter then how to create copy of an object?<\/p>\n<p>&nbsp;<\/p>\n<p>The answer is \u2013 <strong>Copy Constructor<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Copy Constructor:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A copy constructor is a constructor which has the object reference as a parameter i.e. instead of passing object by value we pass reference of the object. The syntax of copy constructor is:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ClassName ( const ClassName &amp; )<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Here the parameter is reference of object and not the copy of object, therefore the object is passed by address.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>(Note: Here the object reference should be passed as const so that the original object is not accidently modified in the copy constructor. )<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Just like constructor and destructor C++ compiler also provides a copy constructor . Whenever copy of an object is created , this copy constructor is invoked . Compiler provided copy constructor creates a shallow copy of the object i.e. it copies the object bit by bit.<\/p>\n<p>&nbsp;<\/p>\n<p>The below statement will invoke compiler provided copy constructor for creating object <strong>t2<\/strong> which creates the exact copy of object <strong>t1 :<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Time t2(t1) ;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Copy constructor is always invoked in the following cases:<\/strong><\/p>\n<ol>\n<li>An object is initialized with the other object of the same class. For example:<\/li>\n<\/ol>\n<p>Time t1(12,30,30);<\/p>\n<p>Time t2 = t1 ;<\/p>\n<p>Time t3(t1) ;<\/p>\n<p>Here both object t2 and t3 are created using copy constructor<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Note: <\/strong>The statement Time t3(t1) is same as Time t3=t1<\/p>\n<p>Here the object t1 is created by invoking a usual parameterized constructor having 3 arguments.<\/p>\n<p>&nbsp;<\/p>\n<p>But the object t2 and t3 are\u00a0\u00a0 initialized with object t1 , so here copy constructor is invoked.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Note <\/strong>that if we first create an object\u00a0 and then assign another object as:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Time t3 ;<\/strong><\/p>\n<p><strong>t3= t2 ;<\/strong><\/p>\n<p><strong>Then object t3 is created using default constructor and not the copy constructor.<\/strong><\/p>\n<ol start=\"2\">\n<li style=\"text-align: justify\">When an object is passed as parameter to a function and when a function returns an object. In both the cases compiler needs to create a temporary object and this temporary object is created using copy constructor.<\/li>\n<\/ol>\n<p><strong>Example:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>voiddisplayTime(Time t) ;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The parameter is a Time object<\/p>\n<p>&nbsp;<\/p>\n<p>Time calculateTime(int milliseconds ) ;\u00a0\u00a0\u00a0\u00a0 \/\/ Return type is Time object<\/p>\n<p>&nbsp;<\/p>\n<p><strong>User Defined Copy Constructor :<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>User can also define a copy constructor . If user defines a copy constructor then compiler will not provide any copy constructor.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>For Time class the user defined copy constructor can be written as:<\/p>\n<p>&nbsp;<\/p>\n<p>class Time{<\/p>\n<p>\/\/ data members and methods<\/p>\n<p>&nbsp;<\/p>\n<p>Time (const Time &amp; t)<\/p>\n<p>{<\/p>\n<p>seconds = t.seconds ;<\/p>\n<p>minutes = t.minutes ;<\/p>\n<p>hours\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = t.hours ;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<div>\n<p><strong>Need for User Defined Copy Constructor :<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>If bit by bit copy i.e. field by field copy is required then there is no need to define a user defined copy constructor as the compiler defined copy constructor does the same job.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the previous example the Time class actually does not require the user defined copy constructor as the compiler defined copy constructor does the same thing i.e. field by field copy.<\/p>\n<p>&nbsp;<\/p>\n<p>User defined copy constructor is needed when a deep copy is required. Usually it is required if there is dynamic memory allocation in constructor. Consider the following example:<\/p>\n<p>&nbsp;<\/p>\n<p>class Vector<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>int size;<\/p>\n<p>int *arr ;<\/p>\n<p>public:<\/p>\n<\/div>\n<div>\n<p>Vector()<\/p>\n<p>{<\/p>\n<p>\/\/Default Constructor<\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">size=0;<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>arr=null<\/p>\n<p>}<\/p>\n<\/div>\n<p>Vector(int s)<\/p>\n<p>{<\/p>\n<p>size=s;<\/p>\n<p>arr=new int[size] ;<\/p>\n<p>&nbsp;<\/p>\n<p>for(int i=0 ; i&lt;size ;i++)<\/p>\n<p>{<\/p>\n<p>arr[i] = 0 ;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>~Vector()<\/p>\n<p>{<\/p>\n<p>cout&lt;&lt;\u201ddestructor invoked \u201d ;<\/p>\n<p>delete [ ] arr ;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>Vector v1(5);<\/p>\n<p>Vector v2=v1;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p style=\"text-align: justify\">Here object <strong><em>v2<\/em><\/strong> is created using compiler provided copy constructor therefore the data members of object <strong><em>v2<\/em><\/strong> will be the exact copy of data members of object <strong><em>v1<\/em><\/strong>. Thus the pointer variable <strong><em>*arr<\/em><\/strong> will also be copied as it is. The address pointed by <strong><em>v1.arr<\/em><\/strong> is copied to <strong><em>v2.arr<\/em><\/strong> and no separate dynamic memory is allocated to the pointer <strong><em>v2.arr<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Thus <strong><em>v1.arr<\/em><\/strong> and <strong><em>v2.arr<\/em><\/strong> both will point to the same memory area.<\/p>\n<p>&nbsp;<\/p>\n<p>Lets understand this point with the help of a diagram:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-89 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-24.png\" alt=\"\" width=\"702\" height=\"846\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-24.png 702w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-24-249x300.png 249w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-24-65x78.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-24-225x271.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-24-350x422.png 350w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/><\/p>\n<p>Here both object are sharing the same address in <strong><em>arr<\/em><\/strong> which can creates problem in the following cases:<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li>When one object modify this <strong><em>arr<\/em><\/strong> pointer, the changes are reflected in the other object also.<\/li>\n<\/ol>\n<p>Suppose if we write the following statements:<\/p>\n<p>&nbsp;<\/p>\n<p>v1.arr[0] = 10 ;<\/p>\n<p>cout&lt;&lt;v1.arr[0] &lt;&lt; \u201c\\n\u201d;<\/p>\n<p>cout&lt;&lt;v2.arr[0] ;<\/p>\n<p>&nbsp;<\/p>\n<p>output: 10<\/p>\n<p>&nbsp;<\/p>\n<p>10<\/p>\n<p>&nbsp;<\/p>\n<p>This is happening because both <strong><em>v1.arr<\/em><\/strong> and <strong><em>v2.arr<\/em><\/strong> are sharing the same memory and no separate dynamic memory is allocated for <strong><em>v2.arr<\/em><\/strong>.<\/p>\n<ol start=\"2\">\n<li style=\"text-align: justify\">Here if one of the object goes out of scope then the user defined destructor is invoked which will deallocate the dynamic memory while the other object is still pointing to the same memory which is being deallocated by compiler.<\/li>\n<\/ol>\n<p>consider the following example:<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>Time t1(10,20,30) ;<\/p>\n<p>{<\/p>\n<p>Time t2(t1) ;<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>\/\/ some code<\/li>\n<\/ul>\n<p>} \/\/ object t2 goes out of scope<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<div>\n<p>In the code above when object <strong><em>t2<\/em><\/strong> goes out of scope , dynamic memory is deallocated by destructor. But object <strong><em>t1<\/em><\/strong> is still pointing to same memory. See the diagram:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-90\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-25.png\" alt=\"\" width=\"942\" height=\"462\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-25.png 942w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-25-300x147.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-25-768x377.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-25-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-25-225x110.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-25-350x172.png 350w\" sizes=\"auto, (max-width: 942px) 100vw, 942px\" \/><\/p>\n<p>To avoid this kind of behavior, user defined copy constructor is needed. Let\u2019s see how to define a copy constructor in the above example:<\/p>\n<p>&nbsp;<\/p>\n<p>class Vector<\/p>\n<p>{<\/p>\n<p>int size;<\/p>\n<p>int *arr ;<\/p>\n<p>public:<\/p>\n<p>\/\/Default Constructor<\/p>\n<p>Vector()<\/p>\n<p>{<\/p>\n<p>size=0;<\/p>\n<p>arr=null<\/p>\n<p>}<\/p>\n<p>Vector(int s)<\/p>\n<p>{<\/p>\n<p>size=s;<\/p>\n<p>arr=new int[size] ;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/copy constructor<\/p>\n<p>&nbsp;<\/p>\n<p>Vector(const Vector &amp;v)<\/p>\n<p>{<\/p>\n<p>size= v.size;<\/p>\n<p>arr= new int[v.size] ; \/\/ allocate seperate memory<\/p>\n<p>for(int i=0 ; i&lt;v.size ;i++)<\/p>\n<p>{<\/p>\n<p>arr[i] = v.arr[i] ;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>~Vector()<\/p>\n<p>{<\/p>\n<p>cout&lt;&lt;\u201ddestructor invoked \u201d ;<\/p>\n<p>delete [ ] arr ;<\/p>\n<p>}<\/p>\n<p>} ;<\/p>\n<p>&nbsp;<\/p>\n<p>Now if we create objects :<\/p>\n<p>&nbsp;<\/p>\n<p>Vector v1 (5) ;<\/p>\n<p>Vector v2=v1;<\/p>\n<p>Here for creating object <strong><em>v2<\/em><\/strong> , user defined copy constructor is invoked which allocate separate memory to <strong><em>v2.arr<\/em><\/strong> .<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-91\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-26.png\" alt=\"\" width=\"770\" height=\"455\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-26.png 770w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-26-300x177.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-26-768x454.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-26-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-26-225x133.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-26-350x207.png 350w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p><strong>Destructors<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In C++ objects are destroyed using Destructor function. Destructors are the counterpart of constructor. Destructor function is automatically invoked by compiler when the object goes out of scope . Just like constructor , destructor function have the same name as the class, preceded by tild (~) .<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>~classname()<\/p>\n<p>{<\/p>\n<p>\/\/ Definition of Destructor function<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>So the Destructor function for Time class can be written as:<\/p>\n<p>~Time()<\/p>\n<p>{<\/p>\n<p>cout&lt;&lt;\u201cobject destroyed\u201d ;<\/p>\n<p>}<\/p>\n<p>The code written in the body of the above destructor is executed when the object of Time goes out of scope.<\/p>\n<p>&nbsp;<\/p>\n<p>Objects are destroyed in the reverse order of their construction. The reason is local objects are stored in stack , so the object which is defined last , destroyed first.<\/p>\n<p>&nbsp;<\/p>\n<p>There are certain rules for destructor function:<\/p>\n<ul>\n<li>&#8211; Does not have a return type.<\/li>\n<li>&#8211; Does not accept any arguments, therefore a class can have at most one destructor ; destructor overloading is not possible.<\/li>\n<\/ul>\n<p style=\"text-align: justify\">If we do not specify any destructor , compiler will generate a destructor that does nothing.\u00a0Compiler will invoke the destructor in the following cases:- An object goes out of scope<\/p>\n<ul>\n<li>&#8211; A dynamically allocated object is explicitly deleted using the delete keyword<\/li>\n<\/ul>\n<p><strong>Note: <\/strong>Although constructors and destructors are invoked automatically , calling them explicitly is also possible.<\/p>\n<p>&nbsp;<\/p>\n<p>Let\u2019s see the complete example which keep track of the number of Time objects :<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>private:<\/p>\n<p>int seconds;<\/p>\n<p>int minutes;<\/p>\n<p>int hours;<\/p>\n<p>static int count ;<\/p>\n<p>public:<\/p>\n<p>Time() {<\/p>\n<p>count++ ;<\/p>\n<p>cout&lt;&lt; \u201cno. of objects \u201c&lt;&lt; count ;<\/p>\n<p>}<\/p>\n<p>\/\/ Destructor for Time class<\/p>\n<p>&nbsp;<\/p>\n<p>~Time() {<\/p>\n<p>cout&lt;&lt; \u201c object destroyed \u201d ;<\/p>\n<p>count- &#8211; ;<\/p>\n<p>cout&lt;&lt; \u201cno. of objects \u201c&lt;&lt; count ;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>Time t1 ;<\/p>\n<p>Time t2 ;<\/p>\n<p>} \/\/ object t1 and t2 goes of scope here so destructor is invoked<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the above example the static <strong><em>count<\/em><\/strong> variable keeps track of number of Time objects . The value of this variable is incremented when the object is created ( in the constructor) and when the object is destroyed the value is decremented (in the destructor ) .<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In most of the cases we donot require to write destructor function , the default destructor is good enough. It is useful for the classes which require to:<\/p>\n<p>&#8211; Write clean up code , for example \u2013 closing a file , releasing some resources occupied by the object.<\/p>\n<p>-Deallocate the dynamically allocated memory .<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example: Destructor and Dynamic memory Allocation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Let\u2019s see the example of a class where user defined destructor function is required .<\/p>\n<p>&nbsp;<\/p>\n<p>class Vector<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int size;<\/p>\n<p>int *arr ;<\/p>\n<p>public:<\/p>\n<p>Vector()<\/p>\n<p>{<\/p>\n<p>size=0;<\/p>\n<p>arr=null<\/p>\n<p>}<\/p>\n<p>Vector(int s)<\/p>\n<p>{<\/p>\n<p>size=s;<\/p>\n<p>arr=new int[size] ;<\/p>\n<p>for(int i=0 ; i&lt;size ;i++)<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>arr[i] = 0 ;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>void display()<\/p>\n<p>{<\/p>\n<p>for(int i=0;i&lt;size;i++)<\/p>\n<p>{<\/p>\n<p>cout&lt;&lt;arr[i] ;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/Destructor<\/p>\n<p>~Vector()<\/p>\n<p>{<\/p>\n<p>cout&lt;&lt;\u201ddestructor invoked \u201d ;<\/p>\n<p>delete [ ] arr ;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>voidfun()<\/p>\n<p>{<\/p>\n<p>Vector v(5) ; \/\/ calls the constructor and allocates dynamic memory in variable \u201carr\u201d v.display();<\/p>\n<p>&nbsp;<\/p>\n<p>} \/\/ the object v goes out of scope , destructor invoked<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the above example the object \u201cv\u201d goes out of scope at the end of function fun() . The destructor function ~Vector() is invoked which deletes the dynamic memory allocated to \u201carr\u201d .\u00a0In above example destructor function is required to free the dynamically allocated memory. In absence of the destructor the memory occupied by the object will be free when the function ends but thedynamic memory allocated to pointer variable *<strong><em>arr<\/em><\/strong>will remain occupied . Let\u2019s understand this with the help of diagram:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Without the user defined destructor function :<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The statement in function fun()<\/p>\n<p>&nbsp;<\/p>\n<p>Vector v(5) ;\u00a0\u00a0\u00a0\u00a0 will allocate the memory :<\/p>\n<table class=\"aligncenter\" style=\"height: 224px;width: 532px\">\n<tbody>\n<tr style=\"height: 28px\">\n<td style=\"width: 114px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 283px;height: 28px\"><strong>dynamically allocated<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 114px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 283px;height: 28px\"><strong>memory<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 114px;height: 28px\"><strong>Object v<\/strong><\/td>\n<td style=\"width: 27px;height: 28px\">0<\/td>\n<td style=\"width: 27px;height: 28px\">0<\/td>\n<td style=\"width: 27px;height: 28px\">0<\/td>\n<td style=\"width: 27px;height: 28px\">0<\/td>\n<td style=\"width: 27px;height: 28px\">0<\/td>\n<td style=\"height: 28px;width: 283px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 114px;height: 28px\"><strong>size=5<\/strong><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 283px;height: 28px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 114px;height: 28px\">*<strong>arr<\/strong> =<\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 27px;height: 28px\"><\/td>\n<td style=\"width: 283px;height: 28px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When function ends then the object is destroyed using compiler provided destructor which destroy the object and release the memory occupied by the object, but it will not release the dynamic memory :<\/p>\n<p style=\"text-align: justify\">When function ends then the object is destroyed using compiler provided destructor which destroy the object and release the memory occupied by the object, but it will not release the dynamic memory :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-92 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-27.png\" alt=\"\" width=\"797\" height=\"606\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-27.png 797w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-27-300x228.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-27-768x584.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-27-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-27-225x171.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-27-350x266.png 350w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-93 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-28.png\" alt=\"\" width=\"883\" height=\"662\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-28.png 883w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-28-300x225.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-28-768x576.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-28-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-28-225x169.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-28-350x262.png 350w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/p>\n<p><strong>Summary:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>In C++ a copy of an object is created using copy constructor .<\/li>\n<li>In case a class does not define any copy constructor, compiler provides one.<\/li>\n<li>Copy constructor does a shallow copy of object.<\/li>\n<li>A class must define a copy constructor if it involves dynamic memory allocation ( for deep copy of object).<\/li>\n<li>At the time of object destruction the Destructor function is invoked.<\/li>\n<li>Just like constructor, compiler will also provide a default destructor in case a class does not define any destructor.<\/li>\n<li>Destructor are invoked implicitly by complier at the time of object creation and destruction respectively.<\/li>\n<li>Destructor overloading is not possible.<\/li>\n<li>A class must contain a Destructor function if it involves dynamic memory allocation.<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Constructor &#38; Destructor<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/ILZ_ghQ5vb0\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Additional References<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.<\/p>\n<p>2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.<\/p>\n<p>3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.<\/p>\n<p>4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.<\/p>\n<p>5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.<\/p>\n<p>6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill<\/p>\n<p>7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html<\/p>\n<p>8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.<\/p>\n<p>9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education<\/p>\n<p>10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill<\/p>\n<p>\u201cC++ FAQs\u201d, Pearson Education<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":10,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-jyotika-doshi"],"pb_section_license":""},"chapter-type":[],"contributor":[60],"license":[],"class_list":["post-88","chapter","type-chapter","status-publish","hentry","contributor-dr-jyotika-doshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/88\/revisions"}],"predecessor-version":[{"id":397,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/88\/revisions\/397"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/88\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=88"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=88"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}