Solved

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

Posted on 2015-01-24
3
80 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
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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

17 Experts available now in Live!

Get 1:1 Help Now