Member Functions – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

member functionsMember Functions | Reference Notes
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Member Functions
A member function performs an operation required by the class. It is the actual interface to initiate an action of an object belonging to a class. It may be used to read, manipulate, or display the data member. The data member of a class must be declared within the body of the class, while the member function of a a class can be defined as two places:

  • inside the class definition
  • outside the class definition

The syntax of a member function definition changes depending on whether it is defined inside or outside the class declaration/definition. However, irrespective of the location of their definition, the member function must perform the same operation. Thus, the code inside the function body would be identical in both the cases. The compiler treats these two definitions in a different manner. Let us see, how we can define the member function inside the class definition.

the syntax for specifying a member function declaration is similar to a normal function definition except that is enclosed within the body of a class. for example, we could define the class as follows:
class Number
{
int x,y,z;
public:
void get_data(void);            //declaration
void maximum(void);         //declaration
void minimum(void);        //declaration
{
int min;
min=x;
if(min>y)
min=y;
if(min>z)
min=z;
cout<<"Minimum value is="<<min<<endl;
}
};

If you look at the above declaration of class Number you can observe that the member function get_data() and maximum() are declared, but they are not defined. The only member function which is defined in the class body is minimum(). When a function is defined inside a class, it is treated as a inline function. Thus, member function minimum() is a inline function. Generally, only small functions are defined inside the class.

Now let us see how we can define the function outside the class body. Member functions that are declared inside a class have to be defined outside the class. Their definition is very much like the normal function. Can you tell how does a compiler know to which class outside defined function belong? Yes, there should be a mechanism of binding the functions to the class to which they belong. This is done by the scope resolution operator (::). It acts as a identity-label. This label tells the compiler which class the function belongs to. The common syntax for member function outside the class is as follows:
return_type class_name::function_name(argument declaration)
{
function body
}

Here the scope resolution :: tells the compiler that the function_name belongs to the class class_name.

Program to illustrate member function definition outside the class
class Number
{
int x,y,z;
public:
void get_data(void);             //declaration
void maximum(void);         //declaration
};
void Number::get_data(void)
{
cout<<"Enter the value of first number(x):"<<endl;
cin>>x;
cout<<"Enter the value of second number(y):"<<endl;
cin>>y;
}
void Number::maximum(void)
{
int max;
max=x;
if(max<y)
max=y;
if(max<z)
max=z;
cout<<"Maximum value is:"<<max<<endl;
}

If you look at the above declaration of class Number, you can easily see that the member function get_data() and maximum() are declared in the class. Thus, it is necessary that you have to define this function. You can also observe in the above snapshot of C++ program identity label (::) which are used in void Number::get_data(void) and void Number::maximum(void) tell the compiler the function get_data() and maximum() belong to the class Number.

(Visited 353 times, 1 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag : ,

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary