Game Generation Forums  

Home Register FAQ Members List Calendar Today's Posts






View Full Version : again C++ help?


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.

Null_Vector
08-02-2003, 08:24 PM
whats the difference between

friend ostream& operator<<(ostream& ,const Mortgage&);

and

ostream& operator<<(ostream &stream, Mortgage &d){

?

Misanthrope
08-03-2003, 03:57 AM
A friend function is used for functions that are not a part from a certain class but that need access to private and protected members of that class.

In this case, the methodes should be public, cause you're using the scope ( :: ) thing to define methodes of your class. You would use friend only when functions aren't a part of thet class.

Get it?

BTW: I haven't seen many friend functions in the wild. ;)

Impaler
08-03-2003, 01:57 PM
yea thats just how he wanted us to do the assignment(friend). I was missing the const I didnt see that. He gave us the formula to calculate pay but said we had to use the pow() function but I keep getting a scxrewed up number in my output But in debug everything has right data except the payment. I tried his formula on a calculator and it DOESNT work there either. Do you know the right formula. Everday this teacher tells us he doesnt know c++ and he always asks us questions about it like has any of you read in the chapter wether or not you can do this I wish I had taken it with a different teacher. we cant ask him for help at all, when I do he always says he will get back to me but never does or he suggests to look on the internet.





- Modified by Octane Software Development | More vB Archives