Member Functions



Member Functions


1.   Defining Member Functions
The data members of a class must be declared within the body of the class, whereas member functions of the class can be defined in following ways:
·        Outside the class definition
·        Inside the class definition

·       Outside the Class Definition
Member functions that are declared inside a class have to be defined separately outside the class. Their definitions are very much like the normal functions. They should have function header and a function body.
An important difference between a member function and a normal function is that a member function incorporates a membership 'identity label' in the header. This label tells the compiler which class the function belongs to. The general form of a member function definition is:
return- type class-name :: function-name (argument declaration)
{
Function Body
}
The membership label class-name :: tells the compiler that the function function- name belongs to the 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 Example:

void Item :: getdata (int a, float b)
{
number = a;
cost = b;
}

void Item :: putdata (void)
{
cout<<”Number:”<<number<<”\n”;
cout<<”Cost:”<<cost<<”\n”;
}
The member functions have some special characteristics which are: -
·        Several different classes can use the same function name. The membership label will resolve their scope.
·        Member functions can access the private data of the class. A non- member function
·        A member function can call another member function directly, without using the dot operator.


·       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. Normally small functions are defined inside the class definition. Example:

class Item
{
int number;
float cost;
public:
void getdata(int a, float b);               // declaration
void putdata(void)                            // definition inside the  & Inline Functionclass
{
cout<<numher<<"\n";
cout <<cost<<"\n";
}
};
When a function is defined inside the class, it is treated as inline function.

A C++ Program using Class

#include<iostream.h>
#include< conio.h>
class date
{
int day;                       // private by default
int month;                  // private by default
int year;                      // private by default
public:
void getdata(int x,int y,int z);                      // function declaration
void display()                                               // function defined inside the class
{
cout<< day<<month<<year <<endl;
}
};

void date :: getdata(int x,int y,int z)             // membership label
{                                                                       // member function definition
day=x;
month=y;
year=z;
}

void main()
{
clrscr();
date d1,d2;                              // object creation
d1.getdata(30,12,2012);         // call member function
d2.getdata(23,11,2011)          // call member function
cout<< “ you have entered following date”;
d1.display();
d2.display();
getch();
}


·       Making an Outside Function Inline

We can define a member function outside the class definition and still make it inline by just using the qualifier inline in the header line of function definition, Example:
class Item
{
 ……….
 ……….

public;
void getdata (int a, float b);                      // declaration

};

inline void Item :: getdata (int a, float b)           // definition
{
number = a;
cost = b;
}


2.   Private Member Functions
Although it is normal practice to place all the data members in the private section and all the functions in public, some situations require certain functions to be hidden (like private data) from the outside calls.
A private member function only be called by another function that is a member of this class. Even an object cannot invoke private function using dot operator. Consider the following example:

class sample
{
int m;
void read();             // private member function
public:
void update();
void write();
};
If s1 is an object of class sample, then
s1.read();                             // won’t work
However, the function read() can be called by the function update() to update the value of m .

void sample :: update(void)
{
read();                                  // simple call; no object used
}




















Comments

Popular posts from this blog

ASCII / ISCII / UNICODE

CISC / RISC / EPIC

Evolution of Computers