You're still a little confused on classes.
Line 100: you are trying to use a member function without an object. You can't do that. You must
first create a variable (of
type 'Member'), then you can use the
variable's methods.
1 2
|
Member someone( "Justin Time", 19843, Date( 2009, 3, 31 ) );
someone.menu();
| |
Make sense?
constructors
A side note on constructors and variable names. A person should, just by looking at "
member.h", understand how to use each method and object in it.
Bad example: line 9. What exactly am I supposed to give the constructor? Make sure to give your arguments telling names.
Member( string name, int account_number, Date expiry_date );
Good example: line 12. Both the method and its argument are well-named. The method name expresses what the method does, and the argument name expresses what it does it with: makes a deposit of a specific amount.
Watch your capitalizations. Is it
Date
or
date
? etc. How does line 25 work? (Not like you think, I'm sure.)
You can make your constructors better (and cooler) by using initialization lists. You can also combine stuff using default arguments if you want. Finally, take const arguments whenever possible (it gives you cool superpowers when using the constructor).
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Member(
// The list of arguments with their default values
const string& name = "John Smith",
int account_number = 1000,
const Date& expiry_date = Date( "12/12/2001" )
):
// This is the initialization list, which initializes the
// object's fields using the constructor's arguments
name( name ),
accNum( account_number ),
expiryDate( expiry_date )
// Finally, there's no need to do anything in the body...
{ }
| |
pointers
If you plan to use pointers, you'll need to be familiar with the
new and
delete operators.
1 2 3 4 5 6 7 8 9 10 11
|
Member someone; // Actually creates a Member named "John Smith", acct 1000, etc.
Member* psomeone; // Only creates a pointer to a Member or one of its descendants
someone.menu(); // works fine
psomeone->menu(); // causes segmentation fault -- there is no Member here
psomeone = new JuniorMember; // now it points to an actual Member object
psomeone->menu();
delete psomeone; // destruct the Member object and free the memory it used
psomeone->menu(); // error again!
| |
vectors
Well, now to your question. Make sure to keep a reference handy.
This one is pretty good:
http://www.cplusplus.com/reference/stl/vector/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// To create a vector, declare it as any other variable.
vector <Member*> all_the_members;
// Add members using one of the insertion routines
all_the_members.push_back( new Member );
// Access the elements
all_the_members[ 0 ]->menu();
// Don't forget to free all memory before the program terminates
for (vector <Member*> member = all_the_members.begin();
member != all_the_members.end();
member++)
delete *member;
all_the_members.clear();
| |
Hope this helps.