View Full Version : C++ Programming
SilverCrusader
02-05-2007, 3:58 PM
I've been learning C++ and I have a few questions, if anyone here knows C++ plz help me.
Here are one right now:
I am lead to understand that Classes "describe an object" so essentially what you put in a class makes up that object. Is this correct?
I learned C++ a few years back, currently taking a Java course as a "refresher", so I think I can help you.
A class is a way of creating a data type (think type String) with multiple variables (some of which can be private and the user can't see or access directly) and also procedures/methods that manipulate the data in the class.
So, the class will be a seperate file, and will be included as a header file (I believe). You then use an object to declare an instance of that class. A perfect example is String. String is actually a class, and creates a new type of variable when you declare a String. So the class would be the header you include at the top of the main program (#include <string> or something, right?) that allows you to access the seperate class file already created. Then, in the client program (void main()) you declare an instance, or an object, of that class. String c = "cee"; You could then go on and declare another. String d = "dee"; So the class is much like the cookie cutter, and the object is the individual cookies you make, and can later decorate or change differently by using the methods/procedures inside the class.
So when you think of memory, think of 2 areas of memory: Local variables, and Objects. In memory, when a primitive variable (int, double, float, char, etc) is created, it just stores that value in a box next to the variable name in the local variable memory. But when you create an object in memory, the variable name is there, but the box doesn't contain information like the primitive one, it contains an arrow (or a "pointer") that points to an object you created using an object declaration.
Say then, you have 2 objects, (String n, String m). You then type:
String n = "hello";
String m = n;
String n's box in local variable memory points to the object in object memory of type String, and it contains "hello". String m's box points to the same object. So then later, if you were to change String n, or String m, they would both change (since they are sharing one object). This is called aliasing.
If you need more help, just ask.
SilverCrusader
02-05-2007, 5:04 PM
Thanks it helps alot ^^
Uuugggg
02-05-2007, 6:55 PM
First of all, what other programming experience do you have?, since that is really a quite simple question... Classes are abstract data types, objects are instances of a class. Just google c++ class tutorial for details.
And uhm, hey! Everything GroG said is about Java. C++ handles the whole pointer thing much more literally. C++ classes *are* local variables, all their data is at their address, in one long 'box'. You have to declare pointers yourself.
'Thing this;' creates an object of type Thing.
'Thing* that;' creates a pointer to an object of type Thing.
'that=&this;' makes that point to this...
'Thing something=this' *copies* the data of this into something. You can look up copy constructors to screw around with that.
'Thing* asdf=that' copies the pointer, but not what it's pointing to.
SilverCrusader
02-05-2007, 7:36 PM
i understand pointers pretty well, they point to locations in memory.
To clarify one difference between C++ and Java. You said that primitives are "local" (on the stack) and objects are in non-local (on the heap). This is true for Java, but you can declare "local" objects in C++.
For example, in Java, something like
MyClass x = new MyClass();
is (roughly) the same as
MyClass* x = new MyClass;
in C++. The difference is in C++ you can do:
MyClass x;
which is the same as instantiating a Java object except that it's on the stack.
Also to clarify on what Uuugggg said, MyClass* x; only sets aside 4-bytes (on x86) on the stack to point to a MyClass object. By itself it doesn't actually do anything except point to a potential garbage address.
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.