+ 1
(C++)Confusing in class,object,method and function.
In concept,class defined object`s identity, attributes, and behavior.But when it cones to method,it said method,like a function,is class`s behavior. There are tons of answer on the web and related comment.Some said a method is basically a function that belongs to a class while others said methods are like actions an object can take.Now i am really confused.
3 Respostas
+ 6
A method is simply a function that's associated with an object.
Like "user.login()"
However, a function is not associated with any object, for example "input()".
If you're from a Java background, you'll be confused because in Java everything is associated with objects, hence "functions" don't exist.
Let's use Python for example
=======================
In python, a function is something like
"input()", "int()", "str()".
Note they aren't "joined" to any specific object/class
However, when you open a file like
file = open('text.txt')
You'll have to use "methods" to work with the file, for example
file.read()
In reality, methods are simply functions joined to a specific object/class.
+ 10
• Functions:
Functions often takes values from an object and returns a value, but it don't change anything on the objects it took as it's arguments.
• Methods:
Methods often take an object and an argument and use the argument to do something to the object.
+ 2
The class is the blueprints of what your object will have, so in the class there will be some fields (these will be its attributes like name, height) then theres your methods (these will be its behaviors like getName() getHeight()) and then you create an object from that class, think of a class as somthing you use to make objects from rather then thinking that the class becomes an object. When you have your object you can call the behaviours and attributes on that object.
//setSound would be a field (attribute)
cat.setSound; //meow
//makeSound would be a method (behavior)
cat.makeSound(); //meow