### Equality Function

```//DISPLAY 11.1 Equality Function
//Program to demonstrate the function equal. The class DayOfYear
//is the same as in Self-Test Exercise 23-24 in Chapter 6.
#include <iostream>
using namespace std;

class DayOfYear
{
public:
DayOfYear(int the_month, int the_day);
//Precondition: the_month and the_day form a
//possible date. Initializes the date according
//to the arguments.

DayOfYear( );
//Initializes the date to January first.

void input( );

void output( );

int get_month( );
//Returns the month, 1 for January, 2 for February, etc.

int get_day( );
//Returns the day of the month.
private:
void check_date( );
int month;
int day;
};

bool equal(DayOfYear date1, DayOfYear date2);
//Precondition: date1 and date2 have values.
//Returns true if date1 and date2 represent the same date;
//otherwise, returns false.

int main( )
{
DayOfYear today, bach_birthday(3, 21);

cout << "Enter today's date:\n";
today.input( );
cout << "Today's date is ";
today.output( );

cout << "J. S. Bach's birthday is ";
bach_birthday.output( );

if ( equal(today, bach_birthday))
cout << "Happy Birthday Johann Sebastian!\n";
else
cout << "Happy Unbirthday Johann Sebastian!\n";
return 0;
}

bool equal(DayOfYear date1, DayOfYear date2)
{
return ( date1.get_month( ) == date2.get_month( ) &&
date1.get_day( ) == date2.get_day( ) );
}

DayOfYear::DayOfYear(int the_month, int the_day)
: month(the_month), day(the_day)
{
check_date();
}

int DayOfYear::get_month( )
{
return month;
}

int DayOfYear::get_day( )
{
return day;
}

//Uses iostream:
void DayOfYear::input( )
{
cout << "Enter the month as a number: ";
cin >> month;
cout << "Enter the day of the month: ";
cin >> day;
}

//Uses iostream:
void DayOfYear::output( )
{
cout << "month = " << month
<< ", day = " << day << endl;
}

```

### Equality Function as a Friend

```//DISPLAY 11.2 Equality Function as a Friend
//Demonstrates the function equal.
//In this version equal is a friend of the class DayOfYear.
#include <iostream>
using namespace std;

class DayOfYear
{
public:
friend bool equal(DayOfYear date1, DayOfYear date2);
//Precondition: date1 and date2 have values.
//Returns true if date1 and date2 represent the same date;
//otherwise, returns false.

DayOfYear(int the_month, int the_day);
//Precondition: the_month and the_day form a
//possible date. Initializes the date according
//to the arguments.

DayOfYear( );
//Initializes the date to January first.

void input( );

void output( );

int get_month( );
//Returns the month, 1 for January, 2 for February, etc.

int get_day( );
//Returns the day of the month.
private:
void check_date( );
int month;
int day;
};

int main( )
{
//The main part of the program is the same as in Display 11.1.
}

bool equal(DayOfYear date1, DayOfYear date2)
{
return ( date1.month == date2.month &&
date1.day == date2.day );
}

//The rest of this display, including the Sample Dialogue, is the same as in Display 11.1.

```

### Money Class Version 1

```//DISPLAY 11.3 Money Class Version 1
//Program to demonstrate the class Money.
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

//Class for amounts of money in U.S. currency.
class Money
{
public:
friend Money add(Money amount1, Money amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns the sum of the values of amount1 and amount2.

friend bool equal(Money amount1, Money amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns true if the amount1 and amount2 have the same value;
//otherwise, returns false.

Money(long dollars, int cents);
//Initializes the object so its value represents an amount with the
//dollars and cents given by the arguments. If the amount is negative,
//then both dollars and cents must be negative.

Money(long dollars);
//Initializes the object so its value represents \$dollars.00.

Money( );
//Initializes the object so its value represents \$0.00.

double get_value( );
//Precondition: The calling object has been given a value.
//Returns the amount of money recorded in the data of the calling object.

void input(istream& ins);
//Precondition: If ins is a file input stream, then ins has already been
//connected to a file. An amount of money, including a dollar sign, has been
//entered in the input stream ins. Notation for negative amounts is -\$100.00.
//Postcondition: The value of the calling object has been set to
//the amount of money read from the input stream ins.
void output(ostream& outs);
//Precondition: If outs is a file output stream, then outs has already been
//connected to a file.
//Postcondition: A dollar sign and the amount of money recorded
//in the calling object have been sent to the output stream outs.
private:
long all_cents;
};

int digit_to_int(char c);
//Function declaration for function used in the definition of Money::input:
//Precondition: c is one of the digits '0' through '9'.
//Returns the integer for the digit; for example, digit_to_int('3') returns 3.

int main( )
{
Money your_amount, my_amount(10, 9), our_amount;
cout << "Enter an amount of money: ";
your_amount.input(cin);
cout << "Your amount is ";
your_amount.output(cout);
cout << endl;
cout << "My amount is ";
my_amount.output(cout);
cout << endl;

if (equal(your_amount, my_amount))
cout << "We have the same amounts.\n";
else
cout << "One of us is richer.\n";
your_amount.output(cout);
cout << " + ";
my_amount.output(cout);
cout << " equals ";
our_amount.output(cout);
cout << endl;
return 0;
}
{
Money temp;

temp.all_cents = amount1.all_cents + amount2.all_cents;
return temp;
}

bool equal(Money amount1, Money amount2)
{
return (amount1.all_cents == amount2.all_cents);
}

Money::Money(long dollars, int cents)
{
if(dollars*cents < 0) //If one is negative and one is positive

{
cout << "Illegal values for dollars and cents.\n";
exit(1);
}
all_cents = dollars*100 + cents;
}

Money::Money(long dollars) : all_cents(dollars*100)
{
//Body intentionally blank.
}

Money::Money( ) : all_cents(0)
{
//Body intentionally blank.
}

double Money::get_value( )
{
return (all_cents * 0.01);
}
//Uses iostream, cctype, cstdlib:
void Money::input(istream& ins)
{
char one_char, decimal_point,
digit1, digit2; //digits for the amount of cents
long dollars;
int cents;
bool negative;//set to true if input is negative.

ins >> one_char;
if (one_char == '-')
{
negative = true;
}
else
negative = false;
//if input is legal, then one_char == '\$'

ins >> dollars >> decimal_point >> digit1 >> digit2;

if ( one_char != '\$' || decimal_point != '.'
|| !isdigit(digit1) || !isdigit(digit2) )

{
cout << "Error illegal form for money input\n";
exit(1);
}
cents = digit_to_int(digit1)*10 + digit_to_int(digit2);

all_cents = dollars*100 + cents;
if (negative)
all_cents = -all_cents;
}

//Uses cstdlib and iostream:
void Money::output(ostream& outs)
{
long positive_cents, dollars, cents;
positive_cents = labs(all_cents);
dollars = positive_cents/100;
cents = positive_cents%100;

if (all_cents < 0)
outs << "-\$" << dollars << '.';
else
outs << "\$" << dollars << '.';

if (cents < 10)
outs << '0';
outs << cents;
}

int digit_to_int(char c)
{
return (static_cast<int>(c) - static_cast<int>('0') );
}

```

