c++ newbie needs help...

thesavage

New member
i thought i knew a lot about computers so i took AP Computer Science but now i realize that i'm really dumb and i have no idea what the f*ck's going on in class...

can somebody please explain to me what a class is and what the class function does? oh yeah and what's object oriented programming (that's what we started the first day of school on :()
 
A class is best described as a 'user defined type with an attitude'.

A 'standard' type is like INTEGER. When you define a variable as INTEGER, it is allotted 2 bytes in memory, and store a value from -32??? to 32???. (I forget the exact number -- it matters not)

A 'user defined type' is like a record. A single type that contains multiple fields. Think of it like a form you fill out... there are lots of different 'variables' on the sheet, but hundreds of the forms are all the same. SO instead of having an array for each of the fields, you have a user defined type that contains them all and then have an array of that type.

A class is sort of like that, but in addition to containing data, it can also contain CODE. It's hard to really explain well, you basically have to try it, and then you realise how much incredible potential it has. Say the record type is information for a bank account. With a class, you could make that information private to the class so that other parts of the program can't access it. Then you have a procedure called 'deposit' and another called 'withdraw' that take money in and out, and the withdrawl procedure checks if there's enough left, and returns an error message if there isn't.

It's called 'safe' programming. If you let any part of your program just screw with the values in the bank account information, a mistake could mean you get a negative balance.

That's a lousy example, because you're probably not writing banking machines in your compsci class... but it's useful for other things too. It's just good practice to have the class itself checking its variables and doing processing that pertains to itself, as opposed to having that code clog up other parts of your program.
 
Actually, the banking example is not a bad analogy of what a class is.

For a class, you need to things, something to store the data (array, pointers, int, char, etc) and a way to manipulate the data -- class functions.

Expanding on what wizardlife said, essentially, you don't need to know anything about the inner workings of the function for it to work. Using the bank example... let's say we have a Account class, which contained the member functions Deposit(amount); getBalance(); and Withdraw(amount); With this information we can go and start using the class, similar to this:
Account myAccount(1000);
cout << myAccount.Deposit(250) << endl
<< myAccount.getBalance << endl
<< myAccount.Withdraw(-500) << endl
<< myAccount.getBalance << endl;

This really simplies programming in groups... you'd dont need to know how the functions work, just need to know how to use them. Also, as long as the signature of the class functions don't change, you can make all the changes you way the functions actually work without affecting the client programmer -- (say you wanted that Withdraw(-500) to produce a new error message, all u'd have to do is change Withdraw(amount) and nothing else.
 
I'm in AP computer science also, but we're not on classes yet. the largest value an integet can have is 32767, I think the lowest is -32766
 
Back
Top