Practical Learning: Using a Pointer to Function
|
- To declare and use a pointer to function, change the Main.cpp file as follows:
#include <iostream>
using namespace std;
#include "Loan.h"
double GetPrincipal();
double GetInterestRate();
double GetPeriod(int &PeriodType, double &NumberOfPeriods);
double Addition(double Value1, double Value2)
{
return Value1 + Value2;
}
int main()
{
double (*AddValues)(double R, double T);
double Principal, IntRate, Period, InterestAmount;
int TypeOfPeriod;
double Periods;
string PeriodName;
cout << "This program allows you to calculate the amount of money a "
<< "customer will owe at the end of the lifetime of a loan\n";
cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
cout << "\nLoan Processing\n";
Principal = GetPrincipal();
IntRate = GetInterestRate();
Period = GetPeriod(TypeOfPeriod, Periods);
AddValues = Addition;
InterestAmount = Finance::InterestAmount(Principal, IntRate, Period);
double Amount = AddValues(Principal, InterestAmount);
if( TypeOfPeriod == 0 )
{
// Since the user made a bad selection, stop the program here
cout << "Press any key to stop...";
return 0;
}
if( TypeOfPeriod == 1 )
{
PeriodName = " Days";
}
else if( TypeOfPeriod == 2 )
{
PeriodName = " Months";
}
else if( TypeOfPeriod == 3 )
{
PeriodName = " Years";
}
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
cout << "\n==================================";
cout << "\nEstimate on loan";
cout << "\n----------------------------------";
cout << "\nPrincipal: $" << Principal;
cout << "\nInterest: " << IntRate << "%";
cout << "\nPeriod: " << Periods << PeriodName;
cout << "\n--------------------------------";
cout << "\nInterest paid on Loan: $" << InterestAmount;
cout << "\nTotal Amount Paid: $" << Amount;
cout << "\n==================================\n";
return 0;
}
. . . No Change
|
- Test the program. Here is an example:
This program allows you to calculate the amount of money a customer will owe at
the end of the lifetime of a loan
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Loan Processing
Enter the Principal: $1500
Enter the Interest Rate (%): 12.55
How do you want to enter the length of time?
1 - In Days
2 - In Months
3 - In Years
Your Choice: 3
Enter the number of years: 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
==================================
Estimate on loan
----------------------------------
Principal: $1500
Interest: 12.55%
Period: 2 Years
--------------------------------
Interest paid on Loan: $376.5
Total Amount Paid: $1876.5
==================================
Press any key to continue...
|
- Return to your programming environment
- To create a programmer-define type based on the AddValues pointer to function name, click the Loan.h tab and create the type as follows:
#ifndef LoanH
#define LoanH
namespace Finance
{
typedef double (*Add)(double R, double T);
double InterestAmount(double P, double r, double t);
}
#endif
|
- To declare and use a variable of the new defined type, change the Main.cpp file as follows:
#include <iostream>
using namespace std;
#include "Loan.h"
double GetPrincipal();
double GetInterestRate();
double GetPeriod(int &PeriodType, double &NumberOfPeriods);
double Addition(double Value1, double Value2)
{
return Value1 + Value2;
}
int main()
{
Finance::Add Plus;
double Principal, IntRate, Period, InterestAmount;
int TypeOfPeriod;
double Periods;
string PeriodName;
cout << "This program allows you to calculate the amount of money a "
<< "customer will owe at the end of the lifetime of a loan\n";
cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
cout << "\nLoan Processing\n";
Principal = GetPrincipal();
IntRate = GetInterestRate();
Period = GetPeriod(TypeOfPeriod, Periods);
Plus = TotalAmount;
InterestAmount = Finance::InterestAmount(Principal, IntRate, Period);
double Amount = Plus(Principal, InterestAmount);
if( TypeOfPeriod == 0 )
{
// Since the user made a bad selection, stop the program here
cout << "Press any key to stop...";
return 0;
}
. . . No Change
return 0;
}
. . .
double Circumference(double (*FDiam)(double R), double Rad)
{
double Circf;
const double PI = 3.14159;
Circf = (*FDiam)(Rad);
return Circf * PI;
}
#include <iostream>
using namespace std;
double Diameter(double);
double Circumference(double (*D)(double R), double r);
int main()
{
double Radius;
Radius = 25.52;
cout << "Cylinder Summary";
cout << "\nRadius: " << Radius;
cout << "\nCircumference = " << Circumference(Diameter, Radius) << endl;
return 0;
}
double Diameter(double Rad)
{
return Rad * 2;
}
double Circumference(double (*FDiam)(double R), double Rad)
{
double Circf;
const double PI = 3.14159;
Circf = (*FDiam)(Rad);
return Circf * PI;
}
This would produce:
Cylinder Summary
Radius: 25.52
Circumference = 160.347
Press any key to continue...
double Circumference(double (*FDiam)(double R), double Rad)
{
const double PI = 3.14159;
return (*FDiam)(Rad) * PI;
}
|
Practical Learning: Passing a Function as Argument
|
- To declare a function that takes another function as argument, in the Loan.h file, declare the following function:
#ifndef LoanH
#define LoanH
namespace Finance
{
typedef double (*Add)(double R, double T);
typedef double (*Subtract)(double First, double Second);
typedef double (*Multiply)(double First, double Second);
typedef double (*Divide)(double First, double Second);
double InterestAmount(double P, double r, double t);
double Rate(double (*AP)(double A, double P),
double a, double p, double t);
}
#endif
|
- In the Loan.cpp, implement the function as follows:
#include "Loan.h"
#pragma package(smart_init)
namespace Finance
{
// Interest = Principal * rate * time in years
double InterestAmount(double P, double r, double t)
{
return P * (r / 100) * t;
}
double Rate(double (*AP)(double A, double P),
double a, double p, double t)
{
double AMinusP = (*AP)(a, p);
double Pt = p * t;
return (AMinusP / Pt) * 100;
}
}
|
- To provide a function used to get the future value of a loan, in the Main.h file, define a GetAmount() function as follows:
#if !defined MainH
#define MainH
. . . No Change
namespace LoanProcessing
{
double GetAmount()
{
double A;
cout << "Enter the future value: $"; cin >> A;
return A;
}
. . . No Change
#endif // MainH
|
- To call the new function, change the Main.cpp file as follows:
#include <iostream>
#include "Main.h"
#include "Loan.h"
using namespace std;
using namespace Accessories;
int main()
{
double Principal, Amount, IntRate, Period, InterestAmount;
int TypeOfPeriod;
double Periods;
string PeriodName;
cout << "This program allows you to perform estimations on loans\n";
cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
cout << "\nLoan Processing\n";
Amount = LoanProcessing::GetAmount();
Principal = LoanProcessing::GetPrincipal();
Period = LoanProcessing::GetPeriod(TypeOfPeriod, Periods);
IntRate = Finance::Rate(Subtraction, Amount, Principal, Period);
if( TypeOfPeriod == 0 )
{
// Since the user made a bad selection, stop the program here
cout << "Press any key to stop...";
return 0;
}
if( TypeOfPeriod == 1 )
{
PeriodName = " Days";
}
else if( TypeOfPeriod == 2 )
{
PeriodName = " Months";
}
else if( TypeOfPeriod == 3 )
{
PeriodName = " Years";
}
cout << "\n==================================";
cout << "\nEstimate on loan";
cout << "\n----------------------------------";
cout << "\nFuture Value: $" << Amount;
cout << "\nPrincipal: $" << Principal;
cout << "\nPeriod: " << Periods << PeriodName;
cout << "\n--------------------------------";
cout << "\nInterest on Loan: " << IntRate << "%";
cout << "\n==================================\n";
return 0;
}
|
- Test the application. Here is an example:
This program allows you to perform estimations on loans
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Loan Processing
Enter the future value: $4500
Enter the Principal: $3000
How do you want to enter the length of time?
1 - In Days
2 - In Months
3 - In Years
Your Choice: 3
Enter the number of years: 3
==================================
Estimate on loan
----------------------------------
Future Value: $4500
Principal: $3000
Period: 3 Years
--------------------------------
Interest on Loan: 16.6667%
==================================
Press any key to continue...
|
- To provide more options to the user and make the program more complete, you can create a menu, allowing the clerk to select the type of calculation. A customer may want to know how much time would be a better length of time to pay a loan. The customer may want to find the differences among the time (period), the rate of interest, the monthly payment the customer can afford, the length of time (the number of days, months or years the customer wants to pay the loan.
To allow the user to select the type of calculation to perform, in the Main.h file, define the following functions:
#if !defined MainH
#define MainH
#include <iostream>
#include "Loan.h"
using namespace std;
namespace Accessories
{
// Accessory Functions
// This function adds two values
double Addition(double Value1, double Value2)
{
return Value1 + Value2;
}
// This function takes two arguments.
// It subtracts the second from the first
double Subtraction(double Value1, double Value2)
{
return Value1 - Value2;
}
// This function multiplies two numbers
double Multiplication(double Value1, double Value2)
{
return Value1 * Value2;
}
// This function takes two arguments
// If the second argument is 0, the function returns 0
// Otherwise, the first argument is divided by the second
double Division(double Numerator, double Denominator)
{
if( Denominator == 0 )
return 0;
// else is implied
return Numerator / Denominator;
}
} // namespace Accessories
namespace LoanProcessing
{
double GetAmount()
{
double A;
cout << "Enter the future value: $"; cin >> A;
return A;
}
double GetPrincipal()
{
double P;
cout << "Enter the Principal: $"; cin >> P;
return P;
}
double GetInterestRate()
{
double r;
cout << "Enter the Interest Rate (%): "; cin >> r;
return r;
}
double GetPeriod(int &TypeOfPeriod, double &Periods)
{
cout << "How do you want to enter the length of time?";
cout << "\n1 - In Days";
cout << "\n2 - In Months";
cout << "\n3 - In Years";
cout << "\nYour Choice: "; cin >> TypeOfPeriod;
if( TypeOfPeriod == 1 )
{
cout << "Enter the number of days: "; cin >> Periods;
return static_cast<double>(Periods / 360);
}
else if( TypeOfPeriod == 2 )
{
cout << "Enter the number of months: "; cin >> Periods;
return static_cast<double>(Periods / 12);
}
else if( TypeOfPeriod == 3 )
{
cout << "Enter the number of years: "; cin >> Periods;
return static_cast<double>(Periods);
}
else
{
TypeOfPeriod = 0;
// The user made an invalid selection. So, we will give up
cout << "\nBad Selection\n";
return 0.00;
}
}
int SelectCalculationType()
{
int Answer;
cout << "What kind of value do you want to estimate?";
cout << "\n1 - Calculate (only) the interest paid on the loan";
cout << "\n2 - Estimate the future value of the entire loan";
cout << "\nYour choice: ";
cin >> Answer;
return Answer;
}
void ProcessInterestAmount()
{
double Principal, IntRate, Period, InterestAmount;
int TypeOfPeriod;
double Periods;
string PeriodName;
cout << "\nWe will calculate the interest amount payed on a loan\n";
cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
cout << "\nLoan Processing\n";
Principal = LoanProcessing::GetPrincipal();
IntRate = LoanProcessing::GetInterestRate();
Period = LoanProcessing::GetPeriod(TypeOfPeriod, Periods);
InterestAmount = Finance::InterestAmount(Principal, IntRate, Period);
if( TypeOfPeriod == 0 )
{
// Since the user made a bad selection, stop the program here
cout << "Press any key to stop...";
return;
}
if( TypeOfPeriod == 1 )
{
PeriodName = " Days";
}
else if( TypeOfPeriod == 2 )
{
PeriodName = " Months";
}
else if( TypeOfPeriod == 3 )
{
PeriodName = " Years";
}
cout << "\n";
cout << "\n==================================";
cout << "\nEstimate on loan";
cout << "\n----------------------------------";
cout << "\nPrincipal: $" << Principal;
cout << "\nInterest: " << IntRate << "%";
cout << "\nPeriod: " << Periods << PeriodName;
cout << "\n--------------------------------";
cout << "\nInterest paid on Loan: $" << InterestAmount;
cout << "\n==================================\n";
}
void ProcessRateOfInterest()
{
double Principal, Amount, IntRate, Period;
int TypeOfPeriod;
double Periods;
string PeriodName;
cout << "\nWe will calculate the interest rate applied on a loan\n";
cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
cout << "\nLoan Processing\n";
Amount = LoanProcessing::GetAmount();
Principal = LoanProcessing::GetPrincipal();
Period = LoanProcessing::GetPeriod(TypeOfPeriod, Periods);
IntRate = Finance::Rate(Accessories::Subtraction, Amount, Principal, Period);
if( TypeOfPeriod == 0 )
{
// Since the user made a bad selection, stop the program here
cout << "Press any key to stop...";
return;
}
if( TypeOfPeriod == 1 )
{
PeriodName = " Days";
}
else if( TypeOfPeriod == 2 )
{
PeriodName = " Months";
}
else if( TypeOfPeriod == 3 )
{
PeriodName = " Years";
}
cout << "\n==================================";
cout << "\nEstimate on loan";
cout << "\n----------------------------------";
cout << "\nFuture Value: $" << Amount;
cout << "\nPrincipal: $" << Principal;
cout << "\nPeriod: " << Periods << PeriodName;
cout << "\n--------------------------------";
cout << "\nInterest on Loan: " << IntRate << "%";
cout << "\n==================================\n";
}
}
#endif // MainH
1. To test the new version of the program, change the Main.cpp file as follows:
2. Test the application and return to your programming environment
3. To allow the clerk to process other types of calculations, in the Loan.h file, declare the following functions:
4. In the Loan.cpp file, implement the functions:
5. To expand the menu, change the Main.h file accordingly:
6. To prepare for a test, in the Main.cpp, add the new options in the main() function:
1. Test the application. Here is an example:
2. Return to your programming environment and save everything
double Diameter(double Radius)
{
return Radius * 2;
}
double Circumference(double Radius)
{
return Diameter(Radius) * PI;
}
double Area(double Radius)
{
return Radius * Radius * PI;
}
#include <iostream>
using namespace std;
const double PI = 3.14159;
double Diameter(double Radius)
{
return Radius * 2;
}
double Circumference(double Radius)
{
return Diameter(Radius) * PI;
}
double Area(double Radius)
{
return Radius * Radius * PI;
}
int main()
{
typedef double (*Measure)(double R);
double R = 12.55;
Measure Calc[] = { Diameter, Circumference, Area };
double D = Calc[0](R);
double C = Calc[1](R);
double A = Calc[2](R);
cout << "Circle Characteristics";
cout << "\nDiameter: " << D;
cout << "\nCircumference: " << C;
cout << "\nArea: " << A << endl;
return 0;
}
This would produce:
Circle Characteristics
Diameter: 25.1
Circumference: 78.8539
Area: 494.808
Press any key to continue...
|
0 comments:
Post a Comment