Defining Member Functions in C++

An important difference between a member function and a normal function is that a member function incorporates a membership.Identify label in the header. The ‘label’ tells the compiler which class the function belongs to. Member can be defined in two places:

  1. inside the class definition
  2. outside the class function

1. inside Class Definition

A method of defining a member function is to replace the function declaration by the actual function definition inside the class .


class item
    int number; 
    float cost;
        void getdata (int a ,float b);
        void putdata(void)
          cout<<number<<endl; cout<<cost<<endl;

Here,For the method putdata() method declaration and definition are given inside the class. but, for the method getdata() is just defined is written inside the class but body is not declared for the function. For such method we have to declare the body of method outside of class.

2. Outside class definition

Member function that are declared inside a class have to be defined separately outside the class. Their definition are very much like the normal functions. This can be done using the scope resolution operator ( :: ).


return type class-name :: function-name(argument declaration )

The member ship 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.


void item :: getdata (int a , float b )
void item :: putdata ( void)
   cout<<”number=:”<<number<<endl; cout<<”cost=”<<cost<<endl;

The member function have some special characteristics 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 can’t do so. A member function can call another member function directly, without using the dot operator.

Related posts