Here are two things you need to remember when you work with constructors:
1) A constructor MUST have the same name as the class.
The constructor for class Loan is called Loan.
2) A constructor DOES NOT have a type, thus it CANNOT
return a value.
In the Loan class, the set function can be replaced with the constructor as its job is to initialize the variable members of the class.
Here is the P12.1a.cpp program with constructors.
// P12.1a.cpp - This program is a driver
written to demonstrate how the constructor function works.
#include<iostream>
class Loan // Loan is called structure tag
{
public:
Loan( );
Loan(int ID, float
amount, float rate, int term);
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(1234, 2300, 5.5, 410); // initialize
to values given
Loan loan2; // use the default values
Loan loan3;
Loan loan4 = loan1;
cout << "Display loan1 \n";
loan1.display();
cout << "Display loan2 \n";
loan2.display();
cout << "Display loan4 \n";
loan4.display();
loan3.set( ); // set the values
cout << "Display loan3 \n";
loan3.display();
return 0;
}
Loan::Loan( )
{
// Constructor 1:
//
Body intentionally kept empty so the default values are used.
// If you wish to set the default value of the members to specific
// values, here is the place to do it.
// For example, to make the default value of amount = 0
// you will use amount = 0;
// You could actually copy the body of set
function here too
}
Loan::Loan(int I, float am, float rt, int trm)
{
// Constructor 2:
//
This constructor sets the values of the members to specific
// values that are passes by value in the parameters.
ID = I;
amount = am;
rate = rt;
term = trm;
}
void Loan::set( )
{
// Initialize the loan1 object
cout << "Enter the ID of this
loan \n";
cin >> ID;
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()
{
cout << ID << endl;
cout << amount << endl;
cout << rate << endl;
cout << term << endl;
}
Exercise 12.1
Modify the above program to include at least 2 constructors. Write a driver that creates 2 loans, prints the loans and the payment due. Use the newly defined constructors. Call the program ex121.cpp.