methods and objects

What is a method and what is an object?
Last edited on
You have no return statement in counter().

A method is another word for member function (a function that belongs to a class).
Last edited on
I'm not sure what I need to return. My cout returns my position count but my method count doesn't show anything.

So in my code a method is everything listed under private and public in my class?


I still don't know how to fix my code.
So in my code a method is everything listed under private and public in my class?

No. You can have other things that aren't functions declared within your class definition, such as data members and types.

What about Peter87's definition is unclear? As he says:

A method is another word for member function (a function that belongs to a class).
Well in Peters definition I take it as him saying that everything listed under public and private is a method.

But it seems from what your telling me that things under those functions are methods as well.

So for example in this piece of code I just threw together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

Class method
{
public:

int random;// <- is this a method?

private:




};

int method::random() 
{

int x = 5; //<- and is this a method?
while( x > i, i++)
{
       cout << "hi" << endl; 
}
}
int main()// what about this line?
{

return 0;


}


Would be easier to point things out to me with code rather than words. And if I'm missing anything that may be a method please inform me, thanks.

I would also like to know what an object is?
Last edited on
Well in Peters definition I take it as him saying that everything listed under public and private is a method.

No. He's saying functions listed under those things are methods. He explicitly uses the word function. Twice. So why would you decide that other things which aren't functions are also methods?

Your code snippet makes no sense. You define a public data member called random, but then define a function also called random. Did you mean to write the following?

1
2
3
public:

int random();// <- is this a method? 


And then:

int x = 5; //<- and is this a method?

Well, does it satisfy Peter's definition? Is is a function? Is it a member of a class?

If you don't even know what a function is, I suggest you return to your textbook and go back to re-read the section on functions.

I would also like to know what an object is?

Surely any textbook or tutorial can tell you this? Hell, a simple Google search should find you some answers.

We'll happily help you solve genuine answers. But asking us simply to re-type material that has already been written and is easily findable, is just lazy and rude.

EDIT: And why are you deleting the contents of your posts? It makes the thread look like nonsense, and ruins the value of this thread as something that can help other people learn too.
Last edited on
The only thing I've omitted from my thread is my code, which was useless to what this thread had become. Which is about what a method is. as for my code it seems that I made edits to it as you replied to it. When I first made it, it wasn't a class so I made quite a few edits as you replied. No need to get hasty.

Google is very handy when it comes to this but when I read an object is an instance of a class. Well that's just like reading that flying means to fly. It doesn't really tell me anything.

And I do know what a function is. I just misread and confused myself when you mentioned data members and types. That's why I asked if an integer was also a method.
Last edited on
The only thing I've omitted from my thread is my code, which was useless to what this thread had become.

The posts solving your initial code problem are still there. Anybody coming into this thread now would find it incomprehensible. Now, no-one can learn from seeing your code and seeing how people helped solve the problem. Just because the later posts have become about a different topic, doesn't mean that those early posts magically disappear.

It's bad form to go back and radically change your earlier posts such that parts of the thread become meaningless. Please don't do that.

A class is a type. An object is when you actually create a thing of that type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// This bit defines what the class is.  It defines a type.  It doesn't actually create any variables (or objects) of this type.
class MyClass
{
public:

  int MyMethod()
  {
    // Do stuff
  }
};

int MyFunction()
{
  MyClass.MyMethod();  // THIS IS MEANINGLESS.  MyClass is a type, not an actual variable you can operate on.

  MyClass myObject;

  // MyClass is a type.  myObject is a variable of that type, i.e. an object.

  myObject.MyMethod();  // This is fine.
}
Thanks Mikey, that clears things up.

Also my initial code problem still remains a problem but I'm going to try some things before I ask for help here again since I got this answered.

Topic archived. No new replies allowed.