Few things going wrong here. First of all, your first file there where you have main is not called a base class. Base classes are classes like your Final class but used to define your Final class using the
class Final : yourbaseclass. The proper term for you first file there is a test driver program. A program used to test the functionality of your Final class. So as of right now you have no inheritance going on and that is probably good because you had some issues with your Final class.
Below is your Final class with some changes.
1) Your calculate_loan no longer takes a parameter. This is because you were misusing the parameter to begin with and all you were doing in that function is returning the value of a calculation... so I removed it and returned the answer of the calculation directly.
2) Related to number 1 above, I also altered the signature for that function int the class definition.
3) Your calculate loan function was attempting to use variables not defined in the class or passed to the function. Remember this function can't see i or n or Amt. It sees the private member variables... Amount, monthly_rate, Annual_rate etc. So you have to alter the equation to take those into account. I replaced your "i" with monthly_rate and your "Amt" with "Amount" and your "n" with "term".
All this is put in a file I called "final.h"
cpp
#include <iostream>
#include <math.h>
using namespace std;
class Final
{
public:
Final();
Final(float, float, float, float);
// Calculate loan takes no parameter
float calculate_loan();
void display_loan();
private:
// These are the ones seen by the methods of Final
// Not i or n or Amt
float Amount;
float Annual_rate;
float monthly_rate;
float term;
};
Final::Final(float AMT, float ar, float I, float N)
{
// Set your private members
Amount = AMT;
Annual_rate = ar;
monthly_rate = I;
term = N;
}
float Final::calculate_loan()
{
// Notice here we use the class' data members
return (monthly_rate +(monthly_rate/((pow((1 + monthly_rate),term))-1))) * Amount;
}
void Final::display_loan()
{
cout << Amount << "," << Annual_rate << "," << term << endl;
}
Read the in line comments to see what has been changed and why. Now for your driver program.
1) You include the Final.h class in your driver program at the top.
2) You then create an instance of the class giving it the input to initialize the class. Once you do this, you have a pointer to the class which you then use to call its methods and do your calculations.
3) Since display_loan() was only printing, it can't be part of a cout << redirection statement. So you have to split it to another line.
cpp
#include <iostream>
#include "final.h"
#include <math.h>
using namespace std;
void main ()
{
float Amt, AR, i, n;
cout << "Enter the amount of money borrowed ";
cin >> Amt;
cout << "Enter the annual interest rate in percentage form ";
cin >> AR;
i = AR/1200;
cout << "Enter the term in months ";
cin >> n;
// Notice we create an instance of the Final class
// We pass the class the input to initialize the class with
// We then have a pointer which we use to call methods using
// the operator "->"
Final *Loan = new Final(Amt, AR, i, n);
cout << "The monthly payment of your loan is " << Loan->calculate_loan() << endl;
cout << "The specifications you entered of the Amount, Annual Rate, and Term are "<< endl;
// Since Display_loan() prints, it can't be part of the other Cout statement.
// So we print using a call and then start another cout underneath.
Loan->display_loan();
cout << "respectively" << endl;
}
So again take a look at the in-code comments and see what has been changed. I think once you see what I have done here, it will start making sense to you.
Now if you want to actually do some inheritance, you will have to create another class which derives from Final. You can do this by declaring your other class using the format I showed earlier
class mynewfinal : Final. As you can see we create a new class called "mynewfinal" and derive it from Final. Final is then acting as your base class.
Read up on base classes and deriving from base classes if this is still a bit confusing.
I hope this has been a help. After you make the changes, I believe your formula will be kicking out valid answers.
Enjoy!
"At DIC we be final class altering code ninjas! Altering classes to be oh so more sexy!"