Impaler
08-02-2003, 07:41 PM
I am trying to do a mortgage loan principle amortization(fun fun) I thought I had the jist of it but I ran into a problem with my friend functions not able to access private members of my mortgage class? Anyway I need some help. I have the following
class Mortgage{
double principle;
double interest;
unsigned length;
double payment;
void compute();
public:
friend istream& operator>>(istream&, Mortgage&);
friend ostream& operator<<(ostream& ,const Mortgage&);
};
void Mortgage::compute(){
// must use pow() function
double x;
double inter1;
inter1 = interest + 1;
x = (principle * interest * inter1);
payment = pow(x,length) / pow(inter1,length - 1);
}
istream& operator>>(istream &stream, Mortgage &d){
cout << "Please enter priniple , interest rate , and length of loan in months: ";
stream >> d.principle >> d.interest >> d.length;
cout << endl;
d.compute();
return stream;
}
ostream& operator<<(ostream &stream, Mortgage &d){
//Mortgage temp = d;
double newprinc;
double monthint;
double princamt;
double totali;
int i;
cout.precision(2);
cout << "Principle.......: " << d.principle << endl;
cout.precision(4);
cout << "Interest rate...: " << d.interest * 100 << "%" << endl;
cout.precision(0);
cout << "Length of loan..: " << d.length << " months" << endl;
cout.precision(2);
cout << "Monthly payment.: " << d.payment << endl;
cout << endl;
newprinc = d.principle;
for(i=0;i<12;i++){
monthint = d.interest / d.length * newprinc;
totali = totali + monthint;
princamt = d.payment - monthint;
cout.precision(2);
cout << i << " " << newprinc << " "<< monthint << " " << princamt << " " << newprinc - princamt << endl;
newprinc = newprinc - princamt;
}
cout << endl;
cout.precision(2);
cout << "Total interest: " << totali << endl;
cout.precision(2);
cout << "Principle.....: " << d.principle << endl;
cout.precision(2);
cout << "Total paid....: " << d.principle + totali << endl;
return stream;
}
I thought friend functions were supposed to be able to access the class's private members. But unfortunately that is not what is happening for me
o yea and I'm not sure about the testing. in main do I say
Mortgage d;
cin >> d;
cout << d;
?? Anyway thanks.
class Mortgage{
double principle;
double interest;
unsigned length;
double payment;
void compute();
public:
friend istream& operator>>(istream&, Mortgage&);
friend ostream& operator<<(ostream& ,const Mortgage&);
};
void Mortgage::compute(){
// must use pow() function
double x;
double inter1;
inter1 = interest + 1;
x = (principle * interest * inter1);
payment = pow(x,length) / pow(inter1,length - 1);
}
istream& operator>>(istream &stream, Mortgage &d){
cout << "Please enter priniple , interest rate , and length of loan in months: ";
stream >> d.principle >> d.interest >> d.length;
cout << endl;
d.compute();
return stream;
}
ostream& operator<<(ostream &stream, Mortgage &d){
//Mortgage temp = d;
double newprinc;
double monthint;
double princamt;
double totali;
int i;
cout.precision(2);
cout << "Principle.......: " << d.principle << endl;
cout.precision(4);
cout << "Interest rate...: " << d.interest * 100 << "%" << endl;
cout.precision(0);
cout << "Length of loan..: " << d.length << " months" << endl;
cout.precision(2);
cout << "Monthly payment.: " << d.payment << endl;
cout << endl;
newprinc = d.principle;
for(i=0;i<12;i++){
monthint = d.interest / d.length * newprinc;
totali = totali + monthint;
princamt = d.payment - monthint;
cout.precision(2);
cout << i << " " << newprinc << " "<< monthint << " " << princamt << " " << newprinc - princamt << endl;
newprinc = newprinc - princamt;
}
cout << endl;
cout.precision(2);
cout << "Total interest: " << totali << endl;
cout.precision(2);
cout << "Principle.....: " << d.principle << endl;
cout.precision(2);
cout << "Total paid....: " << d.principle + totali << endl;
return stream;
}
I thought friend functions were supposed to be able to access the class's private members. But unfortunately that is not what is happening for me
o yea and I'm not sure about the testing. in main do I say
Mortgage d;
cin >> d;
cout << d;
?? Anyway thanks.
