A Conversation for The H2G2 Programmers' Corner
A bit of C++ help please?
Sue Started conversation Jul 8, 2004
Fool that I am, I'm doing a course in C++ with the Open University - I don't want to be a programmer, I just like to dabble in things just enough to find out how they work . The first half of the course was fine - as boring as it could possibly get, but I understood what was going on - that bit was all console applications with if steps and for loops and stuff like that. Then the course does a huge swerve and goes onto objects - and at that point I got lost.
I've had dozens of people try to explain different bits of it to me - I've now arrived at the conclusion that I'm thick! It seems to be something that either you understand or not - and those that do understand have trouble explaining in terms my thick old grey matter can cope with. I point blank refuse to give up, at some point I'm going to get one of those lightbulb moments when everything suddenly becomes clear... eventually.
Can anybody recommend some good reading - books or websites - that will guide a programming thickie like me through the real basics? Everything I can find seems to start at a point that already has me lost .
Please help me save my sanity, my course fee and 6 months hard work!
Sue
A bit of C++ help please?
Dark Master - The end is now (2005/03/01) Officially Left Posted Jul 8, 2004
Basics of Object Oriented Programming
Its easiest if I explain this in terms of the cookie example i learnt it from.
A class is like a Cookie Cutter, a template if you like.
An Object is like an individual Cookie, cut from the template. Every Cookie will be identical (theoretically)
In programming each Object is called an "Instance" of the class it was created from.
You can create several Instances of a class, just as you can create several cookies. All initially Identical.
Objects have properties, for example, a Cookie could have a property "Sprinkles" which describes whether the cookie has sprinkles or not and makes it different from the default cookie that is cut.
Properties of Objects can be assigned a value different to the default one. The same way you would assign a value to a variable.
If you are dealing with multiple Instances of a Class, each Instance keeps a different set of properties with its own values. The same way one cookie can have sprinkles and another one might not, yet they are still made with the same Cookie Cutter.
Objects can also have methods. Methods are predefined section of code that can be executed by caling them. For example, A Cookie might have the method "Break" which would cause it to break.
Obviously this is not an exhaustive explanation. Just the very basics. Hope it helps.
Dark Msster
A bit of C++ help please?
Crazy Man Posted Jul 8, 2004
There's an excellent OOP (Object Oriented Programming) tutorial, however it is not in C++. http://wiki.beyondunreal.com/wiki/Object_Oriented_Programming_Overview
Let me translate (feel free to correct me if you think I'm wrong):
class Pizza : public Meal //a pizza is a meal
{
/ *by default these variables are private, since they don't need to be modified outside the class */
int NumberofParts; //each pizza has a number of slices
bool HasSauce; //does the pizza have sauce?
bool HasCheese; //does the pizza have cheese?
public: // the functions you can use outside the class
int number; //you'll see this later...
void AddSauce() //self explanatory
{
HasSauce = true;
}
void EatSauce() //see previous comment
{
HasSauce = false;
}
void AddCheese()
{
HasCheese = true;
}
void EatCheese()
{
HasCheese = false;
}
void EatParts(int number) //eating pizza slices
{
if(NumberOfParts < number)
NumberOfParts = 0;
else
NumberOfParts -= number;
if(NumberOfParts <= 0)
Destroy(); /* abitrary function just like Meal is an arbitrary class, only for tuturial purposes */
}
}
void main() /* use int main() if you want, but don't forget the return 0 once you're done */
{
Pizza p; //create an object (variable) of type Pizza
p.AddSauce(); //lets add some sauce...
p.AddCheese(); //...and some cheese
p.EatParts(3); //eats three parts of the pizza
getchar(); /* found in cstdio directive or conio, if you're working on an older version of C++ (I've always used this at the end, so you have to press something to get out of the program) */
}
Hope this helps....
A bit of C++ help please?
MaW Posted Jul 9, 2004
I've always found this definition of objects to be helpful:
'An object consists of some data, bundled with a number of methods which operate upon that data'
Classes are like instructions for objects - they set out what data an object has in it, and what code is associated with it.
Good reading... well, I learned C++ from a number of different books, none of which I'd describe as particularly easy. I think the penny dropped on Object-Oriented programming while I was reading 'C++ in Plain English'. Another book, 'Using C++', got me to the point where I was actually a halfway decent programmer, and then I reached that point where you just have to get the definitive book 'The C++ Programming Language' by Bjarne Stroustrup himself (he is the original designer of C++).
I also have Scott Meyers' books 'Effective C++', 'More Effective C++' and 'Effective STL' but they're fairly involved and you really don't want to go near them until you've got a good grasp of the language.
A bit of C++ help please?
MaW Posted Jul 9, 2004
Oh, and if you have any specific questions I'd be happy to answer them. I do this C++ lark professionally, so I probably know the answers to a few questions at least. I'm not very good with general explanations though.
A bit of C++ help please?
Ion the Naysayer Posted Jul 10, 2004
I like the cookie example. To add to it a bit, objects also frequently act on other objects, for example a person object might use its "eat" method on the cookie.
new person Jim;
new cookie JimsCookie;
new person Sally;
Sally->eat(cookie);
Jim->getupsetwith(Sally);
A bit of C++ help please?
Sue Posted Jul 11, 2004
Wow! Loads of help - thank you. The food analogies are rather good - very easy to relate to in my case
I'm a little reluctant to ask specifics - for one thing it's my job to learn this stuff if I'm ever going to pass the exam, also there are several professional programmers doing the course that have lost marks in assignments for achieving a result in a different way from 'course style'.
The bit that really gets me in a complete muddle is where to put everything - I'm told this is something that comes with time and practice, but if any of you have a particular way of remembering this it may help me. Oh and I'm using Borland C++ Builder 5 if it makes any difference.
Thanks
Sue
A bit of C++ help please?
Ion the Naysayer Posted Jul 14, 2004
Duh. I just noticed a rather glaring error in one of my statements.
The line:
Sally->eat(cookie);
should be:
Sally->eat(JimsCookie);
I haven't done an awful lot of real world OO programming so I don't know if this is universally true but in my experience you can't use methods on classes, only on instances of a class.
A bit of C++ help please?
Crazy Man Posted Jul 14, 2004
Well, sometimes in Java you can...
if you have a class called myClass with function "method()"...and you don't need to perform any manipulations on an instance, then you can do:
myClass.method();
Dunno if you can do this in C++ though
Key: Complain about this post
A bit of C++ help please?
More Conversations for The H2G2 Programmers' Corner
Write an Entry
"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."