PDA

View Full Version : can somebody help me not suck at c++ plzkthnx



NeoTifa
08-21-2008, 04:28 AM
wtf are constructors and destuctors for?! omg im so confused!!! is it really neccesary to have

Nutz()
~Nutz()

if it doesnt do anything?! can someone explain this to me in lamemans terms? i be weetahded. i will lub you foreber

Magixion
08-21-2008, 06:06 AM
Constructors and destructors are basically exactly what their name implies. Constructors instantiate and create the object while destructors destroy the object. You have to remember that any variant of C is an object oriented language, so you have to have a certain mindset for it. Really, once you get used to it, these will become second nature and very, very easy.

Flying Mullet
08-21-2008, 02:01 PM
And don't beat yourself up too much over C++ being hard. It's one of the most complex OO languages to learn.

NeoTifa
08-23-2008, 12:50 AM
but could you access the object later on or do you have to recreate it?

o_O
08-23-2008, 06:14 AM
When you declare an object using the constructor, you now have a variable name as a pointer to the object data in in memory. When you call delete() on an object, this calls the destructor. The effect of this is to remove the data pointed to by the variable name, invalidating the pointer.

In plain English, you should have one call to "delete" for every "new" used in object creation, and after you call delete, you can't access that object again.


In this diagram, each box is some memory block, containing some data. The variable name in the first box points to the object in the second box. The 0x0001 is just an arbitrary memory address.
<pre style="line-height: 8px;">joe = new Person("Joe", "Bloggs");
causes:
________ <u>0x0001 </u>
| joe: | | firstname: "Joe" | Memory taken off of the "safe to use" pile and
| 0x0001 | ==> | surname: "Bloggs" | Joe's data stored in it.
|________| |____________________|



delete joe;
causes:
________ <u>0x0001 </u>
| joe: | | Previous memory | Memory gets returned to the "safe to use" pile
| 0x0001 | ==> | of joe but memory | and other objects may now occupy it. For this reason,
|________| | is now available | referring to an object after it's been deleted could
| to other objects | mean you're referring to some other object unknowingly
|____________________| and is therefore unsafe.
</pre>

Moon Rabbits
08-25-2008, 06:28 AM
o_O's explanation is great. I'd also like to point out that objects do not have to be instantiated as pointers (although this is the most useful way to do so). When not using pointers, the constructor is called upon the implementation of an object, ie:

class SomeClass {
public:
SomeClass() {/*do stuff */ };
~SomeClass() {/*free stuff*/};
};

SomeClass someclassobject; // constructor would be called

Once your program exits, the deconstructor will be called. Deconstructors are useful for freeing memory. For instance, say you have an object that represents the player and stores a bitmap full of animations for the player. The constructor could load the bitmap and the deconstructor could free the memory used by the bitmap.

NeoTifa
08-28-2008, 06:01 PM
ok. a week ago that wouldve confused me cuz i just got passed the part in the book with pointers and free space. im starting to understand constructors now thanks. but what is getting me now is overloading operators XD

o_O
08-30-2008, 12:13 AM
Overloading is thankfully easier than understanding blocks of memory and pointers. :p

Consider this scenario:

You have a class that represents a vector, and you need to come up with a way to add them together using the "+" operator. You'd normally store a vector as an array of numbers, but if you try to add two arrays together, you'd get a compile-time error:
int arr1[3] = { 1, 2, 3 };
int arr2[3] = { 4, 5, 6 };

cout << arr1 + arr2; // Bad
However, we can overload the + operator to execute code that actually add the two vectors together:
// Note: Calling the class "Vect" since there are already Vector libraries for C++ called "Vector" :p
class Vect
{
int vector[]; // This array will hold the values in the vector
int size;
Vect::Vect(int numElements, int *values) // Constructor to assign vector values
{
vector = values;
size = numElements;
}

Vect Vect::operator+ (Vect vector1, Vect vector2) // Overloaded + operator
{
Vect added(vector1.size); // Creates a new vector to contain added values
for (int i = 0; i < vector1.size && i < vector2.size; i++) {
added[i] = vector1[i] + vector2[i];
}
return added;
}
}

// Then we can call this code:
Vect arr1(3, {1, 2, 3});
Vect arr2(3, { 4, 5, 6 });

cout << arr1 + arr2; // Good

In plain English, overloading operators allow to to make two objects do whatever you like when you use some operator on them, e.g. obj1 + obj2; obj1 * obj2, etc.
P.S. I haven't tested that code or anything, so don't expect it to work first time (there's bound to be something funky going on with pointers and references there). :p

NeoTifa
08-31-2008, 03:28 PM
hmmmm...... so you are basically making something up to manipulate the data? meh..... im not that smart. im probably gonna have issues with it. my main weakness has always been arrays and such. new concepts + arrays = x_x

o_O
09-01-2008, 12:11 AM
Basically it's defining the way two objects behave when an operator is used on them. You can create an instance of an object such as a string, for example - what should happen when somebody says:
string s("lol" + " internets");
In the rules of English, there's no default action that you should take when somebody applies addition to a string, so in C++ you can overload the operator and make the two strings actually do something useful when you add them. Like joining them (this is actually what C++ already does, but it's an example of how operators are overloaded. :p).
string string::operator+ (string first, string second)
{
// Some code here to join both strings together and return them
}

Similarly, what should happen when you try to divide two strings? C++ doesn't define an action for string division, so you could choose whatever behaviour you like. You could return the lengths of the strings divided; you could return the first half of the first string matched up with the second half of the second string and vice versa; you could even do something like this:
string string::operator/ (string first, string second)
{
return "That doesn't even make sense!";
}Then, if someone tried to divide strings:
string s1("lol");
string s2("internets");
string s3 = s1/s2;
// The string "s3" now contains the string "That doesn't even make sense

NeoTifa
09-01-2008, 10:00 PM
oic!!!! XD thnx!