I remember I used to think the same thing: "Why use classes? Just the same variables contained under a name...".
So,
consider this:
You want to write a checkbook. Well, what goes in a checkbook?
1. A transaction
2. A time of the transaction
3. the initial deposite
4. the name of the checkbook (we may end up createing more than one!)
5. a unique id for this checkbook (if we create more than 1, then this is fool proof identification)
alright... so we have the data. We will be able to make more than 1, be able to save it, be able to make a transaction, be able... to DO THIS DO THAT...
Now mabey you understand: Classes allow us to
combine data and what we
do with that data. It helps us to simplify coding by allowing us to forget about what arguments to pass.
So, mabey instead of this:
1 2 3
|
void make_transaction(vector<string>& names, vector<money>& transactions,
vector<ID_T>& ids, const string& name,
vector<string>& description, vector<chrono_date>& times)
|
It would just look so much nicer like this:
1 2
|
check_book account = "whatever account file you want to load";
account.trans(-5); //withdraw $5.00
|
The benefits to this are also that we can treat it as an object as well:
1 2 3 4 5 6 7 8 9 10 11 12
|
vector<check_book> your_check_books; //no check books
//get the list of valid saves
vector<string> files = get_saved_check_books();
//load them all
for(vector<string>::const_iterator it = files.begin(); it != files.end(); ++it)
{
your_check_books.push_back(check_book()); //create the new object
your_check_books.back() = *it; //set it equal to the file
your_check_books.back().load(); //load the file
}
|
It will make a lot more sense when you start working with a large amount of data. I typically make the
load()
function a public member so that I can optionally load, but that's a different discussion for a different time.
Hope this helps! Good luck to you in the future.