class ID
{
public:
ID( );
ID(int, int, int);
private:
int left;
int middle;
int right;
};
Now we can use this in the Loan class definition.
class Loan
{
public:
Loan( );
Loan(ID id, float
amount, float rate, int term);
void set( );
float payment(
);
void display( );
private:
ID id; // assume an unique
integer between 1111-9999
float amount; // $ amount of
the loan
float rate; // annual interest rate
int term; // number of months,
length of the loan
};
The following program is very similar to the one you used in Lab 3, P63.cpp. The difference is that ID now is defined as a class.
// P84.cpp - This program is a driver written to demonstrate how we
can use a
// class inside another one.
#include<iostream>
using namespace std;
class ID
{
public:
ID( );
ID(int, int, int);
void display();
private:
int left;
int middle;
int right;
};
class Loan
{
public:
Loan( );
Loan(ID id, float amount, float rate, int
term);
void set( );
float payment( );
void display( );
private:
ID id; // assume an unique
integer between 1111-9999
float amount; // $ amount of the
loan
float rate; // annual interest rate
int term; // number of months,
length of the loan
};
int main( )
{
Loan loan1(ID(111,22,4444), 2300, 5.5, 48);
// initialize to values given
Loan loan2;
cout << "Display loan1 \n";
loan1.display();
loan2.set( ); // set the values
cout << "Display loan2 \n";
loan2.display();
return 0;
}
ID::ID( )
{
// use default values
}
ID::ID(int l, int m, int r)
{
left = l;
middle = m;
right = r;
}
void ID::display()
{
cout << right << "-" <<
middle << "-" << right << endl;
}
Loan::Loan( )
{
}
Loan::Loan(ID I, float am, float rt, int trm)
{
id = I;
amount = am;
rate = rt;
term = trm;
}
void Loan::set( )
{
int l, m, r;
ID temp_id;
// Initialize the loan1 object
cout << "Enter the left part
of the loan ID \n";
cin >> l;
cout << "Enter the middle part
of the loan ID \n";
cin >> m;
cout << "Enter the right part
of the loan ID \n";
cin >> r;
id = ID(l, m, r);
cout << "Enter the amount of
this loan \n";
cin >> amount;
cout << "Enter the annual interest
rate of this loan (in %) \n";
cin >> rate;
cout << "Enter the term (number
of months, length of the loan) \n";
cin >> term;
}
void Loan::display()
{
id.display();
cout << amount << endl;
cout << rate << endl;
cout << term << endl;
}
Exercise 8.4
Use operator overloading to overload == such that every time you run
the program, it will tell you whether the two loans are the same, i.e.
ID, amount, rate, and term are the same for both.