#include <iostream> using namespace::std; class coin { friend ostream & operator
ID: 652326 • Letter: #
Question
#include <iostream>
using namespace::std;
class coin {
friend ostream & operator << ( ostream &, const coin &);
friend bool operator == ( int n, const coin & right);
// will return true if n is equal to the count field of right
public:
coin(void);
coin(int ct, const char * d);
coin(const coin &);
~coin(void);
int getCount(void) const;
coin & setCount(int n);
coin & setDesc( const char *);
const coin & getLarger( const coin & ) const;
// will return as an alias the larger coin
// , i.e. larger of the invoking instance and the paramater
// use getCount() to determine the largest
const coin & operator = ( const coin &);
coin operator + ( const coin & ) const;
// return coin with counts added
// same desc as the invoking instance
bool operator == ( const coin &) const;
// return true if the counts are equal
bool operator == ( int v) const;
// return true if v is equal to the count field
private:
const char * getDesc(void) const;
int count;
char desc[25];
};
class bank {
friend ostream & operator<<(ostream &, const bank &);
public:
bank(void);
bank(int, const char * coinDesc, const char * ownerName);
bank(const bank &);
bank( const coin &, const char * ownerName);
~bank();
int getCount(void) const;
bank & changeOwner( const char *);
const bank & operator = (const bank &);
bool operator == ( const bank & ) const;
// will return true if the owners are equal as strings
// and coin fields data are equal as coins
bool operator != ( const bank &) const;
private:
coin data;
char * owner;
};
// Write the Class functions
ostream & operator << ( ostream & w, const coin & c)
{
w << c.getDesc() << " C " << c.getCount() << " C " ;
return w;
}
bool operator == (int n, const coin & right)
// will return true if n is equal to the count field of right
{
if (n == right.count)
{
return true;
}
else{
return false;
}
}
coin::coin(void)
{
setCount(0).setDesc(0);
}
coin::coin(int ct, const char * d)
{
setCount(ct).setDesc(d);
}
coin::coin(const coin & other) //: data(other.data), count(other.count),disc(other.disc)
{
setCount(other.getCount()).setDesc(other.getDesc());
}
coin::~coin(void)
{
}
int coin::getCount(void) const
{
return count ;
}
const char * coin::getDesc(void) const
{
return desc;
}
coin & coin::setCount(int count) // MUST USE THE PARAMETER NAME OF COUNT
{
this->count = count;
return *this;
}
coin & coin::setDesc( const char * c)
{
strcpy(this->desc, c);
return * this;
}
const coin & coin::getLarger( const coin & right ) const
{
if (this->count > right.count)
{
return *this;
}
else{
return right;
}
}
// In the above function, explain the 3 uses of const
const coin & coin::operator = ( const coin & right)
{
if( this == & right ) return *this;
this->count = right.count;
strcpy(this->desc, right.desc);
return *this;
}
coin coin::operator + ( const coin & right) const
{
// return coin with counts added
// same desc as the invoking instance
coin temp;
temp.setCount(this->getCount());
temp.setDesc(this->getDesc());
return temp;
}
bool coin::operator == ( const coin & right) const
// return true if the counts are equal
{
return((this->getCount() == right.getCount()) && (this->getDesc() == right.getDesc()));
}
bool coin::operator == ( int v) const
// return true if v is equal to the count field
{
return (this->getCount() == v) ? true : false;
}
// ****** bank functions *******
ostream & operator<<(ostream & w, const bank & b)
{
w << b.getCount() << "Count: " << b.data;
return w;
}
bank::bank(void): data()
{
owner = new char[4];
strcpy(owner, "N/A");
// data = count;
// owner = desc ;
}
bank::bank(int a, const char * coinDesc, const char * ownerName): data(a, coinDesc) //, owner(ownerName)
{
changeOwner(ownerName);
//coin (a, coinDesc);
}
bank::bank(const bank & other): data(other.data), //owner(other.owner)
{
changeOwner(other.owner);
}
bank::bank( const coin & c, const char * ownerName) : data(c) //, owner(ownerName)
{
strcpy(owner, ownerName);
}
bank::~bank()
{
}
int bank::getCount(void) const
{
return data.getCount();
}
bank & bank::changeOwner( const char * c)
{
if(c != nullptr)
{
if(owner != nullptr)
{
delete owner;
}
owner = new char[strlen(c) + 1];
strcpy(owner, c);
}
}
const bank & bank::operator = (const bank & right)
{
if(this == & right ) return *this;
this->data = right.data ;
//this->owner= right.owner ;
owner = new char[right];
return *this;
}
bool bank::operator == ( const bank & right ) const
{
// will return true if the owners are equal as strings
// and the coin fields are equal as coins
return (this->getCount() == right.getCount()) ? true: false;
}
bool bank::operator != ( const bank & right ) const
{
return (this->getCount() != right.getCount()) ? true: false;
}
// *****************************************************************************
// Client functions
coin returnLarger( const coin & left, const coin & right)
// will return the larger coin, based on the count value
// the desc of the coin returned will be "Larger Coin";
{ // write returnLarger now
}
int main()
{
coin a(4,"Dime");
coin c(5,"Penny"), d(12,"Quarter"), e(15,"$20 Gold Coin"), f;
bank b(24,"Dollar Bill", "John Doe");
cout << a << b << c;
// Write the above using function notation:
// ________________________________________________________
// operator <<(operator<<(operator<<(cout, a ), b), c);
f = (c + d) + e;
// Write the above using function notation NOTE: There are 3 operators:
f = c.operator +(d).operator + (e);
// __________________________________________________________
return 0;
}
Explanation / Answer
* OUTPUT / COIN: (4 of Dime) BANK of John Doe contains: COIN: (24 of Dollar Bill) COIN: (5 of Penny) * Test coin operators. COIN: (4 of Dime) is COIN: (5 of Penny) ? false. COIN: (4 of Dime) is it 4? true Friend operator test of same, is it 5? false * Test returnLarger. COIN: (15 of $20 Gold Coin) COIN: (32 of Penny) COIN: (32 of Larger Coin) * Test bank operators. New Bank: BANK of John Doe contains: COIN: (24 of Dollar Bill) Bank B: BANK of John Doe contains: COIN: (24 of Dollar Bill) false returned by ==, != is true. Bob now has New Bank... New Bank: BANK of Bob contains: COIN: (24 of Dollar Bill) Bank B: BANK of John Doe contains: COIN: (24 of Dollar Bill) false returned by ==, != is true. Press any key to continue . . . */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.