if this program wokrs correctly 52
#include <iostream>
using namespace std;
class Purse
{public:
int hundreds, fifties, twenties, tens, ones, halves, quarters;
//default constructor:
Purse () :
hundreds(0), fifties(0), twenties(0), tens(0), ones(0), halves(0), quarters(0)
{}
//Assignment operator:
Purse (int hundred, int fifty, int twenty, int ten, int one, int half, int quarter)
{
hundreds=hundred;
fifties=fifty;
twenties=twenty;
tens=ten;
ones=one;
halves=half;
quarters=quarter;
}
//constructor
Purse (double money)
{
hundreds=int ( money/100);
money= money - (hundreds * 100);//now we're in fifties step
fifties= int(money/50);
money= money- (fifties*50);//twenties step
twenties= int(money/20);
money=money- (twenties *20);//tens step
tens= int(money/10);
money=money- (tens * 10);//ones step
ones= int (money/1);
money=money- (ones );//halves
halves= int (money/0.5);//the maximum number of halves can be one.
money=money- (halves * 0.5);
quarters= int (money/0.25);
money=money- (quarters *0.25);
}
double computeTotal()
{
double total=0;
total= 100*hundreds + 50*fifties+ 20*twenties+ 10*tens
+ones + 0.5*halves+ 0.25 * quarters;
return total;
}
void print ()
{
cout<<"You have: "<<endl;
cout<<hundreds<<" Hundreds"<<endl;
cout<<fifties<<" Fifties"<<endl;
cout<<twenties<<" Twenties"<<endl;
cout<<tens<<" Tens"<<endl;
cout<<ones<<" Ones"<<endl;
cout<<halves<<" Halves"<<endl;
cout<<quarters<<" Quarters"<<endl;
double total = computeTotal();
cout<<"And the total is: "<<total<<endl<<endl<<endl;
}
Purse collect(Purse & p2)
{
Purse p1;
p1.hundreds= hundreds+ p2.hundreds;
p1.fifties= fifties+ p2.fifties;
p1.twenties= twenties+ p2.twenties;
p1.tens= tens+ p2.tens;
p1.ones= ones+ p2.ones;
p1.halves= halves+ p2.halves;
p1.quarters= quarters+ p2.quarters;
return p1;
}
};
Purse collect(Purse & p2);
int main ()
{
Purse purseA (257.5);
purseA.print ();
Purse Clara (231.5);
Clara.print ();
Purse purseB(385.25);
purseB.print();
Purse test;
test= purseA.collect (purseB);
test.print();
Purse p;
p.print();
}
Last edited on