• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

14. What is a method? How is a method called?

14. What is a method? How is a method called?
What is the difference between function and method? How method is stored in memory?
Is it stored in some table as a pointer to function?
0
Nusrat Nuriyev
Asked:
Nusrat Nuriyev
  • 2
2 Solutions
 
peprCommented:
Class is a (static) description of behaviour of the object of that class. This way "class" can be thoutht of as "kind" of objects. The class (as a description) says how will object also look like from the data point-of-view. If object can be illustrated as a "tangible" entity (say drawn as a circle), data members are smaller circles inside. Better to say, data members are also objects -- we can think about them as about a piece of memory with some content (data) stored inside. However, you have to have access to the data, and you have to know how to interpret them (some numbers mean years, other numbers mean height...).

Methods are member functions of the class. They are also static in the sense of description (one description for any number of objects of that class). If data members (member variables) store values, the member functions (also known as methods) are used for manipulation with the data or for interpretation of the data (i.e. returning something based on the member variables).

Methods are described as a part of a class definition. Typically, they are called with the object (instance of the class) as a standard argument. However, the argument uses a special syntax when the method is called -- the object identifier is written in front of the metod identifier separated by dot (or by -> if pointer to the object is used).

It would be better if you read some decent textbook first and if you asked only the things that you do not understand. Have a look, for example, at "Thinking in C++" by Bruce Eckel (http://mindview.net/Books/TICPP/ThinkingInCPP2e.html -- can also be downloaded for free, or you can by the paper books.)
0
 
Nusrat NuriyevAuthor Commented:
Did you mean that from the description perspective of view methods or data are described more or less similar?
Thank you for the link. I will keep in mind that.
0
 
peprCommented:
Desctiption of both data structure and of the functionality is part of the class definition and it does not depend on whether the object exists of how many objects exist.

When an object is created, the memory for its (internal) data members is allocated (simplified). But there is no need to allocate anything for methods. The object knows its class and the method definitions are part of the class. In other words, data values are part of the memory footprint of the object. The more objects you created, the more memory will be consumed. On the other hand, no extra memory is needed for methods.

Another point of view, both data and methods (i.e. member functions) belong to the object. However, only data is stored inside of the object. Methods describe only the data transformations. Therefore, they are independent of the actual data values.

Another point of view, data definitions and mentod definitions are part of the class definition -- this way they belong to the class. They also  belong to the object of the class, because the object is described by the class. Data values are known to the object but they are not known to the class (simplified).

If you think about "onion", you think about a class of a vegetable. If you have one onion in your hand, it is the object of the class (even in cases when you do not know it is called "onion". The onion class describes how any onion should look like. It may describe its possible weight, but only the object has the exact weight that can be checked whether it is as it should be.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now