WildARMs Posted September 21, 2004 Share Posted September 21, 2004 (edited) I've never written a class before and have been following as many examples as I can, but something is wrong. Anything obvious im missing? THanks in advance! class Account { public: void SetBalance(float balance); float Deposit(float d); float Withdrawal(float w); float GetBalance(); private: float balance; }; #include "Account.h" #include<iostream> void Account::SetBalance(float balance) { balance = bal; } float Account::Deposit(float d) { float d; balance = d + balance; return bal; } float Account::WithDrawal(float w) { float w; if (w <= balance) balance = balance - w; return 1; else return 0; } float Accoutn::GetBalance() { return balance; }; Edited September 21, 2004 by gameguy Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted September 21, 2004 Share Posted September 21, 2004 Incorrect: if (w <= balance) balance = balance - w; return 1; else return 0; Correct: if (w <= balance) { balance = balance - w; return 1; } else return 0; Link to comment Share on other sites More sharing options...
0 WildARMs Posted September 21, 2004 Author Share Posted September 21, 2004 Hey thanks! didnt notice that! Whenever I write a test program though it says that SetBalance does not take 0 parameters. Link to comment Share on other sites More sharing options...
0 HeartsOfWar Posted September 21, 2004 Share Posted September 21, 2004 SetBalance does not take 0 parameters this means you haven't passed the right amount of arguments - check your call statement and make sure you're passing a float. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted September 21, 2004 Share Posted September 21, 2004 void Account::SetBalance(float bal) //instead of float balance { balance = bal; } float Account::Deposit(float d) { float d; balance = d + balance; return balance; //instead of return bal } Link to comment Share on other sites More sharing options...
0 WildARMs Posted September 21, 2004 Author Share Posted September 21, 2004 So if i wanted to test the class, #include "Account.h" #include<iostream> int main() { using namespace std; Account b; b.SetBalance(float bal) return 0; } would that work to send a value in? Link to comment Share on other sites More sharing options...
0 John Veteran Posted September 21, 2004 Veteran Share Posted September 21, 2004 Title edited, code box added Link to comment Share on other sites More sharing options...
0 Endoscient Posted September 21, 2004 Share Posted September 21, 2004 well... it should work but there is no way to tell you what the balance is.... mkaing like a PrintBalance method so you can return the balance.... also u dont do b.SetBalance(float bal) more like: float newBal = 100.99; b.SetBalance(newBal); Link to comment Share on other sites More sharing options...
0 Andareed Posted September 21, 2004 Share Posted September 21, 2004 #include "Account.h" #include<iostream> using namespace std; int main() { Account b; float ba = 123; b.SetBalance(bal) return 0; } Link to comment Share on other sites More sharing options...
0 WildARMs Posted September 21, 2004 Author Share Posted September 21, 2004 well my prof is going to write code to test the class I wrote, so he should be able able to do it effectively. Thanks for the help guys!! I just want the class to work! Link to comment Share on other sites More sharing options...
Question
WildARMs
I've never written a class before and have been following as many examples as I can, but something is wrong. Anything obvious im missing? THanks in advance!
class Account { public: void SetBalance(float balance); float Deposit(float d); float Withdrawal(float w); float GetBalance(); private: float balance; }; #include "Account.h" #include<iostream> void Account::SetBalance(float balance) { balance = bal; } float Account::Deposit(float d) { float d; balance = d + balance; return bal; } float Account::WithDrawal(float w) { float w; if (w <= balance) balance = balance - w; return 1; else return 0; } float Accoutn::GetBalance() { return balance; };Edited by gameguyLink to comment
Share on other sites
9 answers to this question
Recommended Posts