Wednesday, 11 March 2015

Write short notes on – Inline function, Automatic inline function, Nested member function, Private member function, Arrays of object , Object as function argument, Friendly function, Object pointer.

Inline Function
Inline functions are a C++ enhancement feature to increase the execution time of a program. Functions can be instructed to compiler to make them inline so that compiler can replace those function definition wherever those are being called.
However, every time a function is called, it takes a lot of extra time in executing a series of instructions for tasks such as jumping to the function, saving registers etc.  To eliminate the cost of calls to small functions, inline functions are used. Compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime. 

Automatic inline function
If a member function's definition is short enough, the definition can be included inside the class declaration. This kind of function is called automatic inline function. When a function is defined within a class declaration, the inline keyword is no longer necessary.

Nested member function
A member function can be called by its name inside another member function of the same class is known as nested member function.

Private member functions
A private member function can only be called by another function that is a member of its class.
It is normal practice to place all the data items in a private section and all the functions in public, some situations may require certain functions to be hidden from the outside calls.

Arrays of object
We can have arrays of variables that are of the type of the class, such as variables are called arrays of objects.

Object as function argument
Like any other data type, an object may be used as function argument. This can be done in two ways.
· A copy of the entire object is passed to the function
· Only the address of the object is transferred to the function
The first method is called pass-by-value. Since a copy of the object is passed to the function, any
changes made to the object inside the function do not affect the object used to call the function.
the second method is called pass-by-reference. When an address of the object is passed, the called
function works directly on the actual object used in the call. This means that, any changes made to the object inside the function will reflect in the actual object.

Friendly function
A non-member function cannot have an access to the private data of a class. However, there could be a situation where we would like two classes to share a particular function. In such situations, C++ allows the common function to be made friendly with both the classes, thereby allowing the function to have access to the private data of these classes. To make an outside function friendly to a class, we have to simply declare this function as a friend of the class.

Object pointers

It is possible to access a member of an object via a pointer to that object. When a pointer is used, the arrow operator(->) rather than the dot operator is employed. Object pointer is very much important for Polymorphism concept in C++. Pointers provide an alternative approach to access other data objects.

No comments:

Post a Comment