Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - member functions _ http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
                             Member Functions                         _
   Member  functions  allow  the  operations  required  to  implement  an
   abstract  data type to be encapsulated within the type itself.  Member
   functions  are declared within the class body. A declaration  consists
   of the function prototype, thus:

   class Test
   {
   int num;
   float fl;
   protected:
   char *ptr;
   public:
   void set(int i, float f, char *p = NULL);
   int getint(void) { return num; }
   float getfloat(void) { return fl; }
   };

   It  is  also permitted to include the function definition  within  the
   class  body, as in the function getint and getfloat  above.  Functions
   defined  within  the class body are automatically  handled  as  inline
   functions.  Where  a function is used to provide limited access  to  a
   private  class  variable,  it is normally referred  to  as  an  access
   function.

   Larger  member  functions are defined outside of the class  body.  The
   class  name  followed by the scope resolution operator :: is  used  to
   enable the compiler to be told which class the function being  defined
   belongs to. In the above example the function definition of set  might
   be:

   #include <test.hpp>

   void Test::set(int i, float f, char *p = NULL)
   {
   num = i;
   fl = f;
   ptr = p;
   }
   The  use  of syntax char *p = NULL in the function definition  is  not
   required, but serves as a reminder that the value is defaulted.

   Class  member functions can have default initializers in the same  way
   that non-member functions can. In the example above the declaration of
   the Test::set function in class Test has a default initializer for  p,
   if the third parameter is omitted from the function invocation a  NULL
   pointer will be passed to the called function in its place.

   Class  member  functions differ from normal functions in a  number  of
   important ways:

   A  member function can access both public and private members  of  its
   class,  and  if  it is a derived class to  the  protected  and  public
   members  of  its base class. Ordinary functions have  access  only  to
   public  data  members  (unless declared as  friend  functions  to  the
   class). Apart from the situation with inherited class mentioned above,
   member functions of one class do not have any special privileges  with
   respect to data members of another class.

   Member functions are defined within the scope of their class,  whereas
   ordinary functions are defined at file scope. One consequence of  this
   is that member functions are only overloaded by other member functions
   of  the  same  class, since overloaded functions must  have  the  same
   scope.


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson