defining member functions
 
member functions can be defined in two places:
 
- outside-the class definition.
 
- inside the class definition.
 
it is obvious that, irrespective of the place of definition, the function should perform the same task. therefore, the code for the function body would be identical in both the cases. however, there is a subtle different in the way the function header is defined.
 
 
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 a function header and a function body. 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 instance, consider the member functions getdata( )   and putdata( ).  they may be coded as follows.
 
                                                                      void item : : getdata( int a, float b)
 
                                                                      {
 
                                                                                              number = a
 
                                                                                              cost = b
 
                                                                      }
 
                                                                      void item: :putdata(void)
 
                                                                    {
 
                                                                                              cout < < " number : " < < number< < "\n"
 
                                                                                              cout < < " cost            :" < < cost < "\n"
 
                                                                      }
 
since these functions do not return any value, their return-type is void. the member functions have some special characters that are often used in the program development.
 
·    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 cannot do so.
 
·    a member function 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. for example, we could define the item class as follows:
 
                                                                      class item
 
                                                                      {
 
                                                                                              int number
 
                                                                                              float cost
 
                                                                      public:
 
                                                                                              void getdata( int a, float b)   // declaration
 
                                                                                              // inline function
 
                                                                                              void putdata(void)                             // definition
 
                                                                                              {
 
                                                                                                                      cout < < number < < "\n"
 
                                                                                                                      cout < < cost < < "\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.
 
a c++ program with class
 
 
|
/////////////////////[ class implementation ]//////////////////////////////
 
//………………………………… include section………………………………………….
 
#include < iostream.h>
 
 
//………………………………… class declaration ………………………………………..
 
class item                                // class declaration
 
{
 
                int number       // private by default
 
                float cost
 
  public:
 
                void getdata(int a, float b) // prototype declaration
 
                void putdata(void)
 
                {
 
                                cout < < "number :" < < number < < "\n"
 
                                cout < < "coust           :"< < coust< < "\n"
 
                  }
 
}
 
//…………………………………….. member function definition ………………………….
 
void item::getdata(int a, float b)  // use membership label
 
{
 
              number = a     // private variables
 
              cost      = b           // directly used
 
}
 
// …………………………………………. main program ……………………………………..
 
int main (void)
 
{
 
            item x                                                                                                       // create object x
 
            cout < < "\n object   x "   < < "\n"
 
            x.getdata(100, 299.95)                                                 // call member function
 
            x.putdata()                                  
 
 
            item y                                                                                                       // create object y
 
            cout < < "\n object  y "  < < "\n"
 
            y.getdata(200, 175.50)                                                 // call member function
 
            x.putdata()                                  
 
            return (0)
 
}
 
//////////////////////////< program 5.1> ////////////////////////////////
  |
the program shows that:
 
-                  the member functions can have direct access to private data items.
 
-                  the member function putdata( ) has been defined inside the class and therefore behaves like an inline function.
 
-                  the program creates two object x and y in two different statements. this can be combined in one statement.
 
making an outside function
 
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 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)                     // declaration
 
                                              }
 
                                              inline void item: : getdata(int a, float b) // definition
 
                                              {
 
                                                                    number = a
 
                                                                      cost = b
 
                                              }
 
nesting of member functions
 
we just discussed that a member function of a class can be called only by an object of that class using a dot operator. however, there is an exception to this. a member function can called by using its name inside another member function of the same class. this is known as nesting of member functions. program 5.2 illustrates this feature.
|
////////////////////////[ nesting of member functions ] ///////////////////
 
// - include section
 
#include < iostream.h>
 
 
// - class section
 
 
class set
 
{
 
                      int m, n
 
            public:
 
                      void input(void)
 
                      void display(void)
 
                      int largest(void)
 
}
 
 
// member function definition -
 
 
int set : :  largest(void)
 
{
 
                if (m > = n )
 
                      return(m)
 
                else
 
                      return(n)
 
}
 
 
void set : : input( void)
 
{
 
              cout < < " input values of m and n " < < "\n"
 
        cin > > m > > n
 
}
 
 
void set : : display ( void )
 
{
 
          cout < < " largest value = "< < largest() "\n"   // calling member function
 
}
 
 
// main section -
 
 
int main ( void )
 
{
 
                set a
 
                a.input( )
 
                a.display()
 
}
 
///////////////////////////////< program 5.2 > ///////////////////////////
  |
the output of program 5.2 would be:
                                             
                                              input values of m and n
                                              30 17
                                              largest value = 30
 
private member functions
 
although 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 (like private data) from the outside calls. tasks such as deleting an account in a customer file, or providing increment to an employee are events of serious consequences and therefore the functions handling such tasks should have restricted access.  we can place these functions in the private section.
a private member function can only be called by another function that is a member of its class. even an object cannot invoke a private function using the dot operator. consider a class as defined below:
                                                                      class sample
 
                                                                      {
 
                                                                                              int m
 
                                                                                              void read(void) // private member function
 
                                                                              public:
 
                                                                                              void updating(void)
 
                                                                                              void write(void)
 
                                                                      }
 
if   s1 is an object of sample, then
                                              s1.read()                 // won t work objects cannot  access private members
is illegal. however, the function read( ) can be called by the function updating( ) to updating the value of m.
                                              void sample : : updating( void )
 
                                              {
 
                                                                      m= read( )   // simple call no object used
 
                                              }
 
 
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .