banner



How To Use A Member Data In Member Function On Different Member Function C++

(Last Updated On: April 2, 2021)

member functions

The nature of fellow member functions

The member part of the course (referred to equally the form function) is a kind of office, and its usage and role are basically the aforementioned as the general function. Similarly, it also has render values ​​and part types. The difference between it and general functions is only: it is a member of a class, now in the class torso. It can exist designated every bit private, public or protected. When using a class function, pay attention to the permission to call information technology (whether it tin can be called) and its scope (the part can be used What range of information and functions to apply). For example, private member functions can only be called past other fellow member functions in this class, just cannot be called outside the class. Fellow member functions can access whatsoever member of this course (including private and public), simply to refer to data valid in this scope.

The full general approach is to specify the member functions that need to be chosen by the exterior world as public, which are the external interface of the class. In fact, non all member functions are designated as public. Some functions are non intended to be called for the outside globe, and if the fellow member functions in this grade are called, they should be designated as private. The function of this function is to back up the operations of other functions are utility functions of other members of the class, and users cannot call these private tool function. The member function of a class is a very important office of the grade body. If a class does non incorporate fellow member functions, then Equivalent to the structure in C language does not reflect the role of classes in object-oriented programming.

Amazon Purchase Links:

Tiptop Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

 Define member functions outside the class

The member functions yous saw before are divers in the class torso. You lot can also declare but member functions in the grade body, The office definition is performed outside the class. Such equally

Note: When directly defining a role in the class body, yous do not need to add the course name in front of the function name, because which one the role belongs to This category is cocky-evident. But when the member function is defined exterior the class, the class proper noun must exist added to the function proper name to be qualified (qualified), ":" is a field qualifier or scope operator. Use it to declare that a function is a In which category. Pupil::display() represents the display function in the scope of the Student class, which is The brandish role in the Student form. If there is no limitation of "Student:;", display() defaults to the current effective operation The brandish function in the domain, for the in a higher place plan segment, refers to the display role in the global scope, non The display part in the Pupil form. And unlike classes may have functions with the aforementioned name (just the functions may be different), used as Using domain qualifiers to authorize, it clearly indicates the function in which scope, that is, which class of function. If in that location is no grade proper noun before the scope operator ":", or there is neither course proper name nor scope before the role name Operator ":", such as

:: display ()

or

display ()

It means that the brandish function does not belong to whatever course. This function is not a member function, simply a global part, that is, general function. The grade function must exist declared in the grade body first, and then defined exterior the form, that is to say, the position of the class torso should be in the role Before the definition (as shown above), Otherwise, errors will occur during compilation. Although the function is divers outside the class, it will be found co-ordinate to the function prototype declared in the course when the fellow member part is called The definition of the function (role code) to execute the function. Description: Declare fellow member functions inside the class, and define fellow member functions outside the form. This is a part of program design.

A adept addiction. If a function has but two to 3 lines in its function trunk, it can generally be defined in the class body when the course is alleged. Functions with more than 3 lines are mostly alleged inside the course body and defined exterior the form. This not only reduces the length of the course trunk, but also makes The class body is articulate, easy to read, and can separate the interface of the class from the implementation details of the class. Because from the definition of the class user simply see the epitome of the function, but not the details of the function execution. From the perspective of the user of the class, the class function is more than like a black box hides the details of execution. In doing and so, the quality of software engineering science is improved.

Built-in fellow member function (inline fellow member office)

Class member functions can besides be designated equally built-in functions. The size of the fellow member functions divers in the class body is generally very small, and the fourth dimension taken by the system to telephone call the function the cost is relatively large. The time cost of calling a function is much greater than that of a small-calibration function body Execution of all sentences Line time. In gild to reduce time overhead, if the member functions defined in the class body do non include control structures such as loops, The C++ arrangement will automatically treat them as inline functions. In other words, call these into the programme When calling a fellow member function, it does not actually execute the function call procedure (such as retaining the render accost, etc.), merely replaces the office with Lawmaking is embedded in the calling point of the program. This can profoundly reduce the time overhead of calling member functions. C++ requires the keyword inline declaration for general built-in functions, but for member functions defined in the grade, you can omit inline, because these member functions have been implicitly designated equally born functions. Such as

Explicitly declare the display function as a built-in function. The in a higher place two writing methods are equivalent. For functions defined in the grade body Count, generally write inline. People should pay attention to: If the fellow member function is non divers in the class body, but defined outside the class, the system does not default it For congenital-in functions, the procedure of calling these member functions is the same as that of calling general functions. If you lot want to Member functions are designated as built-in functions and should be explicitly declared in inline. Such every bit

For those that need to exist candy as built-in functions, just make an inline announcement in the part definition or the function prototype declaration. (I of the two is fine). Information technology is worth noting that if the inline part is defined outside the class, the proclamation of the class The definition of the member function and the member function are placed in the same header file (or written in the same source file), otherwise information technology cannot be compiled Perform replacement (embed a copy of the office lawmaking into the part phone call signal). But doing so is not conducive to the interface and the class To achieve separation is non conducive to information concealment. Although the execution efficiency of the program has been improved, from the perspective of software engineering quality Expect, this is non a good way. Only when the calibration of the member part defined exterior the grade is small and the calling frequency is high, information technology is designated as a built-in function.

