C++ MEMBER FUNCTION
Member functions can be defined in two places, show below
1. outside the class Definition
2. inside the class Definition
It is obvious that irrespective of the place of definition, the function should do the same task. Therefore code for the function body would be identical in both the cases
However, there is some difference in the way the function header is defined. but these approaches are discussed in detail in this tutorial
outside the class definition
member functions that are declared inside a class have to be defined separate outside the class. They should have function header and a function body, Their definitions are very much like the normal functions.
Since C++ language does not support the old version of function definition, the ANSI prototype form must be used for defining the function header
An important difference between a member functions and a normal functions is that a member function incorporates
a membership identical label in the header. This label tells to the compiler which class the function belongs to .
The general form of a member function definitions is
return-type class-name:: function-name(argument declaration)
{
function body
}
|
The membership label class-name:: tells to the compiler that the function function-name belongs to class class-name. That is the scope of the function is restricted to the class-name specified in the header line. The symbol :: is called the scope resolution operator
for instance consider the member functions getdata() and displaydata() as discussed above they may be coded as follows
student:: getdata( f, r)
{
roll=f;
fees=r;
}
student :: displaydata()
{
std::cout<<"roll number"<<roll<<"\n";
std::cout<<"fees" <<fees<<"\n";
}
|
since these functions do not return any value, their return type is void. function arguments are declared using the ANSI prototype
The member functions have some special characteristics that are often used in the program development.
These characteristics are
1. several different classes can use the same function name. The membership label will resolve their scope.
2. member functions can access the private data of the class. A non member function can not do so.(How ever an exception to this rule is a friend function discussed later)
3. A member function can call another member function directly, without using the dot operator
for example, The function call statement s.getdata(200,89);
is valid and assigns the value 200 f and 89 to r of the object s by implementing the getdata() function. The assignment occur in the actual function
and similar the statement
s.displayData();
Inside the class Definition
Another method of defining a member function is to replace the function declaration by the actual function definition inside the class. for exmaple we could define the item class as follows
item
{
number;
cost;
:
getdata( f, r);
displaydata()
{
std::cout <<roll<<"\n";
std::cout <<fees<<"\n";
}
};
|
when a function is defined inside a class, it is treated as an inline function. Therefore all the restrictions and limitations that apply to an inline function are also applicable here.Normally, only small functions are defined inside the class definition
C++ program with class
<iostream>
using namespace std;
class student
{
roll;
fees;
:
getdata( f, r);
displaydata()
{
std::cout<<"roll no" <<"\roll" <<"\n";
std::cout<<"fees"<<fees <<"\n";
}
};
student:: getdata( f, r)
{
roll=r;
fees=f;
}
main()
{
student s;
std::cout << "\n object s"<<"\n";
s.getdata(100,288);
s.displaydata();
student t;
std::cout<<"\n object t"<<"\n";
t.getdata(300,500);
t.displaydata();
}
|
So explain this program. This program features the class student. This class contain two private variables and two public functions. The
member function getdata() which has been defined outside the class supplies values to the variables.
Note the use of statements such as
in the function of getdata(). This shows that the member functions can have direct access to private data items
The member function displaydata() has been defined inside the class and therefore behaves like an inline function.
This function displays the value the values of the private variables roll and fees.
The program creates two objects, X and Y in two different statements. This can be combined in one statements student s,t;
Output of the above program:
object s
roll no 1000
fees 288
object t
roll no 300
fees 500
|
MAKING AN OUTSIDE FUNCTION INLINE
one of the objectives of OOP is to separate the details of implementation from the class definition. it is therefore good practice to define the member functions outside the class
we can define a member function outside the class defination and skill make it inline by just using the qualifier inline in the header line of function definition
Example
class Student
{
…….
…….
:
getdata( f, r);
};
void item:: getdata( f, r)
{
fees=f;
roll=r;
}
|