1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
class Sterling
{
public:
Sterling(){}
Sterling(double p, int s, int pe):pounds(p),shilling(s),pence(pe){}
friend istream& operator>>(istream& in, Sterling& s);
friend ostream& operator<<(ostream& out, Sterling& s);
operator double();
Sterling (double decpound);
Sterling operator+(Sterling& s){
return Sterling(double(Sterling(pounds,shilling,pence))+double(s));//problem here
}
Sterling operator-(Sterling& s){
return Sterling(double(Sterling(pounds,shilling,pence))-double(s));
}
Sterling operator*(Sterling& s){
return Sterling(double(Sterling(pounds,shilling,pence))*double(s));
}
Sterling operator/(Sterling& s){
return Sterling(double(Sterling(pounds,shilling,pence))/double(s));
}
private:
int shilling, pence;
double pounds;
};
int main()
{
char choice;
Sterling one(4,10,12), two(12,4,8);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
do
{
cout<<"First sterling value: "<<one<<endl<<endl;
cout<<"Second sterling value: "<<two<<endl;
Sterling add=one+two;//problem here
cout<<add<<endl;
cout<<"\n\nAgain (y/n): ";
cin>>choice;
}while(choice=='y');
_getch();
return 0;
}
Sterling::Sterling (double decpound)
{
double decfrac, decshill,decpence;
Sterling temp;
temp.pounds=static_cast<double>(decpound);
decfrac=decpound-temp.pounds;
decshill=20*decfrac;
temp.shilling=static_cast<double>(decshill);
decfrac=decshill-temp.shilling;
decpence=12*decfrac;
temp.pence=static_cast<double>(decpence);
decshill=static_cast<double>(decshill);
}
Sterling::operator double()
{
return(static_cast<double>(pounds)+((static_cast<double>(shilling)/20)+(static_cast<double>(pence)/240)));
}
ostream& operator<<(ostream& out, Sterling& s)
{
out<<"\x9c "<<s.pounds<<"."<<s.shilling<<"."<<s.pence;
return out;
}
istream& operator>>(istream& in, Sterling& s)
{
cout<<"Enter the pounds: ";in>>s.pounds;
while(s.pounds<0){
cout<<"Pounds can't be less than zero, try again: ";
in>>s.pounds;
}
cout<<"Enter the shillings: ";in>>s.shilling;
while(s.shilling<0){
cout<<"Shillings can't be less than zero, enter again: ";
in>>s.shilling;
}
cout<<"Enter the pence: ";in>>s.pence;
while(s.pence<0){
cout<<"Pence can't be less than zero, enter again: ";
in>>s.pence;
}
return in;
}
|