Jump to content
  • Advertisement
Sign in to follow this  
conejo5991

objects and methods

This topic is 3233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am attempting to learn OOP, trying to get there from an algorithmic perspective. What I'm down to is this basic problem:: The schoolbooks say that a member function (method) is associated with an object. That's fine. But I would like to know how it is associated. What does the method do to the object in a general sense, that it would not do if suppose it had not been added. Is it that the value(s) in the method are passed into object variables? Or is it something else? I posted somewhere else, and the answer I got back said that a method didn't necessarily affect the variables in the object at all. If that is true, then, and I hate to sound like a broken record, then how does the method affect the object by being associated with it, if we assume it has no bearing on the variables within the object? And maybe you could, please please pleease attach a bonehead example of an object interacting with a member function. My text will show examples, but it doesn't show the code that makes up the object now the code that makes up the member function. So could you make the member function user defined? AS well as the object. Thank you good sir/ ma'am for taking the time to read this and even more if you can help me see the light of OOP :) Have a nice day. Kudos.

Share this post


Link to post
Share on other sites
Advertisement
In general, a member function has privileged access to the member variables of the object instance. In many OO languages members can be marked public and private, and member functions are the only ones allowed to access and modify private members. Non member functions will not access member variables - even if only by convention in languages that don't support member access control.

Most languages have special syntactical support for member functions, so that calling them is different to calling regular functions. Likewise, the object on which the function was called is made available through a special variable or keyword rather than an explicit parameter.

Finally, member functions can be overridden in derived classes (sometimes requiring explicit keywords). When such a member function is called, the runtime will resolve to the correct member function, even if the static type of the object is different.

Of course, you can have languages where some of the above isn't true, and you can write member functions which do not touch an objects member data.

As for your request for examples, which language are you using?

Share this post


Link to post
Share on other sites
At a very basic level, the only difference between a free function and a member function is that the member function receives a 'this' pointer - i.e. a pointer to the object it is a member of. This pointer allows it to access and modify the state of the object.

In C++, the 'this' pointer is passed to the function automatically, without any effort on the part of the programmer. Some languages (such as Python) require the programmer to explicitly specify the this pointer, instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by conejo5991
But I would like to know how it is associated.

Methods have a hidden parameter called this which points to the object on which the method was invoked.

So if you say someobject.somemethod(1, 2, 3), inside the method this will point to someobject -- this enables you to manipulate its state.

Share this post


Link to post
Share on other sites
Thanks for the responses. I am trying to program in C++, but I am hoping to learn PYTHON eventually because you don't seem to have to declare as much.
But with all your attempts to help me, I still have the lingering question, which is this:

Does the method put values into the variables of thre object,

which non-OOP user-defined functions did except for VOID.

Or does, in general, the method "MODIFY" the object some other

way? How does it modify it? I swear, that's the issue upon which

I am completely lost. What does the code in the method do to the code

in the object, as a general rule. Or, do I have the idea roughly

correct already, that the method will inject the right types of variable

returns into the appropriate variables in the object respectively?

Thanks again for taking the time to help me clarify this situation.

Someday I'll get it, as a programming aspiration, and as an all-pervasive

statement with respect to life itself. Although, that's more for another forum.

Laugh out loud :)

Share this post


Link to post
Share on other sites
Quote:
Original post by conejo5991
What does the code in the method do to the code in the object, as a general rule.

What? There is no "code in the object". An object consists of data, and there are methods that manipulate that data.

For example, if you have a class Person with a member variable name and a member variable age, then each object of that class will have its own name and age, but the methods exist only once, no matter how many objects you have.

When you invoke a method on an object, that method knows which object to modify thanks to the this pointer, and it has access to all the member variables of said object.

Here's a contrived, untested example of a Person class.

class Person
{
std::string name_;
int age_;
std::set<Person*> friends;

public:

Person(const std::string& name, int age) : name_(name), age_(age)
{
}

std::string name() const
{
return name_;
}

int age() const
{
return age_;
}

void celebrate_birthday()
{
++age_;
}

void make_friends_with(Person* p)
{
friends.insert(p);
}

int number_of_friends()
{
return friends.size();
}

void become_misanthrope()
{
friends.clear();
}
};

Share this post


Link to post
Share on other sites
Oh! That's strange. I was thinking that the Class more or less defined things.

Then the OBJECT was the part of OOP where you laid down the core code.

And the Member Functions did the math so you could use the variables

within the object. From the last Reply it seems now that OBJECTS

are filled with only DATA, which reminds me of the READ/ DATA

statements from dinosaur BASIC. Boy, now I have to begin to

retool my whole perception of what's going on.

So if an object is only a set of data, then where do the primitive line

commands get used, only in MEMBER FUNCTIONS?

Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by conejo5991
Oh! That's strange. I was thinking that the Class more or less defined things.

It does. The class defines the member variables and the methods at compile-time. At runtime, each object has its own member variables, but the methods are not unique for each object (that would be a waste of space). Each method manipulates the member variables of the object on which it was invoked.

Quote:
Original post by conejo5991
So if an object is only a set of data, then where do the primitive line commands get used, only in MEMBER FUNCTIONS?

I have absolutely no idea what you are talking about. What is a primitive line command?

Share this post


Link to post
Share on other sites
Verbal glitch, by primitive line commands,

I was meaning C++ code using what I thought they called

Primitive Types, just the rudimentary coding such as:

cout>>, or cin>>, or if/else, or while/do, for/next

That kind of thing. All things algorithmic.

Once you get OOP going, the member function at least has these things right?

But two or three replies ago, someone said that

OBJECTS have only data. I was under the mistaken impression that

OBJECTS have these primitive algorithmic types within them.

Is that true?

And by the way, is there such a thing online as a simple C++ OOP

course, maybe even free, but you can't expect too much I guess,

where the instructor could possibly clear all these things up for me,

and then I could really get a jumpstart on coding?

Share this post


Link to post
Share on other sites
Quote:
Original post by conejo5991
Once you get OOP going, the member function at least has these things right?

Anything you can do in a normal function you can also do in a member function.

Quote:
Original post by conejo5991
But two or three replies ago, someone said that

OBJECTS have only data. I was under the mistaken impression that

OBJECTS have these primitive algorithmic types within them.

Is that true?

The data can consist of these primitive types... Look at my Person example. A person consists of a string, an int and a set of pointers to other Persons. int is a primitive type.

And "objects have only data" means that at runtime, the data is what takes up space. The methods logically BELONG to the object, but the object does not HAVE them (you were talking about "code in the object" which is clearly wrong). The data and the methods belong together, and that's why you find them bundled together in classes. Maybe you are just confusing classes and objects?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!