[Optimus One, P500, V General] andy572:
take as example a class object variable, this variable allocates some memory - even if its empty

or not initialized (u can declare all variables in a programm and use it later):

class myclass {
public:
myclass() {} // empty default constructor
}

myclass *obj1; // we would like to use the as object as pointer, here the variable isnt instanciated
myclass obj2; // we dont want to use the variable as pointer, here the variable is instanciated

The object "obj1" doesnt have a valid instance, you shouldnt use it as is now - this gives you a

core dump. instead, you have to initialize the pointer variable before usage to allocate a valid memory adress:

obj = new myclass();

Now, what is the difference between obj1 and obj2?

obj1 now points to a valid memory adress - u can use every time the same memory adress instead of allocating more and more memory

for the same "****" as with obj2. the mostly difference between these two objects is, that obj2 doesnt can call the class constructor

or destructor - all what you allocate in memory must be initialized before.

Lets extend the example class to understand what happens if we call some things in the class:

class myclass {
public:
myclass() {
mystring = "hello world";
}

void printString() {
cout << mystring;
}

private:
char *mystring;
}

myclass *obj1 = new myclass(); // declare and instanciate a class object
obj1->printString(); // we use the pointer

myclass obj2; // declare the class object
obj2.printString(); // we use simply the unallocated class

whats happen now?

the first example will print the declared string "hello world", it was allocated in the class constructor

the second example will show you a nice core dump (in this example not because the string is simply empty) or

in this example it does nothing: the string variable was never filled.

using "this" in a class:

Lets extend the example class again and create a child class from it:

class mysecond extends myclass {
public function printMyString() {
cout << mystring;
}
}

mysecond *obj1 = new mysecond();
obj1->printString(); // call function from parent class
obj1->printMyString(); // call function in child class

Whats happen now? And, why we dont have a constructor?

We dont need a new constructor here, the constructor is called from parent class, so the

variable "mystring" gets filled with "hello world".

the first call "printString()" will work, the second call not - it gives you a compiler error:

the variable "mystring" is declared as "private", so you dont have permission to use it.

To use the variable, you have to choices:

1.) "mystring" must be protected or public in the parent class

2.) "mystring" can resist both in the parent class and in child class

choice 1)

Use the variable "mystring" from parent and change the permission to "protected":

class myclass {
public:
myclass() {
mystring = "hello world";
}

void printString() {
cout << mystring;
}

protected:
char *mystring;
}

class mysecond extends myclass {
public function printMyString() {
cout << mystring;
}
}

mysecond *obj1 = new mysecond();
obj1->printMyString(); // call function in child class

this example is working, we use the variable from parent now and its allowed because of "protected" access.

Back to "this" in a class:

we see in our parent the variable "mystring" now as protected or private, but we dont want to use this variable because

its doing some **** or is definitvley closed access for us - but, we would provide a function that does the same.

To do this, we have to look at the parent class - ****, the variable is private but lets assume the function "printString" is

virtual: virtual means, you can overwrite this function and all is fine:

class myclass {
public:
myclass() {
mystring = "hello world";
}

virtual void printString() {
cout << mystring;
}

protected:
char *mystring;
}

class mysecond extends myclass {
public mysecond() {
this->mystring = "in child class"; // we dont use the variable from parent but our variable in the class
}
public function printString() { // we overwrite the parent function
cout << this->mystring;
}
private char *mystring;
}

mysecond *obj1 = new mysecond();
obj1->printString(); // prints now "In child class"

As you can see, we can simply use our own variables with "this" instead of **** from parent.

the "this" access is a pointer too, it points directly to the memory adress of our class instance.

I hope, this helps something :D