Question: link http://cs.ucsb.edu/~cspensky/teaching/cs16/cs16/SavitchCPP9_LabManual/lab6/act2.html#ex62 I need to do exercise 6.2 Exercise 6.2 Complete program P62.cpp by defining the member functions display and payment for class loan. Note
link http://cs.ucsb.edu/~cspensky/teaching/cs16/cs16/SavitchCPP9_LabManual/lab6/act2.html#ex62
I need to do exercise 6.2
Exercise 6.2 Complete program P62.cpp by defining the member functions display and payment for class loan. Note that the display function will display all the information about a loan, i.e.,
ID:
Amount: Rate:
Term:
You will display the monthly payment in the main by assigning the returned value from the payment function to a variable of type float. Suppose, in the main we have declared:
..... float p;
Loan loan1;
loan1.set( );
..... loan1.display( ) // will display the data members of loan1
p = loan1.payment( ) // will return the monthly payment of loan1 .....
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
// P62.cpp - This program uses a class for a loan, initializes it from the keyboard, then // displays the class
#include
class Loan // loan class definition { public: int 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 };
float payment(Loan l);
int main( ) { Loan loan1; float monthly_payment;
// Initialize the loan1 object cout << "Enter the ID of this loan "; cin >> loan1.ID;
cout << "Enter the amount of this loan "; cin >> loan1.amount;
cout << "Enter the annual interest rate of this loan (in %) "; cin >> loan1.rate;
cout << "Enter the term (number of months, length of the loan) "; cin >> loan1.term;
monthly_payment = payment(loan1);
cout << "The monthly payment for loan " << loan1.ID << " is: " << monthly_payment << endl;
return 0; }
float payment(Loan l) { l.rate = l.rate/1200; // To convert % yearly rate to monthly fraction return l.amount*l.rate*( pow( (l.rate+1), l.term)/ (pow( (l.rate+1), l.term) - 1) ); }
// P62a.cpp - This program is a driver written to demonstrate how the set function works. #include
class Loan // Loan is called structure tag { public: void set( ); float payment( ); void display( ); private: int 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;
loan1.set( );
return 0; }
void Loan::set( ) { // Initialize the loan1 object cout << "Enter the ID of this loan "; cin >> ID;
cout << "Enter the amount of this loan "; cin >> amount;
cout << "Enter the annual interest rate of this loan (in %) "; cin >> rate;
cout << "Enter the term (number of months, length of the loan) "; cin >> term; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
