# Difference: Lab7Startup (1 vs. 3)

#### Revision 32018-03-29 - JimSkon

Line: 1 to 1

 META TOPICPARENT name="Fall2017"

# Classes and Objects with friends

### Goal

Enhance Date function with friend and operator methods

### Steps

Changed:
<
<
1. Start with the Date class developed previously in class. A copy is here : DateClass
2. Add a friend function "bool Equals(Date a, Date b)" that returns true if the dates are equal
3. Add a friend function "Date Add(Date d, int i) " that adds an integer i days to Date, and returns that date.
4. Add a operator "==" if the two are equal.
5. Add a "+" opperator that adds Date with an integer: d2 = d1 + 16;
>
>
1. Start with the Date class developed previously in class. A copy is here : DateClass

2. Add a friend function "bool Equals(Date a, Date b)" that returns true if the dates are equal

3. Can we do arithmetic with dates?

• Days between to dates?
• Add days to date to get new date
• check if a date is real
• what about leap year?
Review these algorithms on computing years with leap year: link

Algorithms converted to friend functions: link

Add a friend function "Date Add(Date d, int i) " that adds an integer i days to Date, and returns that date.
Add a operator "==" if the two are equal.
Add a "+" opperator that adds Date with an integer: d2 = d1 + 16;

#### Revision 22018-03-28 - JimSkon

Line: 1 to 1

 META TOPICPARENT name="Fall2017"
Changed:
<
<

# Classes and Objects Example

%CODE{"c++"}% //DISPLAY 10.6 /Program to demonstrate the class BankAccount. //Demonstrates an alternative implementation of the class BankAccount. #include using namespace std;
>
>

# Classes and Objects with friends

### Goal

Changed:
<
<
//Class for a bank account: class BankAccount { public: BankAccount(int dollars, int cents, double rate); //Initializes the account balance to \$dollars.cents and //initializes the interest rate to rate percent.

BankAccount(int dollars, double rate); //Initializes the account balance to \$dollars.00 and //initializes the interest rate to rate percent.

BankAccount( ); //Initializes the account balance to \$0.00 and the interest rate to 0.0%.

void update( ); //Postcondition: One year of simple interest has been added to the account //balance.

double get_balance( ); //Returns the current account balance.

double get_rate( ); //Returns the current account interest rate as a percentage.

void output(ostream& outs); //Precondition: If outs is a file output stream, then //outs has already been connected to a file. //Postcondition: Account balance and interest rate //have been written to the stream outs. private: double balance; double interest_rate; double fraction(double percent); //Converts a percentage to a fraction. For example, fraction(50.3) //returns 0.503.

};

int main( ) { BankAccount account1(100, 2.3), account2;

cout << "account1 initialized as follows:\n"; account1.output(cout); cout << "account2 initialized as follows:\n"; account2.output(cout);

account1 = BankAccount(999, 99, 5.5); cout << "account1 reset to the following:\n"; account1.output(cout); return 0; }

BankAccount::BankAccount(int dollars, int cents, double rate) { if ((dollars < 0) || (cents < 0) || (rate < 0)) { cout << "Illegal values for money or interest rate.\n"; } balance = dollars + 0.01*cents; interest_rate = rate; }

BankAccount::BankAccount(int dollars, double rate) { if ((dollars < 0) || (rate < 0)) { cout << "Illegal values for money or interest rate.\n"; } balance = dollars; interest_rate = rate; }

BankAccount::BankAccount( ) : balance(0), interest_rate(0.0) { //Body intentionally empty. }

//Uses iostream: void BankAccount::output(ostream& outs) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(2); outs << "Account balance \$" << balance << endl; outs << "Interest rate " << interest_rate << "%" << endl; }

%ENDCODE%

### Start Project 7

>
>
Enhance Date function with friend and operator methods

### Steps

1. Start with the Date class developed previously in class. A copy is here : DateClass
2. Add a friend function "bool Equals(Date a, Date b)" that returns true if the dates are equal
3. Add a friend function "Date Add(Date d, int i) " that adds an integer i days to Date, and returns that date.
4. Add a operator "==" if the two are equal.
5. Add a "+" opperator that adds Date with an integer: d2 = d1 + 16;

#### Revision 12017-10-23 - JimSkon

Line: 1 to 1
>
>
 META TOPICPARENT name="Fall2017"

# Classes and Objects Example

`<-- SyntaxHighlightingPlugin -->`
```//DISPLAY 10.6 /Program to demonstrate the class BankAccount.
//Demonstrates an alternative implementation of the class BankAccount.
#include <iostream>
using namespace std;

//Class for a bank account:
class BankAccount
{
public:
BankAccount(int dollars, int cents, double rate);
//Initializes the account balance to \$dollars.cents and
//initializes the interest rate to rate percent.

BankAccount(int dollars, double rate);
//Initializes the account balance to \$dollars.00 and
//initializes the interest rate to rate percent.

BankAccount( );
//Initializes the account balance to \$0.00 and the interest rate to 0.0%.

void update( );
//Postcondition: One year of simple interest has been added to the account
//balance.

double get_balance( );
//Returns the current account balance.

double get_rate( );
//Returns the current account interest rate as a percentage.

void output(ostream& outs);
//Precondition: If outs is a file output stream, then
//outs has already been connected to a file.
//Postcondition: Account balance and interest rate
//have been written to the stream outs.
private:
double balance;
double interest_rate;
double fraction(double percent);
//Converts a percentage to a fraction. For example, fraction(50.3)
//returns 0.503.

};

int main( )
{
BankAccount account1(100, 2.3), account2;

cout << "account1 initialized as follows:\n";
account1.output(cout);
cout << "account2 initialized as follows:\n";
account2.output(cout);

account1 = BankAccount(999, 99, 5.5);
cout << "account1 reset to the following:\n";
account1.output(cout);
return 0;
}

BankAccount::BankAccount(int dollars, int cents, double rate)
{
if ((dollars < 0) || (cents < 0) || (rate < 0))
{
cout << "Illegal values for money or interest rate.\n";
}
balance = dollars + 0.01*cents;
interest_rate = rate;
}

BankAccount::BankAccount(int dollars, double rate)
{
if ((dollars < 0) || (rate < 0))
{
cout << "Illegal values for money or interest rate.\n";
}
balance = dollars;
interest_rate = rate;
}

BankAccount::BankAccount( )  : balance(0), interest_rate(0.0)
{
//Body intentionally empty.
}

//Uses iostream:
void BankAccount::output(ostream& outs)
{
outs.setf(ios::fixed);
outs.setf(ios::showpoint);
outs.precision(2);
outs << "Account balance \$" << balance << endl;
outs << "Interest rate " << interest_rate << "%" << endl;
}
```
`<-- end SyntaxHighlightingPlugin -->`

### Start Project 7

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