When a member function is defined outside of the class declaration the function name?

Member functions of the class can be defined at two places

1) Outside the class definition

2) Inside the class definition

Irrespective of the place of definition, the function performs the same operation. Hence, code for the function body is identical in both the cases. Only function header is changed.

1)Outside the class definition

a) The general form of member function definition

return-type class-name : : function-name (Argument declaration)
{
   function body
}

outside the class definition is

b) The member function incorporates an identity label or membership label (i.e., class-name: :)

c)  This label tells the compiler the class to which the function belongs and restricts the scope of that function the objects of the class ‘class-name’ specified in the header line.

e.g.

//class definition
class try 1
{
   public:
   void display (void);
};
//member function definition outside class
void try 1 : : display (void)
{
   cout<<"Programming is func";
}

2) Inside the class definition
a) Another method for defining a member function is to replace the function declaration by the actual function definition.

e.g.

class try 1
{
  public:
  void display (void)
  { }
};

b) When a function is defined inside a class, it is treated as an inline function. Normally, only small functions are defined inside the class definition.

  1. Last updated
  2. Save as PDF
  • Page ID29114
  • Member Functions in Classes 

    There are 2 ways to define a member function:

    • Inside class definition
    • Outside class definition

    To define a member function outside the class definition we have to use the scope resolution :: operator along with class name and function name.

    // C++ program to demonstrate function 
    // declaration outside class 
    
    #include <bits/stdc++.h> 
    using namespace std; 
    class Courses 
    { 
        public: 
        string coursename; 
        int id; 
        
        // printname is not defined inside class definition 
        void printname(); 
        
        // printid is defined inside class definition 
        void printid() 
        { 
            cout << "Course id is: " << id; 
        } 
    }; 
    
    // Definition of printname using scope resolution operator :: 
    void Courses::printname() 
    { 
        cout << "Course name is: " << coursename; 
    } 
    
    int main() { 
        
        Courses thisObj; 
        thisObj.coursename = "CSP 31A"; 
        thisObj.id=976; 
        
        // call printname() 
        // This is defined outside of the class Courses
        thisObj.printname(); 
        cout << endl; 
        
        // call printid() 
        // This is defined inside of the class Courses
        thisObj.printid(); 
        return 0; 
    } 
    

    In this code we have 2 separate ways to provide output. One is the method printid(), which is a method of the Courses class, it is defined inside of the class definition because it is between the opening an d closing brackets of the block of code which is the class definition.

    We also have the method printname(), which is defined outside of the class definition. It is still a method of the class Courses, because as we can see it is specifically defined as void Courses::printname() using the scope resolution operator ::. Also, notice that we do declare printname() inside the class, but provide no body to the code.

    Both of these methods belong to  the class Courses, and you have to call them with the object dot manner in your code. 

    // printname() method prints this statement
    Geekname is: CSP 31A
    
    // printid() method prints this statement
    Geek id is: 976
    

    Note that all the member functions defined inside the class definition are by default inline, but you can also make any non-class function inline by using keyword inline with them. Inline functions are actual functions, which are copied everywhere during compilation, like pre-processor macro, so the overhead of function calling is reduced.

    Note: Declaring a friend function is a way to give private access to a non-member function.

    Adapted from:
    "C++ Classes and Objects" by BabisSarantoglou, Geeks for Geeks is licensed under CC BY-SA 4.0


    A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.

    Let us take previously defined class to access the members of the class using a member function instead of directly accessing them −

    class Box {
       public:
          double length;         // Length of a box
          double breadth;        // Breadth of a box
          double height;         // Height of a box
          double getVolume(void);// Returns box volume
    };
    

    Member functions can be defined within the class definition or separately using scope resolution operator, : −. Defining a member function within the class definition declares the function inline, even if you do not use the inline specifier. So either you can define Volume() function as below −

    class Box {
       public:
          double length;      // Length of a box
          double breadth;     // Breadth of a box
          double height;      // Height of a box
       
          double getVolume(void) {
             return length * breadth * height;
          }
    };
    

    If you like, you can define the same function outside the class using the scope resolution operator (::) as follows −

    double Box::getVolume(void) {
       return length * breadth * height;
    }
    

    Here, only important point is that you would have to use class name just before :: operator. A member function will be called using a dot operator (.) on a object where it will manipulate data related to that object only as follows −

    Box myBox;          // Create an object
    
    myBox.getVolume();  // Call member function for the object
    

    Let us put above concepts to set and get the value of different class members in a class −

    #include <iostream>
    
    using namespace std;
    
    class Box {
       public:
          double length;         // Length of a box
          double breadth;        // Breadth of a box
          double height;         // Height of a box
    
          // Member functions declaration
          double getVolume(void);
          void setLength( double len );
          void setBreadth( double bre );
          void setHeight( double hei );
    };
    
    // Member functions definitions
    double Box::getVolume(void) {
       return length * breadth * height;
    }
    
    void Box::setLength( double len ) {
       length = len;
    }
    void Box::setBreadth( double bre ) {
       breadth = bre;
    }
    void Box::setHeight( double hei ) {
       height = hei;
    }
    
    // Main function for the program
    int main() {
       Box Box1;                // Declare Box1 of type Box
       Box Box2;                // Declare Box2 of type Box
       double volume = 0.0;     // Store the volume of a box here
     
       // box 1 specification
       Box1.setLength(6.0); 
       Box1.setBreadth(7.0); 
       Box1.setHeight(5.0);
    
       // box 2 specification
       Box2.setLength(12.0); 
       Box2.setBreadth(13.0); 
       Box2.setHeight(10.0);
    
       // volume of box 1
       volume = Box1.getVolume();
       cout << "Volume of Box1 : " << volume <<endl;
    
       // volume of box 2
       volume = Box2.getVolume();
       cout << "Volume of Box2 : " << volume <<endl;
       return 0;
    }
    

    When the above code is compiled and executed, it produces the following result −

    Volume of Box1 : 210
    Volume of Box2 : 1560
    

    cpp_classes_objects.htm

    When a member function is defined outside of the class declaration?

    If a member function's definition is outside the class declaration, it is treated as an inline function only if it is explicitly declared as inline . In addition, the function name in the definition must be qualified with its class name using the scope-resolution operator ( :: ).

    What is called when a function is defined outside a class?

    A public member function can also be defined outside of the class with a special type of operator known as Scope Resolution Operator (SRO); SRO represents by :: (double colon)

    When a member function is defined inside of the class declaration?

    If a member function is defined inside a class declaration, it is treated as an inline function, and there is no need to qualify the function name with its class name. Although functions defined inside class declarations are already treated as inline functions, you can use the inline keyword to document code.

    How member function define inside and outside the class?

    A member function can be defined inside the class body where it is declared. Function's entire body is defined inside class body. Outside the class. A member function can be defined outside the class. To define a function outside the class, scope resolution operator is used.