Solved

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

Posted on 2015-01-24
3
85 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Assisted Solution

by:pepr
pepr earned 500 total points
ID: 40568145
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
ID: 40568966
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 29

Accepted Solution

by:
pepr earned 500 total points
ID: 40569193
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 120
Dialogbox API leak? 18 109
Beginner to Unreal Engine 4 5 106
Precision Problem in C++ 7 51
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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