Loading....
Coupon Accepted Successfully!

 

Defining Member Functions

The member function must be declared inside the class. They can be defined as (a) private or public section and (b) inside or outside the class. The member functions defined inside the class are treated as inline function. If the member function is small then it should be defined inside the class. Otherwise, it should be defined outside the class.

If function is defined outside the class, its prototype declaration must be done inside the class. While defining the function, scope access operator and class name should precede the function name. The following programs illustrate all about member function and how to access the private member of the class.

Member Function Inside the class

Member function inside the class can be declared in public or private section. The following program illustrates the use of a member function inside the class in public section.

8.6 Write a program to access private members of a class using member function.

 

Explanation: In the above program, the member function show() is defined inside the class in public section. In function main(), object one is declared. We know that an object has a permission to access the public members of the class. The object one invokes the public member function show(). The public member function can access the private members of the same class. The function show() initializes the private member variables and displays the contents on the console. For the sake of understanding only one function is defined.

In the above program the member function is defined inside the class in public section. Now the following program explains how to define private member function inside the class.

Private Member Function

In the last section, we learned how to access private data of a class using public member function. It is also possible to declare a function in private section like data variables. To execute private member function, it must be invoked by public member function of the same class. A member function of a class can invoke any other member function of its own class. This method of invoking function is known as nesting of member function. When one member function invokes other member function, the frequent method of calling function is not used. The member function can be invoked by its name terminated with a semicolon only like normal function. The following program illustrates this point.

8.7 Write a program to declare private member function and access it using public member function.

 

Explanation: In the above program, the private section of a class item contains one-member function values(). The function show() is defined in public section. In function main(), one is an object of class item. The object one cannot access the private member function. In order to execute the private member function, the private function must be invoked using public member function. In this example, the public member function show() invokes the private member function values(). In the invocation of function values(), object name and operator are not used.

Member Function Outside the class

In the previous examples, we observed that the member functions are defined inside the class. The function prototype is also not declared. The functions defined inside the class are considered as inline functions. If a function is small, it should be defined inside the class and if large it must be defined outside the class. To define a function outside the class, following care must be taken.

  1. The prototype of function must be declared inside the class.
  2. The function name must be preceded by class name and its return type separated by scope access operator.

The following example illustrates the function defined outside the class.


8.8 Write a program to define member function of class outside the class.

 

Explanation: In the above program, the prototype of function show() is declared inside the class terminated by class definition. The body of function show() is defined inside the class. The class name that it belongs to and its return type precede the function name. The function declarator of function show() is as follows:

void item:: show()


where void is return type; that is function is not returning a value. The item is a class name. Scope access operator separates the class name and function name, followed by the body of function that is defined.





Test Your Skills Now!
Take a Quiz now
Reviewer Name