In: Computer Science
C++(OOP)plz SOLVE THE PROBLEM ACCORDING TO
INSTRUCTIONS
Imagine you are on a trip around Europe and have collected
currencies from
the different countries that you have travelled.
You are to write a simple money counting program. Your program will
be able
to deal in the following three currencies
a) Pakistani Rupee
b) Turkish Lira
c) Pound Sterling
You must make a class called Currency. Create its data members
and any
member functions you require. Your class should be written such
that you
are able to execute statements like these in your main
function:
C_Sum =100 C1 + 11 (C2) + 56 (C3)
where C1, C2 and C3 represent different currency bank notes.
% C1 - displays C1 amount in all three currencies.
C1 [“pkr”]– displays the C1 amount in PKR along with the date and
current
exchange rate of the input currency with pkr.
C1 [“gbp”]– displays the C1 amount in Pound Sterling along with the
date and
current exchange rate between the input currency and pounds.
A FEW GUIDELINES
This question involves operator overloading and needs to be
accomplished using classes.
Your main function must be as small as possible. This implies you
must
use class constructors and class functions to accomplish everything
in assignment
Make a separate class called Conversion_Rate. Use constructors to
take
inputs for the exchange rate for the day. Display them every time
you
display the total sum.
Every time you perform a money conversion, your program must
contact
the Conversion_Rate class class to provide the exchange rates for
the
given day. You are to apply the concept of friend functions/
friend
classes (as you need) to allow this class to share its
information.
We cannot overload binary operator '%' as unary operator as given in the question, therefore, I have used - (minus) operator for the function of %C1
Output:
Raw Code:
#include<iostream>
#include<ctime>
using namespace std;
class Currency
{
private:
string type; //
currency type
float val;
// amount
public:
Currency(string typ="",float v=0) //
constructor
{ type=typ; val=v;
}
void print() // print
data
{ cout << val << " " << type
<< endl<<endl; }
Currency operator + (Currency const &);
void operator - ();
void operator [] (string);
friend class Conversion_Rate; // friend
class
};
class Conversion_Rate{
public:
float convert(Currency const &obj,string
type) // convert currency
{
if(obj.type=="pkr")
{
if(type=="tl")
return 0.052 * obj.val;
else
if(type=="gbp")
return 0.0048 * obj.val;
return
obj.val;
}
else if(obj.type=="tl")
{
if(type=="pkr")
return 19.22 * obj.val;
else
if(type=="gbp")
return 0.093 * obj.val;
return
obj.val;
}
else if(obj.type=="gbp")
{
if(type=="pkr")
return 207.7 * obj.val;
else
if(type=="tl")
return 10.8 * obj.val;
return
obj.val;
}
return 0.0;
}
};
Currency Currency:: operator + (Currency const
&obj) // add different currencies
{
Conversion_Rate object;
Currency res;
res.type = type;
res.val = val + object.convert(obj,type);
return res;
}
void Currency:: operator - ()
{
Conversion_Rate object;
// convert to all currencies
cout<<val<<" "<<type<<" =
"<<object.convert(*this,"pkr")<<"
pkr"<<endl;
cout<<val<<" "<<type<<" =
"<<object.convert(*this,"tl")<<" tl"<<endl;
cout<<val<<" "<<type<<" =
"<<object.convert(*this,"gbp")<<"
gbp"<<endl<<endl;
}
void Currency:: operator [] (string t)
{
Conversion_Rate object;
// convert to specified currency
cout<<object.convert(*this,t)<<endl;
// date
time_t now = time(0);
cout<<"Date: "<<ctime(&now);
// Exchange rate
Currency obj(type,1);
cout<<"Exchange rate: 1 "<<type<<" =
"<<object.convert(obj,t)<<"
"<<t<<endl<<endl;
}
int main()
{
// "pkr" => Pakistani Rupee
// "tl" => Turkish Lira
// "gbp" => Pound Sterling
Currency C1("pkr",12), C2("tl",11), C3("gbp",10);
// overloading + operator
Currency C4 = C1+C2+C3;
C4.print();
// overloading - operator to convert to all currencies
-C1;
// convert the object to specified currency along with date and
exchange rate
C2["pkr"];
}