{"id":242,"date":"2018-07-11T08:15:55","date_gmt":"2018-07-11T08:15:55","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=242"},"modified":"2022-01-06T09:12:51","modified_gmt":"2022-01-06T09:12:51","slug":"conversion-function","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/conversion-function\/","title":{"rendered":"Conversion Function"},"content":{"raw":"We may recall that when we assign a value of specific data type to the variable of another type, the implicit conversion takes place.\r\n\r\n&nbsp;\r\n\r\nFor example if we have two variables declared as follows,\r\n\r\n&nbsp;\r\n\r\nint\u00a0 x = 10;\r\n\r\nfloat\u00a0 y;\r\n\r\n&nbsp;\r\n\r\nWe can have following assignment statements in our program y = x ; -----(i)\r\n\r\n&nbsp;\r\n\r\nx = y ;\u00a0 -----(ii)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Here x is integer and y is float. Though no operation can be performed on different data types, above statement works because:<\/p>\r\n&nbsp;\r\n\r\nIn case (i) x gets implicitly converted to float and assignment succeeds.\r\n\r\nIn case (ii) y gets implicitly converted to int and assignment succeeds.\r\n\r\n&nbsp;\r\n\r\nWhat if we declare an object of Time t and an integer declared as follows\r\n\r\n&nbsp;\r\n\r\nTime t;\r\n\r\nint\u00a0 x;\r\n\r\n&nbsp;\r\n\r\nWill this assignment succeed?\r\n\r\n&nbsp;\r\n\r\nx = t\u00a0\u00a0\u00a0 or\r\n\r\nt = x ;\r\n\r\n&nbsp;\r\n\r\nNo. Why not? Why here t does not get implicitly converted to int and x does not get implicitly get converted to object of Time class?\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">This is so what because int and float is standard \/ pre-defined data type provided by complier , therefore complier knows how to convert int to float or float to int or in general how to convert one standard data type into another standard data type.<\/p>\r\n&nbsp;\r\n\r\nBut when need arises for the following conversion x = t;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Where t is a variable of user defined data type Time, the complier doesn\u2019t know how to convert time object to integer or vice a versa.Implicit conversions of user defined data type is not defined, but C++ allows as the facility to define how we wish to convert a user defined data type(implemented as class) to standard data type or vice versa. Also we can define how to convert one user defined data type to another user defined data type (object of one class to another type).<\/p>\r\n&nbsp;\r\n\r\nUser defined conversions can be achieved through\r\n<ul>\r\n \t<li>(1) Constructor<\/li>\r\n \t<li>(2) Conversion function<\/li>\r\n<\/ul>\r\nLet us understand how following user Defined Conversions are defined for user defined data types implemented as class\r\n<ul>\r\n \t<li>(1) Class to Standard data type<\/li>\r\n \t<li>(2) Standard data type to class<\/li>\r\n \t<li>(3) Object of one class to object of another Data type<\/li>\r\n \t<li><strong>(1) <\/strong><strong>Class to Standard Data type<\/strong><\/li>\r\n<\/ul>\r\nThis conversion can be achieved through a special function called conversion function.\r\n\r\n&nbsp;\r\n\r\nThe syntax of conversion function is\r\n\r\n&nbsp;\r\n\r\nOperator datatype()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nReturn datatype;\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">For example if we wish to convert a time object to an integer (seconds). We can define conversion function \/ operator function in Time class as follows<\/p>\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n{\r\n\r\nint hrs,min,sec;\r\n\r\npublic:\r\n\r\nTime() {hrs=min=sec=0;}\r\n\r\n&nbsp;\r\n\r\nTime (int h, int m,int s)\r\n\r\n{\r\n\r\nhrs = h;\r\n\r\nmin = m;\r\n\r\nsec = s;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\noperatorint ()\r\n\r\n{\r\n\r\nint seconds;\r\n\r\nseconds = hrs*60*60 + min*60 + sec;\r\n\r\nreturn seconds;\r\n\r\n}\r\n\r\n}\r\n\r\nHaving defined this function in Time class, following code will be successfully compiled.\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nint seconds;\r\n\r\nTime t(2,15,30);\r\n\r\n&nbsp;\r\n\r\nseconds = t;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Will work\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>(2) Standard data type to class<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">This Conversion can be achieved by single parameter constructor. For example if we wish to convert an integer to time object. We can define a constructor in time class which takes single integer parameter<\/p>\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint hrs, min, sec;\r\n\r\n&nbsp;\r\n\r\npublic:\r\n\r\nTime(int seconds)\r\n\r\n{\r\n\r\nsec = seconds;\r\n\r\n}\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nHaving defined this constructor we will be able to do following assignment.\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nint seconds;\r\n\r\nTime t;\r\n\r\n&nbsp;\r\n\r\nt = seconds;\u00a0\u00a0 \/\/\u00a0\u00a0 Will work\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\n<strong>(3) Object of one class to object of another class<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">It is also possible to assign object of one class to object of another class. Let us understand it with the help of an example, if we have two class namely INR and USDOLLLAR<\/p>\r\n&nbsp;\r\n\r\nIf we have following objects defined.\r\n\r\nINR mymoney;\r\n\r\nUSDOLLAR yourmoney;\r\n\r\n&nbsp;\r\n\r\nWe can make appropriate enhancement in class INR and USDOLLAR to make following assignments work\r\n\r\nmymoney = yourmoney\r\n\r\nor\r\n\r\nYourmoney= mymoney\r\n\r\n&nbsp;\r\n\r\nFollowing assignment can be made to work by defining of constructor in sourceor conversion function in appropriate class\r\n<table class=\"aligncenter\" style=\"height: 140px;\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 28px;\">\r\n<td style=\"width: 296.063px; height: 28px;\">Mymoney=<\/td>\r\n<td style=\"width: 13.0625px; height: 28px;\"><\/td>\r\n<td style=\"width: 337.063px; height: 28px;\">yourmoney<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px;\">\r\n<td style=\"width: 296.063px; height: 28px;\">\u2193<\/td>\r\n<td style=\"width: 13.0625px; height: 28px;\"><\/td>\r\n<td style=\"width: 337.063px; height: 28px;\">\u2193<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px;\">\r\n<td style=\"width: 296.063px; height: 28px;\">Destination<\/td>\r\n<td style=\"width: 13.0625px; height: 28px;\"><\/td>\r\n<td style=\"width: 337.063px; height: 28px;\">Source<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px;\">\r\n<td style=\"width: 296.063px; height: 28px;\">Destination class INR<\/td>\r\n<td style=\"width: 13.0625px; height: 28px;\"><\/td>\r\n<td style=\"width: 337.063px; height: 28px;\">Source class USDOLLAR<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">To make this assignment work we can either write conversion function in source class or we can write constructor in destination class.If we want to modify destination class we can write constructor in destination (which is class INR in above assignment statement) class as follows<\/p>\r\n&nbsp;\r\n\r\nclass USDOLLAR\r\n\r\nclass INR\r\n\r\n{\r\n\r\nint Rupee;\r\n\r\nint paisa;\r\n\r\n&nbsp;\r\n\r\npublic:\r\n\r\n&nbsp;\r\n\r\nINR() { Rupee = 0; paisa = 0;}\r\n\r\nINR (USDOLLAR USD)\r\n\r\n{\r\n\r\nRupee = USD.dollar \/70; ___(1)\r\n\r\npaisa = USD&gt;cent \/70; ___(2)\r\n\r\n}\r\n\r\n}\r\n<p style=\"text-align: justify;\">But in the above program statement (1) and (2) will not work. Why these statements will not work?This is because dollar and cent are private members of USDOLLAR class, therefore they can\u2019t be accessed in constructor of INR class. So what can we do?<\/p>\r\n&nbsp;\r\n\r\nWe can write get functions in INR class to get the values of dollar and cent data members.\r\n\r\n&nbsp;\r\n\r\nclass INR\r\n\r\n{\r\n\r\nint Rupee;\r\n\r\n&nbsp;\r\n\r\nint paisa;\r\n\r\n&nbsp;\r\n\r\npublic:\r\n\r\nINR (USDOLLAR USD)\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nRupee = USD.getdollar()*70;\r\n\r\npaisa = USD.getdollar()*70;\r\n\r\n}\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nIf we want to modify Source class, we can write conversion function in USDOLLAR class.\r\n\r\n&nbsp;\r\n\r\nclass USDoller\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nint doller;\r\n\r\nint cent;\r\n\r\npublic:\r\n\r\n&nbsp;\r\n\r\n---\r\n\r\n---\r\n\r\noperator INR()\r\n\r\n{\r\n\r\nINR money;\r\n\r\nmoney Rupe=dollar\/70; __(1)\r\n\r\nmoney paisa=cent\/70; __(2)\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nHere also\u00a0 (1) and (2) will not work.\u00a0 Why?\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Again for the same reason that rupee and paisa are private members of INR class , therefore they are not accessible in USDOLLAR class.To make above conversion function work, we can do one of the following things<\/p>\r\n&nbsp;\r\n\r\n(1)\u00a0 By creating and returning creating unnamed INR object.\r\n\r\n&nbsp;\r\n\r\nclass USDOLLAR\r\n\r\n{\r\n\r\nint dollar;\r\n\r\n&nbsp;\r\n\r\nint cent;\r\n\r\n&nbsp;\r\n\r\npublic:\r\n\r\n&nbsp;\r\n\r\noperator INR()\r\n\r\n{\r\n\r\nreturn INR(dollar\/70,cent\/70);\r\n\r\n}\r\n\r\n}\r\n<p style=\"text-align: justify;\">Here we are creating unnamed INR object initializing it with values dollar\/70 and cent\/70.Note: this will work if we have parameterized constructor in USDOLAAR class.<\/p>\r\n&nbsp;\r\n\r\n(2)\u00a0 By writing set method in INR class.\r\n\r\n&nbsp;\r\n\r\nclass USDollar\r\n\r\n{\r\n\r\nint dollar;\r\n\r\nint cent;\r\n\r\npublic:\r\n\r\noperator IN()\r\n\r\n{\r\n\r\nINR money\r\n\r\nmoney.setrupee(dollar\/70);\r\n\r\nmoney.setpaisa(dollar\/70);\r\n\r\nreturn money;\r\n\r\n}\r\n\r\n};\r\n\r\nLet us summaries again\r\n\r\nIf we have declared following two objects\r\n\r\nINR mymoney;\r\n\r\nUSDollar\u00a0 yourmoney;\r\n\r\n&nbsp;\r\n\r\nTo make following assignment statements work\r\n\r\nmymoney =yourmoney --- (1)\r\n\r\nyourmoney =mymoney ---- (2)\r\n\r\n&nbsp;\r\n\r\nWe can do either of the following\r\n\r\n(1) We can write appropriate constructors in INR and USDollar class.\r\n\r\n(2) We can write constructor in INR class to make (1) work and conversion function in INR class to make (2) work.\r\n\r\n(3) We can write constructor in USDollar class to make (2) work and write conversion function in USDollar class to make (1) work.\r\n\r\n(4) We can write conversion function in USDollar class and INR class to make (1) and (2) work respectively.\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n\r\n(1) User defined conversion can be achieved through\r\n\r\n(i) Constructor\r\n\r\n(ii) Conversion function\r\n\r\n(2) Conversion from scalar data type to class type can be achieved by writing appropriate constructor in class.\r\n\r\n(3) Conversion of class type to scalar data type can be achieved by writing conversion function in class.\r\n\r\n(4) Class to class conversion canbe achieved by either writing constructor function in destination class or by writing operator function in source class.\r\n\r\n&nbsp;\r\n\r\n<strong>Additional References<\/strong>\r\n\r\n&nbsp;\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;\r\n\r\n&nbsp;","rendered":"<p>We may recall that when we assign a value of specific data type to the variable of another type, the implicit conversion takes place.<\/p>\n<p>&nbsp;<\/p>\n<p>For example if we have two variables declared as follows,<\/p>\n<p>&nbsp;<\/p>\n<p>int\u00a0 x = 10;<\/p>\n<p>float\u00a0 y;<\/p>\n<p>&nbsp;<\/p>\n<p>We can have following assignment statements in our program y = x ; &#8212;&#8211;(i)<\/p>\n<p>&nbsp;<\/p>\n<p>x = y ;\u00a0 &#8212;&#8211;(ii)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Here x is integer and y is float. Though no operation can be performed on different data types, above statement works because:<\/p>\n<p>&nbsp;<\/p>\n<p>In case (i) x gets implicitly converted to float and assignment succeeds.<\/p>\n<p>In case (ii) y gets implicitly converted to int and assignment succeeds.<\/p>\n<p>&nbsp;<\/p>\n<p>What if we declare an object of Time t and an integer declared as follows<\/p>\n<p>&nbsp;<\/p>\n<p>Time t;<\/p>\n<p>int\u00a0 x;<\/p>\n<p>&nbsp;<\/p>\n<p>Will this assignment succeed?<\/p>\n<p>&nbsp;<\/p>\n<p>x = t\u00a0\u00a0\u00a0 or<\/p>\n<p>t = x ;<\/p>\n<p>&nbsp;<\/p>\n<p>No. Why not? Why here t does not get implicitly converted to int and x does not get implicitly get converted to object of Time class?<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">This is so what because int and float is standard \/ pre-defined data type provided by complier , therefore complier knows how to convert int to float or float to int or in general how to convert one standard data type into another standard data type.<\/p>\n<p>&nbsp;<\/p>\n<p>But when need arises for the following conversion x = t;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Where t is a variable of user defined data type Time, the complier doesn\u2019t know how to convert time object to integer or vice a versa.Implicit conversions of user defined data type is not defined, but C++ allows as the facility to define how we wish to convert a user defined data type(implemented as class) to standard data type or vice versa. Also we can define how to convert one user defined data type to another user defined data type (object of one class to another type).<\/p>\n<p>&nbsp;<\/p>\n<p>User defined conversions can be achieved through<\/p>\n<ul>\n<li>(1) Constructor<\/li>\n<li>(2) Conversion function<\/li>\n<\/ul>\n<p>Let us understand how following user Defined Conversions are defined for user defined data types implemented as class<\/p>\n<ul>\n<li>(1) Class to Standard data type<\/li>\n<li>(2) Standard data type to class<\/li>\n<li>(3) Object of one class to object of another Data type<\/li>\n<li><strong>(1) <\/strong><strong>Class to Standard Data type<\/strong><\/li>\n<\/ul>\n<p>This conversion can be achieved through a special function called conversion function.<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax of conversion function is<\/p>\n<p>&nbsp;<\/p>\n<p>Operator datatype()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>Return datatype;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">For example if we wish to convert a time object to an integer (seconds). We can define conversion function \/ operator function in Time class as follows<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>{<\/p>\n<p>int hrs,min,sec;<\/p>\n<p>public:<\/p>\n<p>Time() {hrs=min=sec=0;}<\/p>\n<p>&nbsp;<\/p>\n<p>Time (int h, int m,int s)<\/p>\n<p>{<\/p>\n<p>hrs = h;<\/p>\n<p>min = m;<\/p>\n<p>sec = s;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>operatorint ()<\/p>\n<p>{<\/p>\n<p>int seconds;<\/p>\n<p>seconds = hrs*60*60 + min*60 + sec;<\/p>\n<p>return seconds;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Having defined this function in Time class, following code will be successfully compiled.<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>int seconds;<\/p>\n<p>Time t(2,15,30);<\/p>\n<p>&nbsp;<\/p>\n<p>seconds = t;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Will work<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>(2) Standard data type to class<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">This Conversion can be achieved by single parameter constructor. For example if we wish to convert an integer to time object. We can define a constructor in time class which takes single integer parameter<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int hrs, min, sec;<\/p>\n<p>&nbsp;<\/p>\n<p>public:<\/p>\n<p>Time(int seconds)<\/p>\n<p>{<\/p>\n<p>sec = seconds;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>Having defined this constructor we will be able to do following assignment.<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>int seconds;<\/p>\n<p>Time t;<\/p>\n<p>&nbsp;<\/p>\n<p>t = seconds;\u00a0\u00a0 \/\/\u00a0\u00a0 Will work<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p><strong>(3) Object of one class to object of another class<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">It is also possible to assign object of one class to object of another class. Let us understand it with the help of an example, if we have two class namely INR and USDOLLLAR<\/p>\n<p>&nbsp;<\/p>\n<p>If we have following objects defined.<\/p>\n<p>INR mymoney;<\/p>\n<p>USDOLLAR yourmoney;<\/p>\n<p>&nbsp;<\/p>\n<p>We can make appropriate enhancement in class INR and USDOLLAR to make following assignments work<\/p>\n<p>mymoney = yourmoney<\/p>\n<p>or<\/p>\n<p>Yourmoney= mymoney<\/p>\n<p>&nbsp;<\/p>\n<p>Following assignment can be made to work by defining of constructor in sourceor conversion function in appropriate class<\/p>\n<table class=\"aligncenter\" style=\"height: 140px;\">\n<tbody>\n<tr style=\"height: 28px;\">\n<td style=\"width: 296.063px; height: 28px;\">Mymoney=<\/td>\n<td style=\"width: 13.0625px; height: 28px;\"><\/td>\n<td style=\"width: 337.063px; height: 28px;\">yourmoney<\/td>\n<\/tr>\n<tr style=\"height: 28px;\">\n<td style=\"width: 296.063px; height: 28px;\">\u2193<\/td>\n<td style=\"width: 13.0625px; height: 28px;\"><\/td>\n<td style=\"width: 337.063px; height: 28px;\">\u2193<\/td>\n<\/tr>\n<tr style=\"height: 28px;\">\n<td style=\"width: 296.063px; height: 28px;\">Destination<\/td>\n<td style=\"width: 13.0625px; height: 28px;\"><\/td>\n<td style=\"width: 337.063px; height: 28px;\">Source<\/td>\n<\/tr>\n<tr style=\"height: 28px;\">\n<td style=\"width: 296.063px; height: 28px;\">Destination class INR<\/td>\n<td style=\"width: 13.0625px; height: 28px;\"><\/td>\n<td style=\"width: 337.063px; height: 28px;\">Source class USDOLLAR<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">To make this assignment work we can either write conversion function in source class or we can write constructor in destination class.If we want to modify destination class we can write constructor in destination (which is class INR in above assignment statement) class as follows<\/p>\n<p>&nbsp;<\/p>\n<p>class USDOLLAR<\/p>\n<p>class INR<\/p>\n<p>{<\/p>\n<p>int Rupee;<\/p>\n<p>int paisa;<\/p>\n<p>&nbsp;<\/p>\n<p>public:<\/p>\n<p>&nbsp;<\/p>\n<p>INR() { Rupee = 0; paisa = 0;}<\/p>\n<p>INR (USDOLLAR USD)<\/p>\n<p>{<\/p>\n<p>Rupee = USD.dollar \/70; ___(1)<\/p>\n<p>paisa = USD&gt;cent \/70; ___(2)<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p style=\"text-align: justify;\">But in the above program statement (1) and (2) will not work. Why these statements will not work?This is because dollar and cent are private members of USDOLLAR class, therefore they can\u2019t be accessed in constructor of INR class. So what can we do?<\/p>\n<p>&nbsp;<\/p>\n<p>We can write get functions in INR class to get the values of dollar and cent data members.<\/p>\n<p>&nbsp;<\/p>\n<p>class INR<\/p>\n<p>{<\/p>\n<p>int Rupee;<\/p>\n<p>&nbsp;<\/p>\n<p>int paisa;<\/p>\n<p>&nbsp;<\/p>\n<p>public:<\/p>\n<p>INR (USDOLLAR USD)<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>Rupee = USD.getdollar()*70;<\/p>\n<p>paisa = USD.getdollar()*70;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>If we want to modify Source class, we can write conversion function in USDOLLAR class.<\/p>\n<p>&nbsp;<\/p>\n<p>class USDoller<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>int doller;<\/p>\n<p>int cent;<\/p>\n<p>public:<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;<\/p>\n<p>&#8212;<\/p>\n<p>operator INR()<\/p>\n<p>{<\/p>\n<p>INR money;<\/p>\n<p>money Rupe=dollar\/70; __(1)<\/p>\n<p>money paisa=cent\/70; __(2)<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Here also\u00a0 (1) and (2) will not work.\u00a0 Why?<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Again for the same reason that rupee and paisa are private members of INR class , therefore they are not accessible in USDOLLAR class.To make above conversion function work, we can do one of the following things<\/p>\n<p>&nbsp;<\/p>\n<p>(1)\u00a0 By creating and returning creating unnamed INR object.<\/p>\n<p>&nbsp;<\/p>\n<p>class USDOLLAR<\/p>\n<p>{<\/p>\n<p>int dollar;<\/p>\n<p>&nbsp;<\/p>\n<p>int cent;<\/p>\n<p>&nbsp;<\/p>\n<p>public:<\/p>\n<p>&nbsp;<\/p>\n<p>operator INR()<\/p>\n<p>{<\/p>\n<p>return INR(dollar\/70,cent\/70);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p style=\"text-align: justify;\">Here we are creating unnamed INR object initializing it with values dollar\/70 and cent\/70.Note: this will work if we have parameterized constructor in USDOLAAR class.<\/p>\n<p>&nbsp;<\/p>\n<p>(2)\u00a0 By writing set method in INR class.<\/p>\n<p>&nbsp;<\/p>\n<p>class USDollar<\/p>\n<p>{<\/p>\n<p>int dollar;<\/p>\n<p>int cent;<\/p>\n<p>public:<\/p>\n<p>operator IN()<\/p>\n<p>{<\/p>\n<p>INR money<\/p>\n<p>money.setrupee(dollar\/70);<\/p>\n<p>money.setpaisa(dollar\/70);<\/p>\n<p>return money;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>Let us summaries again<\/p>\n<p>If we have declared following two objects<\/p>\n<p>INR mymoney;<\/p>\n<p>USDollar\u00a0 yourmoney;<\/p>\n<p>&nbsp;<\/p>\n<p>To make following assignment statements work<\/p>\n<p>mymoney =yourmoney &#8212; (1)<\/p>\n<p>yourmoney =mymoney &#8212;- (2)<\/p>\n<p>&nbsp;<\/p>\n<p>We can do either of the following<\/p>\n<p>(1) We can write appropriate constructors in INR and USDollar class.<\/p>\n<p>(2) We can write constructor in INR class to make (1) work and conversion function in INR class to make (2) work.<\/p>\n<p>(3) We can write constructor in USDollar class to make (2) work and write conversion function in USDollar class to make (1) work.<\/p>\n<p>(4) We can write conversion function in USDollar class and INR class to make (1) and (2) work respectively.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>(1) User defined conversion can be achieved through<\/p>\n<p>(i) Constructor<\/p>\n<p>(ii) Conversion function<\/p>\n<p>(2) Conversion from scalar data type to class type can be achieved by writing appropriate constructor in class.<\/p>\n<p>(3) Conversion of class type to scalar data type can be achieved by writing conversion function in class.<\/p>\n<p>(4) Class to class conversion canbe achieved by either writing constructor function in destination class or by writing operator function in source class.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Additional References<\/strong><\/p>\n<p>&nbsp;<\/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<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":35,"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-242","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\/242","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\/242\/revisions"}],"predecessor-version":[{"id":415,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/242\/revisions\/415"}],"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\/242\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=242"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=242"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=242"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}