{"id":111,"date":"2018-07-10T06:27:41","date_gmt":"2018-07-10T06:27:41","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=111"},"modified":"2019-05-14T12:01:23","modified_gmt":"2019-05-14T12:01:23","slug":"the-important-trio","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/the-important-trio\/","title":{"rendered":"The Important Trio"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/YyEVDfMhLb4\" 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 this module we will discuss about important trio namely\r\n<ol>\r\n \t<li>User Defined Destructor<\/li>\r\n \t<li>User Defined Copy Constructor<\/li>\r\n \t<li>User Defined Overloaded = Operator<\/li>\r\n<\/ol>\r\nBut before we understand the need of this important trio, let us recall where these functions are used one by one.\r\n\r\n&nbsp;\r\n\r\n<strong>Destructors<\/strong>\r\n\r\n&nbsp;\r\n\r\nDestructors are called automatically when object goes out of scope.\r\n\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\nMatrix resultant(row,col);\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\n&nbsp;\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As we know the scope of the object is the block in which it is declared and it\u2019s lifetime is the duration for which the block is executing. The Matrix object resultant is declared inside the operator+ function therefore when execution of the function ends the object will go out of scope and destructor will be called for its destruction. Also the formal argument m, which is object of class Matrix will come into existence when control enters function operator+ and will be destroyed once the function ends, by means of invocation of destructor.<\/p>\r\n&nbsp;\r\n\r\n<strong>Copy Constructor<\/strong>\r\n\r\n&nbsp;\r\n\r\nCopy Constructor is called:\r\n<ol>\r\n \t<li>When we make copy of an object. Time t1(2,30,45);<\/li>\r\n<\/ol>\r\nTime t2(t1);\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here object t1 is constructed with the help of parameterized constructor initializing its data members namely hrs, minutes and seconds with values 2, 30 and 45 respectively. Object t2 is created as a copy of t1 and its construction is done by copy constructor.<\/p>\r\n\r\n<ol start=\"2\">\r\n \t<li>When we pass an object as an argument by value to a method.<\/li>\r\n<\/ol>\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nMatrix resultant(row,col); for (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;\r\n\r\n}\r\n\r\nObjects in C++ are passed by value Here object m will be created as a copy of actual argument and will be constructed by copy constructor.\r\n\r\n&nbsp;\r\n<ol start=\"3\">\r\n \t<li>When we return an object from a method by value.<\/li>\r\n<\/ol>\r\n&nbsp;\r\n\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\nMatrix resultant(row,col);\r\n\r\n&nbsp;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\n&nbsp;\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nAs we know when value is returned from function its temporary copy is created before the returning object goes out of scope and destroyed. This temporary copy is created as copy of returning object using copy constructor.\r\n\r\n&nbsp;\r\n\r\n<strong>Overloaded Assignment Operator<\/strong>\r\n\r\n&nbsp;\r\n\r\nOverloaded Assignment Operator is called when one object of the class is assigned to another object of the same class.\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nMatrix m1(2,2),m2(2,2), result(2,2);\r\n\r\nm1.readMatrix();\r\n\r\nm2.readMatrix();\r\n\r\n&nbsp;\r\n\r\nresult = m1+m2;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"The resultant matrix is \" &lt;&lt; endl;\r\n\r\nresult.displayMatrix();\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here in the program the matrix object holding sum of matrix objects m1 and m2 is returned and assigned to matrix object result by means of overloaded assignment operator.<\/p>\r\n&nbsp;\r\n\r\nLet us define the complete Matrix class to understand this.\r\n\r\n&nbsp;\r\n\r\nClass Matrix\r\n\r\n\/* Program : C++ program to add two matrices*\/\r\n\r\n#include&lt;iostream&gt;\r\n\r\n#include&lt;cmath&gt;\r\n\r\nusing namespace std;\r\n\r\nclass Matrix\r\n\r\n{\r\n\r\nint row,col;\r\n\r\nint matrix[10][10];\r\n\r\npublic :\r\n\r\nMatrix()\r\n\r\n{ row = 0; col = 0; } Matrix(int r, int c)\r\n\r\n{\r\n\r\nrow = r; col = c;\r\n\r\nfor (int i=0; i&lt;row; i++) for (int j=0; j&lt;col; j++)\r\n\r\nmatrix[i][j] =0;\r\n\r\n}\r\n\r\nvoid readMatrix()\r\n\r\n{\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"Enter the matrix\" &lt;&lt; endl;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncin &gt;&gt; matrix[i][j];\r\n\r\n}\r\n\r\nvoid displayMatrix()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n{\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncout &lt;&lt; matrix[i][j] &lt;&lt; \" \";\r\n\r\ncout &lt;&lt; endl;\r\n\r\n}\r\n\r\n}\r\n\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\nMatrix resultant(row,col);\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j];\r\n\r\nreturn resultant;\r\n\r\n}\r\n\r\n}; \/\/ end of class\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nMatrix m1(2,2),m2(2,2), result(2,2);\r\n\r\nm1.readMatrix();\r\n\r\n&nbsp;\r\n\r\nm2.readMatrix();\r\n\r\n&nbsp;\r\n\r\nresult = m1+m2;\r\n\r\n&nbsp;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"The resultant matrix is \" &lt;&lt; endl;\r\n\r\nresult.displayMatrix();\r\n\r\n}<img class=\"size-full wp-image-112 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-38.png\" alt=\"\" width=\"367\" height=\"393\" \/>\r\n\r\nIn this program we have not defined destructor or copy constructor or overloaded assignment operator. Then\r\n<ul>\r\n \t<li>How objects are destroyed when objects go out of scope ?<\/li>\r\n \t<li>How copy of objects is created when objects are passed as value in functions or when objects are returned from the function?<\/li>\r\n \t<li>How objects are assigned to object?<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0Well all this is achieved by means of default implementation of destructor, copy constructor and overloaded assignment operator provided by compiler. Now the question is when we have compiler supplied Destructor, Copy constructor and overloaded assignment operator, what is the need of defining them ourselves?<\/p>\r\n&nbsp;\r\n\r\n<strong>Need of user defined Important Trio<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Compiler supplied Destructor, Copy constructor and overloaded assignment operator are good enough as long as the memory of data members are not constructed dynamically. But if we create a class which constructs memory for some of its data members dynamically, it becomes important\/necessary to define all the three ourselves. Let us understand it with the help of matrix class constructed dynamically.<\/p>\r\n&nbsp;\r\n\r\n<strong>Matrix Class with Dynamic Construction<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/* Program : C++ program to add two matrices*\/\r\n\r\n&nbsp;\r\n\r\n#include&lt;iostream&gt;\r\n\r\n#include&lt;cmath&gt;\r\n\r\nusing namespace std;\r\n\r\nclass Matrix\r\n\r\n{\r\n\r\nint row,col;\r\n\r\nint **matrix;\r\n\r\npublic :\r\n\r\nMatrix(int r, int c)\r\n\r\n{\r\n\r\nrow = r;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 col = c;\r\n\r\nmatrix = new int *[row];\r\n\r\nfor (int i=0; i&lt;col; i++)\r\n\r\nmatrix[i] = new int[col];\r\n\r\n}\r\n\r\nvoid readMatrix()\r\n\r\n{\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"Enter the matrix\" &lt;&lt; endl;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncin &gt;&gt; matrix[i][j];\r\n\r\n}\r\n\r\nvoid displayMatrix()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n{\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncout &lt;&lt; matrix[i][j] &lt;&lt; \" \";\r\n\r\ncout &lt;&lt; endl;\r\n\r\n}\r\n\r\n}\r\n\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\nMatrix resultant(row,col);\r\n\r\n&nbsp;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;\r\n\r\n}\r\n\r\n}; \/\/ End of Class\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nint r,c;\r\n\r\ncout &lt;&lt; \"Enter the no of rows of input Matrices : \"; cin &gt;&gt; r;\r\n\r\ncout &lt;&lt; \"Enter the no of columns of input Matrices : \"; cin &gt;&gt; c;\r\n\r\nMatrix m1(r,c),m2(r,c), result(r,c);\r\n\r\nm1.readMatrix();\r\n\r\nm2.readMatrix();\r\n\r\nresult = m1+m2;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"The resultant matrix is \" &lt;&lt; endl;\r\n\r\nresult.displayMatrix();\r\n\r\n}\r\n\r\nThis program will also produce the same result as that of the program with matrix class without dynamic construction of data member.\r\n\r\n<img class=\"size-full wp-image-113 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-39.png\" alt=\"\" width=\"418\" height=\"321\" \/>\r\n<p style=\"text-align: justify\">But the advantage of dynamic construction of matrix is that we can get only that much memory allocated as much as is needed. In earlier program no matter how small or big matrix we wish to store in object of Matrix class, the memory for 10 by 10 matrix will be allocated to the object. If we wish to store 2 by 2 matrix, we will be requiring 8 bytes but 100 bytes will be allocated for in the object for the matrix. And if we wish to store 15 by 15 matrix, we will not be able to do so as the dimension of array for storing the matrix is 10 by 10. In above program, with dynamic memory allocation for data member matrix, we can get only that much memory allocated as much is needed, removing memory wastage. But this will lead to other kind of memory leak. Let us understand this with following figure<\/p>\r\n&nbsp;\r\n\r\nMatrix m;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-114 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-40.png\" alt=\"\" width=\"897\" height=\"449\" \/>\r\n<p style=\"text-align: justify\">When we declare object m, 6 bytes (assuming integers and pointers occupy 2 bytes in our machine) will be allocate to m. The memory allocated for data member matrix dynamically, will be allocated outside the object in heap.<img class=\"size-full wp-image-115 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-41.png\" alt=\"\" width=\"916\" height=\"401\" \/><\/p>\r\n<p style=\"text-align: justify\">When the object goes out of scope memory for object will be released by default destructor, but memory allocated in heap will not be released, generating garbage<\/p>\r\n<img class=\"size-full wp-image-117 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43.png\" alt=\"\" width=\"1171\" height=\"476\" \/>\r\n<p style=\"text-align: justify\">This problem can be overcome by creating destructor which releases dynamically constructed memory in heap before destroying the object. We can define destructor in our matrix class as follows<\/p>\r\n&nbsp;\r\n\r\n~Matrix()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\ndelete matrix[i];\r\n\r\ndelete matrix;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \u201cI am Destructor\" &lt;&lt; endl;\r\n\r\n}\r\n<p style=\"text-align: justify\">Defining this destructor in our matrix class will solve the problem of garbage , as when object goes out of scope this destructor will be called which will release the dynamically created memory in heap before destroying the object.<\/p>\r\n<img class=\"size-full wp-image-118 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-44.png\" alt=\"\" width=\"797\" height=\"315\" \/>\r\n\r\n&nbsp;\r\n\r\nFollowing is the program to add two matrices with dynamic constructor and user defined destructor.\r\n\r\n&nbsp;\r\n\r\n\/* Program : C++ program to add two matrices with dynamic constructor and user defined destructor*\/\r\n\r\n&nbsp;\r\n\r\n#include&lt;iostream&gt;\r\n\r\n#include&lt;cmath&gt;\r\n\r\nusing namespace std;\r\n\r\nclass Matrix\r\n\r\n{\r\n\r\nint row,col;\r\n\r\nint **matrix;\r\n\r\n&nbsp;\r\n\r\npublic :\r\n\r\nMatrix(int r, int c)\r\n\r\n{\r\n\r\nrow = r;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 col = c;\r\n\r\nmatrix = new int *[row];\r\n\r\n&nbsp;\r\n\r\nfor (int i=0; i&lt;col; i++)\r\n\r\nmatrix[i] = new int[col];\r\n\r\n}\r\n\r\nvoid readMatrix()\r\n\r\n{\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"Enter the matrix\" &lt;&lt; endl;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncin &gt;&gt; matrix[i][j];\r\n\r\n}\r\n\r\nvoid displayMatrix()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n{\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncout &lt;&lt; matrix[i][j] &lt;&lt; \" \";\r\n\r\ncout &lt;&lt; endl;\r\n\r\n}\r\n\r\n}\r\n\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\nMatrix resultant(row,col);\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;\r\n\r\n}\r\n\r\n~Matrix()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\ndelete matrix[i];\r\n\r\ndelete matrix;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \u201cI am Destructor\" &lt;&lt; endl;\r\n\r\n}\r\n\r\n}; \/\/ End of Class\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nint r,c;\r\n\r\ncout &lt;&lt; \"Enter the no of rows of input Matrices : \"; cin &gt;&gt; r;\r\n\r\ncout &lt;&lt; \"Enter the no of columns of input Matrices : \"; cin &gt;&gt; c;\r\n\r\nMatrix m1(r,c),m2(r,c), result(r,c);\r\n\r\nm1.readMatrix();\r\n\r\nm2.readMatrix();\r\n\r\nresult = m1+m2;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"The resultant matrix is \" &lt;&lt; endl;\r\n\r\nresult.displayMatrix();\r\n\r\n}\r\n\r\n<img class=\"alignnone size-full wp-image-119\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-45.png\" alt=\"\" width=\"921\" height=\"676\" \/><img class=\"size-full wp-image-120 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-46.png\" alt=\"\" width=\"533\" height=\"256\" \/>\r\n<p style=\"text-align: justify\">Here destructor will release dynamically constructed memory in heap before destroying the object. But as shown above, defining appropriate destructor solves the problem of memory leak, but the above code results in runtime error - access violation while reading some memory. What is the cause of this error? Let us try to understand the same.<\/p>\r\n&nbsp;\r\n\r\nWell the cause of the error is the compiler provided copy constructor. Let us understand it with the help of an example.\r\n\r\n&nbsp;\r\n\r\nHave a look at the following declaration\r\n\r\nMatrix m;\r\n\r\nMatrix m2(m1);\r\n<p style=\"text-align: justify\">This will create object m with default constructor, and m2 will be constructed from m1. Since we have not defined copy constructor, m2 will be constructed with compiler provided copy constructor. Compiler provided constructor performs shallow copy, which will creates copy of object byte by byte, therefore copy of object is made but copy of memory allocated for matrix in heap will not be made. Only the copy of address stored in the object will be made, which will cause both m1 and m2 to point to same memory in heap.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-121 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-47.png\" alt=\"\" width=\"755\" height=\"739\" \/>\r\n<p style=\"text-align: justify\">In our program the error arises when compiler provided copy constructor creates shallow copy of function argument and shallow copy of temporary copy of return object.<\/p>\r\n&nbsp;\r\n\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\nMatrix resultant(row,col);\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;\r\n\r\n}\r\n\r\nWhen these two objects go out of scope they will destroy the memory allocated in heap for matrix, which is also being pointed by actual argument and the object receiving the returned object respectively.\r\n\r\nSo what is the solution for this problem?\r\n\r\nWell to solve this problem we can define our own copy constructor, which does deep copy as follows\r\n\r\nMatrix(Matrix &amp;m)\r\n\r\n{\r\n\r\nrow = m.row;\r\n\r\ncol = m.col;\r\n\r\nmatrix = new int *[row];\r\n\r\nfor (int i=0; i&lt;col; i++)\r\n\r\nmatrix[i] = new int[col];\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n{\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nmatrix[i][j] = m.matrix[i][j];\r\n\r\n}\r\n\r\n}\r\n\r\nMatrix Class with Dynamic Construction , Destructor and Copy Constructor\r\n\r\n\/* Program : C++ program to add two matrices*\/\r\n\r\n#include&lt;iostream&gt;\r\n\r\n#include&lt;cmath&gt;\r\n\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nclass Matrix\r\n\r\n{\r\n\r\nint row,col;\r\n\r\nint **matrix;\r\n\r\npublic :\r\n\r\nMatrix(int r, int c)\r\n\r\n{\r\n\r\nrow = r;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 col = c;\r\n\r\nmatrix = new int *[row];\r\n\r\nfor (int i=0; i&lt;col; i++)\r\n\r\nmatrix[i] = new int[col];\r\n\r\n}\r\n\r\nvoid readMatrix()\r\n\r\n{\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"Enter the matrix\" &lt;&lt; endl;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncin &gt;&gt; matrix[i][j];\r\n\r\n}\r\n\r\nvoid displayMatrix()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n{\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncout &lt;&lt; matrix[i][j] &lt;&lt; \" \";\r\n\r\ncout &lt;&lt; endl;\r\n\r\n}\r\n\r\n}\r\n\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\nMatrix resultant(row,col);\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;\r\n\r\n}\r\n\r\n~Matrix()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\ndelete matrix[i];\r\n\r\ndelete matrix;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \u201cI am Destructor\" &lt;&lt; endl;\r\n\r\n}\r\n\r\nMatrix(Matrix &amp;m)\r\n\r\n{\r\n\r\nrow = m.row;\r\n\r\ncol = m.col;\r\n\r\nmatrix = new int *[row];\r\n\r\nfor (int i=0; i&lt;col; i++)\r\n\r\nmatrix[i] = new int[col];\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n{\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nmatrix[i][j] = m.matrix[i][j];\r\n\r\n}\r\n\r\n}\r\n\r\n}; \/\/ End of Class\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nint r,c;\r\n\r\n&nbsp;\r\n\r\ncout &lt;&lt; \"Enter the no of rows of input Matrices : \"; cin &gt;&gt; r;\r\n\r\ncout &lt;&lt; \"Enter the no of columns of input Matrices : \"; cin &gt;&gt; c;\r\n\r\nMatrix m1(r,c),m2(r,c), result(r,c);\r\n\r\nm1.readMatrix();\r\n\r\nm2.readMatrix();\r\n\r\nresult = m1+m2;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"The resultant matrix is \" &lt;&lt; endl;\r\n\r\nresult.displayMatrix();\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-122\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48.png\" alt=\"\" width=\"1354\" height=\"1220\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Well this time it is default implementation of overloaded = operator function provided by the compiler. If we do not overload = operator in our class, compiler provides overloaded = operator. The default implementation of overloaded = operator provided by compiler again does shallow copy. So how to overcome this problem. To overcome this problem we can define our overloaded assignment operator which does deep copy as follows<\/p>\r\n&nbsp;\r\n\r\nvoid operator= (Matrix m)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nrow = m.row;\r\n\r\ncol = m.col;\r\n\r\n&nbsp;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n&nbsp;\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nmatrix[i][j] = m.matrix[i][j];\r\n\r\n}\r\n<p style=\"text-align: justify\">Now in our matrix class we have defined important trio namely destructor, user defined copy constructor amd user defined overloed =operator function, our program will work fine.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Matrix Class with Dynamic Construction, Derstructor, Copy Constructor and Overloaded = Operator<\/p>\r\n&nbsp;\r\n\r\n\/* Program : C++ program to add two matrices*\/\r\n\r\n#include&lt;iostream&gt;\r\n\r\n#include&lt;cmath&gt;\r\n\r\n&nbsp;\r\n\r\nusing namespace std;\r\n\r\nclass Matrix\r\n\r\n{\r\n\r\nint row,col;\r\n\r\nint **matrix;\r\n\r\n&nbsp;\r\n\r\npublic :\r\n\r\nMatrix(int r, int c)\r\n\r\n{\r\n\r\nrow = r;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 col = c;\r\n\r\nmatrix = new int *[row];\r\n\r\nfor (int i=0; i&lt;col; i++)\r\n\r\nmatrix[i] = new int[col];\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\nvoid readMatrix()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"Enter the matrix\" &lt;&lt; endl;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n&nbsp;\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncin &gt;&gt; matrix[i][j];\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nvoid displayMatrix()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n{\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\ncout &lt;&lt; matrix[i][j] &lt;&lt; \" \";\r\n\r\n&nbsp;\r\n\r\ncout &lt;&lt; endl;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\nMatrix operator+ (Matrix m)\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nMatrix resultant(row,col);\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nresultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j];\r\n\r\nreturn resultant;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n~Matrix()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\ndelete matrix[i];\r\n\r\ndelete matrix;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \u201cI am Destructor\" &lt;&lt; endl;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nMatrix(Matrix &amp;m)\r\n\r\n{\r\n\r\nrow = m.row;\r\n\r\n&nbsp;\r\n\r\ncol = m.col;\r\n\r\nmatrix = new int *[row];\r\n\r\nfor (int i=0; i&lt;col; i++)\r\n\r\nmatrix[i] = new int[col];\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nmatrix[i][j] = m.matrix[i][j];\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nvoid operator= (Matrix m)\r\n\r\n{\r\n\r\nrow = m.row;\r\n\r\ncol = m.col;\r\n\r\n&nbsp;\r\n\r\nfor (int i=0; i&lt;row; i++)\r\n\r\nfor (int j=0; j&lt;col; j++)\r\n\r\nmatrix[i][j] = m.matrix[i][j];\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n}; \/\/ End of Class\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint r,c;\r\n\r\n&nbsp;\r\n\r\ncout &lt;&lt; \"Enter the no of rows of input Matrices : \"; cin &gt;&gt; r;\r\n\r\n&nbsp;\r\n\r\ncout &lt;&lt; \"Enter the no of columns of input Matrices : \"; cin &gt;&gt; c;\r\n\r\nMatrix m1(r,c),m2(r,c), result(r,c);\r\n\r\n&nbsp;\r\n\r\nm1.readMatrix();\r\n\r\nm2.readMatrix();\r\n\r\n&nbsp;\r\n\r\nresult = m1+m2;\r\n\r\ncout &lt;&lt; endl &lt;&lt; \"The resultant matrix is \" &lt;&lt; endl;\r\n\r\n&nbsp;\r\n\r\nresult.displayMatrix();\r\n\r\n}<img class=\"size-full wp-image-123 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-49.png\" alt=\"\" width=\"611\" height=\"486\" \/>\r\n<p style=\"text-align: justify\">Well, appropriate implementation of important trio in our class makes our program work !!!<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Compiler supplied Destructor, Copy constructor and overloaded assignment operator are good enough as long as the memory of data members are not constructed dynamically. But if we create a class which constructs memory for some of its data members dynamically, it becomes important\/necessary to define this trio ourselves appropriately in our class.<\/p>\r\n&nbsp;\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on The Important Trio<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/YyEVDfMhLb4\" 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\n11) \u201cC++ FAQs\u201d, Pearson Education.\r\n\r\n&nbsp;","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/YyEVDfMhLb4\" 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 this module we will discuss about important trio namely<\/p>\n<ol>\n<li>User Defined Destructor<\/li>\n<li>User Defined Copy Constructor<\/li>\n<li>User Defined Overloaded = Operator<\/li>\n<\/ol>\n<p>But before we understand the need of this important trio, let us recall where these functions are used one by one.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Destructors<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Destructors are called automatically when object goes out of scope.<\/p>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>Matrix resultant(row,col);<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>&nbsp;<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As we know the scope of the object is the block in which it is declared and it\u2019s lifetime is the duration for which the block is executing. The Matrix object resultant is declared inside the operator+ function therefore when execution of the function ends the object will go out of scope and destructor will be called for its destruction. Also the formal argument m, which is object of class Matrix will come into existence when control enters function operator+ and will be destroyed once the function ends, by means of invocation of destructor.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Copy Constructor<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Copy Constructor is called:<\/p>\n<ol>\n<li>When we make copy of an object. Time t1(2,30,45);<\/li>\n<\/ol>\n<p>Time t2(t1);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here object t1 is constructed with the help of parameterized constructor initializing its data members namely hrs, minutes and seconds with values 2, 30 and 45 respectively. Object t2 is created as a copy of t1 and its construction is done by copy constructor.<\/p>\n<ol start=\"2\">\n<li>When we pass an object as an argument by value to a method.<\/li>\n<\/ol>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>Matrix resultant(row,col); for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;<\/p>\n<p>}<\/p>\n<p>Objects in C++ are passed by value Here object m will be created as a copy of actual argument and will be constructed by copy constructor.<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"3\">\n<li>When we return an object from a method by value.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>Matrix resultant(row,col);<\/p>\n<p>&nbsp;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>&nbsp;<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>As we know when value is returned from function its temporary copy is created before the returning object goes out of scope and destroyed. This temporary copy is created as copy of returning object using copy constructor.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Overloaded Assignment Operator<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Overloaded Assignment Operator is called when one object of the class is assigned to another object of the same class.<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>Matrix m1(2,2),m2(2,2), result(2,2);<\/p>\n<p>m1.readMatrix();<\/p>\n<p>m2.readMatrix();<\/p>\n<p>&nbsp;<\/p>\n<p>result = m1+m2;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;The resultant matrix is &#8221; &lt;&lt; endl;<\/p>\n<p>result.displayMatrix();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here in the program the matrix object holding sum of matrix objects m1 and m2 is returned and assigned to matrix object result by means of overloaded assignment operator.<\/p>\n<p>&nbsp;<\/p>\n<p>Let us define the complete Matrix class to understand this.<\/p>\n<p>&nbsp;<\/p>\n<p>Class Matrix<\/p>\n<p>\/* Program : C++ program to add two matrices*\/<\/p>\n<p>#include&lt;iostream&gt;<\/p>\n<p>#include&lt;cmath&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>class Matrix<\/p>\n<p>{<\/p>\n<p>int row,col;<\/p>\n<p>int matrix[10][10];<\/p>\n<p>public :<\/p>\n<p>Matrix()<\/p>\n<p>{ row = 0; col = 0; } Matrix(int r, int c)<\/p>\n<p>{<\/p>\n<p>row = r; col = c;<\/p>\n<p>for (int i=0; i&lt;row; i++) for (int j=0; j&lt;col; j++)<\/p>\n<p>matrix[i][j] =0;<\/p>\n<p>}<\/p>\n<p>void readMatrix()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;Enter the matrix&#8221; &lt;&lt; endl;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cin &gt;&gt; matrix[i][j];<\/p>\n<p>}<\/p>\n<p>void displayMatrix()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>{<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cout &lt;&lt; matrix[i][j] &lt;&lt; &#8221; &#8220;;<\/p>\n<p>cout &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>Matrix resultant(row,col);<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j];<\/p>\n<p>return resultant;<\/p>\n<p>}<\/p>\n<p>}; \/\/ end of class<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>Matrix m1(2,2),m2(2,2), result(2,2);<\/p>\n<p>m1.readMatrix();<\/p>\n<p>&nbsp;<\/p>\n<p>m2.readMatrix();<\/p>\n<p>&nbsp;<\/p>\n<p>result = m1+m2;<\/p>\n<p>&nbsp;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;The resultant matrix is &#8221; &lt;&lt; endl;<\/p>\n<p>result.displayMatrix();<\/p>\n<p>}<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-112 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-38.png\" alt=\"\" width=\"367\" height=\"393\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-38.png 367w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-38-280x300.png 280w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-38-65x70.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-38-225x241.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-38-350x375.png 350w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/p>\n<p>In this program we have not defined destructor or copy constructor or overloaded assignment operator. Then<\/p>\n<ul>\n<li>How objects are destroyed when objects go out of scope ?<\/li>\n<li>How copy of objects is created when objects are passed as value in functions or when objects are returned from the function?<\/li>\n<li>How objects are assigned to object?<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0Well all this is achieved by means of default implementation of destructor, copy constructor and overloaded assignment operator provided by compiler. Now the question is when we have compiler supplied Destructor, Copy constructor and overloaded assignment operator, what is the need of defining them ourselves?<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Need of user defined Important Trio<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Compiler supplied Destructor, Copy constructor and overloaded assignment operator are good enough as long as the memory of data members are not constructed dynamically. But if we create a class which constructs memory for some of its data members dynamically, it becomes important\/necessary to define all the three ourselves. Let us understand it with the help of matrix class constructed dynamically.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Matrix Class with Dynamic Construction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/* Program : C++ program to add two matrices*\/<\/p>\n<p>&nbsp;<\/p>\n<p>#include&lt;iostream&gt;<\/p>\n<p>#include&lt;cmath&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>class Matrix<\/p>\n<p>{<\/p>\n<p>int row,col;<\/p>\n<p>int **matrix;<\/p>\n<p>public :<\/p>\n<p>Matrix(int r, int c)<\/p>\n<p>{<\/p>\n<p>row = r;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 col = c;<\/p>\n<p>matrix = new int *[row];<\/p>\n<p>for (int i=0; i&lt;col; i++)<\/p>\n<p>matrix[i] = new int[col];<\/p>\n<p>}<\/p>\n<p>void readMatrix()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;Enter the matrix&#8221; &lt;&lt; endl;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cin &gt;&gt; matrix[i][j];<\/p>\n<p>}<\/p>\n<p>void displayMatrix()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>{<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cout &lt;&lt; matrix[i][j] &lt;&lt; &#8221; &#8220;;<\/p>\n<p>cout &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>Matrix resultant(row,col);<\/p>\n<p>&nbsp;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;<\/p>\n<p>}<\/p>\n<p>}; \/\/ End of Class<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>int r,c;<\/p>\n<p>cout &lt;&lt; &#8220;Enter the no of rows of input Matrices : &#8220;; cin &gt;&gt; r;<\/p>\n<p>cout &lt;&lt; &#8220;Enter the no of columns of input Matrices : &#8220;; cin &gt;&gt; c;<\/p>\n<p>Matrix m1(r,c),m2(r,c), result(r,c);<\/p>\n<p>m1.readMatrix();<\/p>\n<p>m2.readMatrix();<\/p>\n<p>result = m1+m2;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;The resultant matrix is &#8221; &lt;&lt; endl;<\/p>\n<p>result.displayMatrix();<\/p>\n<p>}<\/p>\n<p>This program will also produce the same result as that of the program with matrix class without dynamic construction of data member.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-113 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-39.png\" alt=\"\" width=\"418\" height=\"321\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-39.png 418w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-39-300x230.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-39-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-39-225x173.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-39-350x269.png 350w\" sizes=\"auto, (max-width: 418px) 100vw, 418px\" \/><\/p>\n<p style=\"text-align: justify\">But the advantage of dynamic construction of matrix is that we can get only that much memory allocated as much as is needed. In earlier program no matter how small or big matrix we wish to store in object of Matrix class, the memory for 10 by 10 matrix will be allocated to the object. If we wish to store 2 by 2 matrix, we will be requiring 8 bytes but 100 bytes will be allocated for in the object for the matrix. And if we wish to store 15 by 15 matrix, we will not be able to do so as the dimension of array for storing the matrix is 10 by 10. In above program, with dynamic memory allocation for data member matrix, we can get only that much memory allocated as much is needed, removing memory wastage. But this will lead to other kind of memory leak. Let us understand this with following figure<\/p>\n<p>&nbsp;<\/p>\n<p>Matrix m;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-114 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-40.png\" alt=\"\" width=\"897\" height=\"449\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-40.png 897w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-40-300x150.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-40-768x384.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-40-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-40-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-40-350x175.png 350w\" sizes=\"auto, (max-width: 897px) 100vw, 897px\" \/><\/p>\n<p style=\"text-align: justify\">When we declare object m, 6 bytes (assuming integers and pointers occupy 2 bytes in our machine) will be allocate to m. The memory allocated for data member matrix dynamically, will be allocated outside the object in heap.<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-115 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-41.png\" alt=\"\" width=\"916\" height=\"401\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-41.png 916w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-41-300x131.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-41-768x336.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-41-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-41-225x98.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-41-350x153.png 350w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/p>\n<p style=\"text-align: justify\">When the object goes out of scope memory for object will be released by default destructor, but memory allocated in heap will not be released, generating garbage<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-117 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43.png\" alt=\"\" width=\"1171\" height=\"476\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43.png 1171w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43-300x122.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43-768x312.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43-1024x416.png 1024w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43-225x91.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-43-350x142.png 350w\" sizes=\"auto, (max-width: 1171px) 100vw, 1171px\" \/><\/p>\n<p style=\"text-align: justify\">This problem can be overcome by creating destructor which releases dynamically constructed memory in heap before destroying the object. We can define destructor in our matrix class as follows<\/p>\n<p>&nbsp;<\/p>\n<p>~Matrix()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>delete matrix[i];<\/p>\n<p>delete matrix;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; \u201cI am Destructor&#8221; &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p style=\"text-align: justify\">Defining this destructor in our matrix class will solve the problem of garbage , as when object goes out of scope this destructor will be called which will release the dynamically created memory in heap before destroying the object.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-118 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-44.png\" alt=\"\" width=\"797\" height=\"315\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-44.png 797w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-44-300x119.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-44-768x304.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-44-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-44-225x89.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-44-350x138.png 350w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Following is the program to add two matrices with dynamic constructor and user defined destructor.<\/p>\n<p>&nbsp;<\/p>\n<p>\/* Program : C++ program to add two matrices with dynamic constructor and user defined destructor*\/<\/p>\n<p>&nbsp;<\/p>\n<p>#include&lt;iostream&gt;<\/p>\n<p>#include&lt;cmath&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>class Matrix<\/p>\n<p>{<\/p>\n<p>int row,col;<\/p>\n<p>int **matrix;<\/p>\n<p>&nbsp;<\/p>\n<p>public :<\/p>\n<p>Matrix(int r, int c)<\/p>\n<p>{<\/p>\n<p>row = r;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 col = c;<\/p>\n<p>matrix = new int *[row];<\/p>\n<p>&nbsp;<\/p>\n<p>for (int i=0; i&lt;col; i++)<\/p>\n<p>matrix[i] = new int[col];<\/p>\n<p>}<\/p>\n<p>void readMatrix()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;Enter the matrix&#8221; &lt;&lt; endl;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cin &gt;&gt; matrix[i][j];<\/p>\n<p>}<\/p>\n<p>void displayMatrix()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>{<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cout &lt;&lt; matrix[i][j] &lt;&lt; &#8221; &#8220;;<\/p>\n<p>cout &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>Matrix resultant(row,col);<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;<\/p>\n<p>}<\/p>\n<p>~Matrix()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>delete matrix[i];<\/p>\n<p>delete matrix;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; \u201cI am Destructor&#8221; &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>}; \/\/ End of Class<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>int r,c;<\/p>\n<p>cout &lt;&lt; &#8220;Enter the no of rows of input Matrices : &#8220;; cin &gt;&gt; r;<\/p>\n<p>cout &lt;&lt; &#8220;Enter the no of columns of input Matrices : &#8220;; cin &gt;&gt; c;<\/p>\n<p>Matrix m1(r,c),m2(r,c), result(r,c);<\/p>\n<p>m1.readMatrix();<\/p>\n<p>m2.readMatrix();<\/p>\n<p>result = m1+m2;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;The resultant matrix is &#8221; &lt;&lt; endl;<\/p>\n<p>result.displayMatrix();<\/p>\n<p>}<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-119\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-45.png\" alt=\"\" width=\"921\" height=\"676\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-45.png 921w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-45-300x220.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-45-768x564.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-45-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-45-225x165.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-45-350x257.png 350w\" sizes=\"auto, (max-width: 921px) 100vw, 921px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-120 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-46.png\" alt=\"\" width=\"533\" height=\"256\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-46.png 533w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-46-300x144.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-46-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-46-225x108.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-46-350x168.png 350w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/p>\n<p style=\"text-align: justify\">Here destructor will release dynamically constructed memory in heap before destroying the object. But as shown above, defining appropriate destructor solves the problem of memory leak, but the above code results in runtime error &#8211; access violation while reading some memory. What is the cause of this error? Let us try to understand the same.<\/p>\n<p>&nbsp;<\/p>\n<p>Well the cause of the error is the compiler provided copy constructor. Let us understand it with the help of an example.<\/p>\n<p>&nbsp;<\/p>\n<p>Have a look at the following declaration<\/p>\n<p>Matrix m;<\/p>\n<p>Matrix m2(m1);<\/p>\n<p style=\"text-align: justify\">This will create object m with default constructor, and m2 will be constructed from m1. Since we have not defined copy constructor, m2 will be constructed with compiler provided copy constructor. Compiler provided constructor performs shallow copy, which will creates copy of object byte by byte, therefore copy of object is made but copy of memory allocated for matrix in heap will not be made. Only the copy of address stored in the object will be made, which will cause both m1 and m2 to point to same memory in heap.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-121 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-47.png\" alt=\"\" width=\"755\" height=\"739\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-47.png 755w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-47-300x294.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-47-65x64.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-47-225x220.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-47-350x343.png 350w\" sizes=\"auto, (max-width: 755px) 100vw, 755px\" \/><\/p>\n<p style=\"text-align: justify\">In our program the error arises when compiler provided copy constructor creates shallow copy of function argument and shallow copy of temporary copy of return object.<\/p>\n<p>&nbsp;<\/p>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>Matrix resultant(row,col);<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;<\/p>\n<p>}<\/p>\n<p>When these two objects go out of scope they will destroy the memory allocated in heap for matrix, which is also being pointed by actual argument and the object receiving the returned object respectively.<\/p>\n<p>So what is the solution for this problem?<\/p>\n<p>Well to solve this problem we can define our own copy constructor, which does deep copy as follows<\/p>\n<p>Matrix(Matrix &amp;m)<\/p>\n<p>{<\/p>\n<p>row = m.row;<\/p>\n<p>col = m.col;<\/p>\n<p>matrix = new int *[row];<\/p>\n<p>for (int i=0; i&lt;col; i++)<\/p>\n<p>matrix[i] = new int[col];<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>{<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>matrix[i][j] = m.matrix[i][j];<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Matrix Class with Dynamic Construction , Destructor and Copy Constructor<\/p>\n<p>\/* Program : C++ program to add two matrices*\/<\/p>\n<p>#include&lt;iostream&gt;<\/p>\n<p>#include&lt;cmath&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>class Matrix<\/p>\n<p>{<\/p>\n<p>int row,col;<\/p>\n<p>int **matrix;<\/p>\n<p>public :<\/p>\n<p>Matrix(int r, int c)<\/p>\n<p>{<\/p>\n<p>row = r;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 col = c;<\/p>\n<p>matrix = new int *[row];<\/p>\n<p>for (int i=0; i&lt;col; i++)<\/p>\n<p>matrix[i] = new int[col];<\/p>\n<p>}<\/p>\n<p>void readMatrix()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;Enter the matrix&#8221; &lt;&lt; endl;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cin &gt;&gt; matrix[i][j];<\/p>\n<p>}<\/p>\n<p>void displayMatrix()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>{<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cout &lt;&lt; matrix[i][j] &lt;&lt; &#8221; &#8220;;<\/p>\n<p>cout &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>Matrix resultant(row,col);<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; return resultant;<\/p>\n<p>}<\/p>\n<p>~Matrix()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>delete matrix[i];<\/p>\n<p>delete matrix;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; \u201cI am Destructor&#8221; &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>Matrix(Matrix &amp;m)<\/p>\n<p>{<\/p>\n<p>row = m.row;<\/p>\n<p>col = m.col;<\/p>\n<p>matrix = new int *[row];<\/p>\n<p>for (int i=0; i&lt;col; i++)<\/p>\n<p>matrix[i] = new int[col];<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>{<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>matrix[i][j] = m.matrix[i][j];<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}; \/\/ End of Class<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>int r,c;<\/p>\n<p>&nbsp;<\/p>\n<p>cout &lt;&lt; &#8220;Enter the no of rows of input Matrices : &#8220;; cin &gt;&gt; r;<\/p>\n<p>cout &lt;&lt; &#8220;Enter the no of columns of input Matrices : &#8220;; cin &gt;&gt; c;<\/p>\n<p>Matrix m1(r,c),m2(r,c), result(r,c);<\/p>\n<p>m1.readMatrix();<\/p>\n<p>m2.readMatrix();<\/p>\n<p>result = m1+m2;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;The resultant matrix is &#8221; &lt;&lt; endl;<\/p>\n<p>result.displayMatrix();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-122\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48.png\" alt=\"\" width=\"1354\" height=\"1220\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48.png 1354w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48-300x270.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48-768x692.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48-1024x923.png 1024w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48-65x59.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48-225x203.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-48-350x315.png 350w\" sizes=\"auto, (max-width: 1354px) 100vw, 1354px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Well this time it is default implementation of overloaded = operator function provided by the compiler. If we do not overload = operator in our class, compiler provides overloaded = operator. The default implementation of overloaded = operator provided by compiler again does shallow copy. So how to overcome this problem. To overcome this problem we can define our overloaded assignment operator which does deep copy as follows<\/p>\n<p>&nbsp;<\/p>\n<p>void operator= (Matrix m)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>row = m.row;<\/p>\n<p>col = m.col;<\/p>\n<p>&nbsp;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>&nbsp;<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>matrix[i][j] = m.matrix[i][j];<\/p>\n<p>}<\/p>\n<p style=\"text-align: justify\">Now in our matrix class we have defined important trio namely destructor, user defined copy constructor amd user defined overloed =operator function, our program will work fine.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Matrix Class with Dynamic Construction, Derstructor, Copy Constructor and Overloaded = Operator<\/p>\n<p>&nbsp;<\/p>\n<p>\/* Program : C++ program to add two matrices*\/<\/p>\n<p>#include&lt;iostream&gt;<\/p>\n<p>#include&lt;cmath&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace std;<\/p>\n<p>class Matrix<\/p>\n<p>{<\/p>\n<p>int row,col;<\/p>\n<p>int **matrix;<\/p>\n<p>&nbsp;<\/p>\n<p>public :<\/p>\n<p>Matrix(int r, int c)<\/p>\n<p>{<\/p>\n<p>row = r;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 col = c;<\/p>\n<p>matrix = new int *[row];<\/p>\n<p>for (int i=0; i&lt;col; i++)<\/p>\n<p>matrix[i] = new int[col];<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>void readMatrix()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;Enter the matrix&#8221; &lt;&lt; endl;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>&nbsp;<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cin &gt;&gt; matrix[i][j];<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void displayMatrix()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>{<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>cout &lt;&lt; matrix[i][j] &lt;&lt; &#8221; &#8220;;<\/p>\n<p>&nbsp;<\/p>\n<p>cout &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>Matrix operator+ (Matrix m)<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>Matrix resultant(row,col);<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>resultant.matrix[i][j] = matrix[i][j] + m.matrix[i][j];<\/p>\n<p>return resultant;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>~Matrix()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>delete matrix[i];<\/p>\n<p>delete matrix;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; \u201cI am Destructor&#8221; &lt;&lt; endl;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Matrix(Matrix &amp;m)<\/p>\n<p>{<\/p>\n<p>row = m.row;<\/p>\n<p>&nbsp;<\/p>\n<p>col = m.col;<\/p>\n<p>matrix = new int *[row];<\/p>\n<p>for (int i=0; i&lt;col; i++)<\/p>\n<p>matrix[i] = new int[col];<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>matrix[i][j] = m.matrix[i][j];<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void operator= (Matrix m)<\/p>\n<p>{<\/p>\n<p>row = m.row;<\/p>\n<p>col = m.col;<\/p>\n<p>&nbsp;<\/p>\n<p>for (int i=0; i&lt;row; i++)<\/p>\n<p>for (int j=0; j&lt;col; j++)<\/p>\n<p>matrix[i][j] = m.matrix[i][j];<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}; \/\/ End of Class<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int r,c;<\/p>\n<p>&nbsp;<\/p>\n<p>cout &lt;&lt; &#8220;Enter the no of rows of input Matrices : &#8220;; cin &gt;&gt; r;<\/p>\n<p>&nbsp;<\/p>\n<p>cout &lt;&lt; &#8220;Enter the no of columns of input Matrices : &#8220;; cin &gt;&gt; c;<\/p>\n<p>Matrix m1(r,c),m2(r,c), result(r,c);<\/p>\n<p>&nbsp;<\/p>\n<p>m1.readMatrix();<\/p>\n<p>m2.readMatrix();<\/p>\n<p>&nbsp;<\/p>\n<p>result = m1+m2;<\/p>\n<p>cout &lt;&lt; endl &lt;&lt; &#8220;The resultant matrix is &#8221; &lt;&lt; endl;<\/p>\n<p>&nbsp;<\/p>\n<p>result.displayMatrix();<\/p>\n<p>}<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-123 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-49.png\" alt=\"\" width=\"611\" height=\"486\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-49.png 611w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-49-300x239.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-49-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-49-225x179.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-49-350x278.png 350w\" sizes=\"auto, (max-width: 611px) 100vw, 611px\" \/><\/p>\n<p style=\"text-align: justify\">Well, appropriate implementation of important trio in our class makes our program work !!!<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Compiler supplied Destructor, Copy constructor and overloaded assignment operator are good enough as long as the memory of data members are not constructed dynamically. But if we create a class which constructs memory for some of its data members dynamically, it becomes important\/necessary to define this trio ourselves appropriately in our class.<\/p>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on The Important Trio<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/YyEVDfMhLb4\" 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>11) \u201cC++ FAQs\u201d, Pearson Education.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":13,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-jyoti-pareek"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-111","chapter","type-chapter","status-publish","hentry","contributor-dr-jyoti-pareek"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/111","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":6,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/111\/revisions"}],"predecessor-version":[{"id":403,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/111\/revisions\/403"}],"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\/111\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=111"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=111"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}