Program To Convert Given Amount In Dollars To Rupees Using Classes And By Overloading Assignment Operator In C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click View Source “View Source” on the right of this code snippet.


Background and Explanation of the code


We have assumed that 1$ = Rs. 44.20. The above program converts the amount in dollars to rupees. For this we have created two classes rupee and dollar for maintaining amount in rupees and dollars respectively.In main ( ) we first create an object d1 of class dollar and initialize it using single parameter constructor which is then displayed.Then we create object r1 of class rupee.The statement

r1 = d1;

on execution invokes the overloaded typecast operator function operator rupee ( ) which is defined as conversion function in source class dollar.As the above statement involves objects of different classes so there is need of conversion function.The operator rupee ( ) conversion function converts the object d1 of  which it is a member to rupee object which is then returned and assigned to object r1 in main ( ).The statement

return (rupee (rs1));

first creates an unnamed object of class rupee and initialize it  using single parameter constructor (rupee (double)) which is then returned.

OUTPUT

Money in dollars = 4

Money in Rs. =  176.8