Can anyone tell me how objects and member functioins interact? Could I get the information in simple, non-technical terms perhaps followed by an example. If you help me, then I would be happy to help you back in some way or another. I don't know if that would be possible, though because my money is just about gone already. So, basically, I'm begging for help in this crucial matter to my project.
Thank you Bazzy for your response. If you are interested, then here's the specific question:
When it comes to objects and member functions I see this:
"OBJECT" + "."(dot operator) + "MEMBERFUNCTION".
This is what I don't understand:
How does that member function apply to its object?
Or, Where in the object is the member function called?
Because,
in algorithmic programming, which I have done
there are regular user-defined functions
and they are called by I guess you would call it reference maybe
where you use the call the function as a variable
in specific locations of the program
which, I guess, going from algorithmic to object oriented
would become basically OBJECTS.
I DON'T SEE HOW YOU CAN TACK ON AN ENTIRE FUNCTION
TO AN ENTIRE OJECT because
I DON'T SEE WHERE IN THE OBJECT IT IS CALLED.
THAT IS WHERE I AM HAVING ALL MY TROUBLES RIGHT NOW.
So I have laid out my question as specifically as I can.
If you can help me resolve this one,
THEN I WILL BE EXTREMELY GRATEFUL.
THANK YOU SIR OR MA'AM.
How does that member function apply to its object?
Lets take an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Class {
int i;
public:
void set_i() { i = 5; }
};
int main()
{
Class obj; // create an object of Class
obj.set_i(); // call the set_i function
return 0;
}
Here, you create a class which in this case consists of one member function and a member variable. This means that any object that is created from this class will be given ALL of these traits. Therefore, if you created another two objects say obj2 and obj3, both of these will also have the member function set_i() and the private member variable i. This is how you can "TACK ON AN ENTIRE FUNCTION TO AN ENTIRE OBJECT". The class the object is made from is like the objects foundations.
[Edit]
The dot operator (.) is c++'s way of objects calling their member functions.
Taking the simplest case, the layout of an object in memory is as follows.
Consider the following class:
1 2 3 4 5 6 7 8
class Foo {
int x;
int y;
public:
virtual ~Foo();
virtualvoid Frobnicate();
void MemFun();
};
In memory, the instance of this class looks like (assume 32-bit machine)
first 4 bytes = vtable pointer
next 4 bytes = x
next 4 bytes = y
Forget the vtable pointer for now.
One calls MemFun like this:
1 2
Foo f;
f.MemFun();
What this does is simply call a function, just like any other function, with an implicit first parameter
that is a pointer to the instance that the function is being called on (so, a pointer to the 12-byte
block shown above).
In C, the equivalent is (forget virtuals/vtable pointer):
1 2 3 4 5 6 7
struct Foo {
/* void* vtable; forget this */
int x;
int y;
};
void MemFun( Foo* This ) {}