Classes

This may be a dumb question, but I don't see the point of classes. I know that inheritance would help alot with coding time and what not, but other than that I don't get the purpose. Can someone explain them to me, or give some examples when they would be "useful"
Lumpkin wrote:
http://img689.imageshack.us/img689/4742/9h2u.png


You know what's more useful than posting pictures of messages on the forum you're frequenting? Posting links to the actual message so that the context of the message and any accompanying discussion is available.
^ made me lol(it was more of a chuckle really but nonetheless)
To make code less messy.
While you are with console,they are useless ,but when you start creating big projects,you will definitely need them
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.
Last edited on
Topic archived. No new replies allowed.