Classes

solved, thanks
Last edited on
You need to create an instance of the class.

1
2
3
4
5
6
7
8
9
int main( int argc, char* argv[] )
{
   BankAccount acc( "123456", "I P Freely" );

   std::cout << "Account name: " << acc.getAccountName() << std::endl;
   std::cout << "Account no: " << acc.getAccountNumber() << std::endl;
   std::cout << "Security breach? Yep. ;-)" << std::endl;

}
1
2
3
void main()
{   BankAccount::getAccountNumber() = 5678; //this is the part i need help with
}


You can't assign a value to a function.
You want to set the account number, use the setter.

You also have not instantiated BankAccount.
Try this:
1
2
3
4
5
void main()
{   BankAccount  acct;

  acct.setAccountNumber ("5678");  
}

Note that setAccountNumber takes a string, not a number.




thanks for the reply hutch, sort of puzzled with what you did there, why is main being passed
int argc, char* argv[]?
the rest i sort of understand, thanks

p.s. i believe you dont need to spam std::, use "using namespace std;" then you can simply use cout << and cin >>.

thanks for the reply abstract, i noticed that too that it only takes a string, but that was the code we were giving to work with. so i automatically assumed that im in the wrong.

sort of confused now, after looking at your code abstract, i thought the getter. was used to get the data, then the setter sets it to the bank account. but if this is the case the getter isnt passed anything (not my original code).

can you plz explain the purpose of the getter and setter? if the data is going directly to the setter the getter seems pointless... unless the getter retrieves the data at another point in the program to work with it? i always thought the getter, gets the primary data
Last edited on
Pieface wrote:
why is main being passed int argc, char* argv[]?

Force of habit - they're a count and list of arguments passed into the program. You needn't worry about them just now - they don't affect your program at all. As I say, it's just habit.

Pieface wrote:
the rest i sort of understand, thanks

If there's anything that you're unsure of, please feel free to ask and I can explain.

Pieface wrote:
p.s. i believe you dont need to spam std::, use "using namespace std;" then you can simply use cout << and cin >>.

You can, yes, and in small programs it's usually fine to do so. In bigger programs that include multiple headers and make use of namespaces it could be a problem. Explicitly typing the namespace reduces chance of ambiguity (amongst other things) and, in my opinion, adds clarity. Again though, it's nothing you need to worry about at this stage. :-)
Last edited on
i understand this better now, i always thought that the getter gets the primary data then the setter sets it.. sort of misunderstood the lecture.
This is what ive got now, dont really understand what ive done wrong
1
2
3
4
5
6
7
8
9
10
11
12
void main()
{
	BankAccount acc;

	acc.setAccountNumber("5678");
	acc.setAccountName("Fireman Sam");
	acc.deposit(1500.00);

	cout << acc.getAccountName() << endl;
	cout << acc.getAccountNumber() << endl;
	cout << acc.getBalance() << endl;
}


gives a error, unresolved externals
Last edited on
What does the error say exactly?

At a guess, I'd say it's because you've declared the withdraw and getBalance functions but haven't implemented them.
erm.. yea that was pretty stupid of me.. get back to you in 5 haha,

yea i didnt have them in the program, i wrote them but commented them out so i could forget about them, but yea, its all working as it should now.

thanks guys!
Last edited on
Topic archived. No new replies allowed.