Storage of member functions

When defining objects with classes, the system allocates storage space for each object. If a grade includes data and functions Number, logically speaking, to allocate storage space for data and role code (referring to compiled object code) respectively. If you utilize The aforementioned form defines 10 objects, so practise you lot need to allocate storage for each object's data and function code separately? Unit of measurement and "package" them together (as shown in below Figure )?

Member Function

In fact, this is non the instance. After analysis, information technology can be seen that the values ​​of data members in different objects of the same type are by and large different The same, merely the lawmaking of the part of unlike objects is the same, no affair which object's function code is called, information technology is actually adapted The aforementioned code is used. In this example, open upwardly 10 segments of space in the memory to store 10 identical internal The function code segment of content is obviously unnecessary. People naturally think: Can you lot but use a space to shop this common The target code of the role, when calling the function of each object, all call this common function code, every bit shown in below Figure.

Member Function

Obviously, this will profoundly salvage storage space. The C++ compilation system does exactly this, so every object The storage space occupied is only the storage infinite occupied by the information members of the object, and does non include the storage infinite occupied by the function lawmaking storage. If one of the following classes is declared:

Readers tin can apply the following argument to become the number of bytes occupied by this type of object: cout << sizeof (Time) << endl;

In the Visual C++ environment, the output value is 12. This proves that the infinite occupied by an object only depends on It is the space occupied by the data member in the object, and has nothing to exercise with the member role. The target code of the function is stored in the object empty Outside of time. If 10 objects are defined for the same form, the member functions of these objects correspond to the same part Code segment instead of 10 different function lawmaking segments. It should be noted that although the same part code is executed when calling fellow member functions of different objects, the execution The row results are by and large not the same. For example, when the member function display of the stud1 object is called, the output is the object

The stud1 data num, name and sex values, when calling the fellow member function display of the stud2 object, the output is The data num, name and sex of the object stud2 have different results. Because the member function of the object stud1 visits The question is virtually the members of this object. And so, a trouble occurred: different objects utilize the same function code segment, how can it be separated How to operate on the data in different objects? Originally, C++ specifically set up a pointer named this for this purpose. Betoken to different objects, when the fellow member function of the object stud1 is called, the his pointer points to stud), the fellow member function admission

Is the member of stud1. When the member function of the object stud2 is chosen, the this arrow points to stud2, and the fellow member The function visits the members of stud2. Demand to explain: Month

(i) Regardless of whether the member function is defined in the class or exterior the class, the storage method of the lawmaking segment of the member function is the same Similarly, it does not occupy the storage infinite of the object. Don't mistake the code segment of the member role divers in the form to occupy the object The storage space of the member function divers exterior the class does not occupy the storage space of the object.

(2) Don't confuse this storage method of member functions with the concept of inline (built-in) functions. Don't mistake A member function declared inline (or inline by default), its code segment occupies the storage space of the object, instead of inline

The code segment of the declared member function does not occupy the storage space of the object. Regardless of whether information technology is declared inline or non, the lawmaking segment of the member function does not occupy the storage space of the object (readers tin go to the computer Verify). The part of the inline declaration is to copy and insert the lawmaking segment of the function into the role call when the function is called. Apply a point, and if you don't employ the inline statement, when the role is called, the process transfers to the entry address of the function code segment. After the function code segment, the process returns to the function telephone call signal. The inline function only affects the execution efficiency of the program. Whether the number occupies the storage space of the object is irrelevant, they do non belong to the same category and should non be dislocated.

 (three) Someone may ask: Since the code of the member function is not placed in the storage space of the object, then I said before Is the statement of "the fellow member function brandish of the object stud1" wrong? It should exist explained:

"Member role" is from a logical point of view, and the storage method of member functions (not stored in the space of the object), From a concrete point of view, it is optimized by the computer according to The two principles are non contradictory. Physical realization must ensure the logical realization. For example, someone who has a lot of money can put it at habitation or hire it in the bank in his safety, although the rubber is not physically in his dwelling, he rented the prophylactic. This money is undoubtedly for him, this is from a logical point of view. Similarly, although member functions are not placed in the storage space of the object, Just from a logical point of view, member functions are encapsulated in an object together with data, and only functions of members in this object are allowed. Admission to private information in the same object. Therefore, "call the member function display of the object stud1, and output the object The hour and minute data in stud1 volition non crusade misunderstanding. Every bit a programmer, information technology is beneficial to empathise some knowledge of concrete implementation, which can deepen the trouble understanding.

How To Use A Member Data In Member Function On Different Member Function C++,

Source: https://www.electroniclinic.com/member-function-in-c-with-examples/

Posted by: speerblema1996.blogspot.com

0 Response to "How To Use A Member Data In Member Function On Different Member Function C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel