Solved

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

Posted on 2015-01-24
3
79 Views
Last Modified: 2015-01-25
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
Comment
Question by:Nusrat Nuriyev
  • 2
3 Comments
 
LVL 28

Assisted Solution

by:pepr
pepr earned 500 total points
Comment Utility
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
 

Author Comment

by:Nusrat Nuriyev
Comment Utility
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
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now