### The Class Money with Constant Parameters

```//DISPLAY 11.4 The Class Money with Constant Parameters
//Class for amounts of money in U.S. currency.
class Money
{
public:
friend Money add(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns the sum of the values of amount1 and amount2.
friend bool equal(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns true if amount1 and amount2 have the same value;
//otherwise, returns false.

Money(long dollars, int cents);
//Initializes the object so its value represents an amount with the
//dollars and cents given by the arguments. If the amount is negative,
//then both dollars and cents must be negative.

Money(long dollars);
//Initializes the object so its value represents \$dollars.00.

Money( );
//Initializes the object so its value represents \$0.00.

double get_value( ) const;
//Precondition: The calling object has been given a value.
//Returns the amount of money recorded in the data of the calling object.

void input(istream& ins);
//Precondition: If ins is a file input stream, then ins has already been
//connected to a file. An amount of money, including a dollar sign, has been
//entered in the input stream ins. Notation for negative amounts is -\$100.00.
//Postcondition: The value of the calling object has been set to
//the amount of money read from the input stream ins.

void output(ostream& outs) const;
//Precondition: If outs is a file output stream, then outs has already been
//connected to a file.
//Postcondition: A dollar sign and the amount of money recorded
//in the calling object have been sent to the output stream outs.
private:
long all_cents;
};

```

```//DISPLAY 11.5 Overloading Operators
//Program to demonstrate the class Money. (This is an improved version of
//the class Money that we gave in Display 11.3 and rewrote in Display 11.4.)
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

//Class for amounts of money in U.S. currency.
class Money
{
public:
friend Money operator +(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns the sum of the values of amount1 and amount2.

friend bool operator ==(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns true if amount1 and amount2 have the same value;
//otherwise, returns false.

Money(long dollars, int cents);

Money(long dollars);

Money( );

double get_value( ) const;

void input(istream& ins);

void output(ostream& outs) const;
private:
long all_cents;
};

//Any extra function declarations from Display 11.3 go here.

int main( )
{
Money cost(1, 50), tax(0, 15), total;
total = cost + tax;

cout << "cost = ";
cost.output(cout);
cout << endl;

cout << "tax = ";
tax.output(cout);
cout << endl;
cout << "total bill = ";
total.output(cout);
cout << endl;
if (cost == tax)
cout << "Move to another state.\n";
else
cout << "Things seem normal.\n";
return 0;
}

Money operator +(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents + amount2.all_cents;
return temp;
}

bool operator ==(const Money& amount1, const Money& amount2)
{
return (amount1.all_cents == amount2.all_cents);
}

//The definitions of the member functions are the same as in
//in various places so that the function headings match the function
//declarations in the preceding class definition. No other changes
//are needed in the member function definitions. The bodies of the
//member function definitions are identical to those in Display 11.3.
```

```//DISPLAY 11.6 Overloading a Unary Operator
//Class for amounts of money in U.S. currency.
class Money
{
public:
friend Money operator +(const Money& amount1, const Money& amount2);

friend Money operator -(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns amount 1 minus amount2.

friend Money operator -(const Money& amount);
//Precondition: amount has been given a value.
//Returns the negative of the value of amount.

friend bool operator ==(const Money& amount1, const Money& amount2);

Money(long dollars, int cents);

Money(long dollars);

Money( );

double get_value( ) const;

void input(istream& ins);
void output(ostream& outs) const;
private:
long all_cents;
};

//Any additional function declarations as well as the main part of the program go here.

Money operator -(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents - amount2.all_cents;
return temp;
}

Money operator -(const Money& amount)
{
Money temp;
temp.all_cents = -amount.all_cents;
return temp;
}
//The other function definitions are the same as in Display 11.5.

```

This topic: SCMP118/Fall2016 > WebHome > CodeClass3
Topic revision: r1 - 2015-11-19 - JimSkon

Copyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback