When a member function is defined outside of the class?

Member functions are the functions, which have their declaration inside the class definition and works on the data members of the class. The definition of member functions can be inside or outside the definition of class.

If the member function is defined inside the class definition it can be defined directly, but if its defined outside the class, then we have to use the scope resolution :: operator along with class name alng with function name.

For example:

class Cube
{
    public:
    int side;
    /*
        Declaring function getVolume 
        with no argument and return type int.
    */
    int getVolume();     
};

If we define the function inside class then we don't not need to declare it first, we can directly define the function.

class Cube
{
    public:
    int side;
    int getVolume()
    {
        return side*side*side;      //returns volume of cube
    }
};

But if we plan to define the member function outside the class definition then we must declare the function inside class definition and then define it outside.

class Cube
{
    public:
    int side;
    int getVolume();
}

// member function defined outside class definition
int Cube :: getVolume()
{
    return side*side*side;
}

The main function for both the function definition will be same. Inside main() we will create object of class, and will call the member function using dot . operator.


Calling Class Member Function in C++

Similar to accessing a data member in the class, we can also access the public member functions through the class object using the dot operator (.).

Below we have a simple code example, where we are creating an object of the class Cube and calling the member function getVolume():

int main()
{
    Cube C1;
    C1.side = 4;    // setting side value
    cout<< "Volume of cube C1 = "<< C1.getVolume();
}

Volume of cube C1 = 16

Similarly we can define the getter and setter functions to access private data members, inside or outside the class definition.

Hello everyone, in this tutorial, we will learn how we can define a member function outside the class in C++. A member function of a class is a function that is declared or defined within the class definition.

In this post, we are going to discuss two ways to define a member function of a class which is given below:

  • Within the class
  • Outside the class

Let’s see how a member function in C++ can be defined outside the class.

If you are not very much familiar with the working of class and object in C++, read this first: Classes and objects in CPP

C++ program to define a member function outside the class

We are going to use the Scope Resolution Operator(::) to define a function outside the class. Remember that the function must be declared inside the class. Have a look at the following program to understand the use of the scope resolution operator to define a member function outside of a class.

#include <iostream>

using namespace std;

class Square
{
  public:
    int side;
    
    int area(int side);
    int perimeter(int side);
    
};

int Square::area(int side)
{
  return side*side;
}

int Square::perimeter(int side)
{
  return 4*side;
} 

int main()
{
  //creating object of Square class
  Square s;
  
  s.side = 5;
  
  cout << "Area of a square of side "<< s.side << " is " << s.area(s.side) << "\n";
  cout << "Perimetr of a square of side " << s.side << " is " << s.perimeter(s.side) ;
 	
  return 0;
}

And, the output of the example program is:

Area of a square of side 5 is 25
Perimetr of a square of side 5 is 20

Explanation: As you can see, first we have created a class Square that has an integer variable side and member functions area() and perimeter().

We have not given the definition of the functions inside the class instead we have defined function area() and perimeter() outside the class Square using scope resolution operator as demonstrated in the above example program.

In the main() function, we have created an object of the Square class and set the side variable for this object as 4 and called the member functions area() and perimeter() which prints out the area and perimeter of the square respectively. That is 25 and 20.

Member functions of a class can be defined either outside the class definition or inside the class definition. In both the cases, the function body remains the same, however, the function header is different.

Outside the Class: Defining a member function outside a class requires the function declaration (function prototype) to be provided inside the class definition. The member function is declared inside the class like a normal function. This declaration informs the compiler that the function is a member of the class and that it has been defined outside the class. After a member function is declared inside the class, it must be defined (outside the class) in the program.

The definition of member function outside the class differs from normal function definition, as the function name in the function header is preceded by the class name and the scope resolution operator (: :). The scope resolution operator informs the compiler what class the member belongs to. The syntax for defining a member function outside the class is

Return_type class_name :: function_name (parameter_list) {
  // body of the member function
}

To understand the concept of defining a member function outside a class, consider this example.

Example : Definition of member function outside the class

class book {
  // body of the class
};
 void book :: getdata(char a[],float b) {
    // defining member function outside the class
    strcpy(title,a):
    price = b:
}
  void book :: putdata () {
    cout<<"\nTitle of Book: "<<title;
    cout<<"\nPrice of Book: "<<price;
}

Note that the member functions of the class can access all the data members and other member functions of the same class (private, public or protected) directly by using their names. In addition, different classes can use the same function name.

Inside the Class: A member function of a class can also be defined inside the class. However, when a member function is defined inside the class, the class name and the scope resolution operator are not specified in the function header. Moreover, the member functions defined inside a class definition are by default inline functions.

To understand the concept of defining a member function inside a class, consider this example.

Example : Definition of a member function inside a class

class book {
  char title[30];
  float price;
  public:
     void getdata(char [],float); II declaration
     void putdata()//definition inside the class {
     cout<<"\nTitle of Book: "<<title;
     cout<<"\nPrice of Book: "<<price;
} ;

In this example, the member function putdata() is defined inside the class book. Hence, putdata() is by default an inline function.

Note that the functions defined outside the class can be explicitly made inline by prefixing the keyword inline before the return type of the function in the function header. For example, consider the definition of the function getdata().

Which function of a class is defined outside that class?

A member function is declared in the class but defined outside the class and is called using the object of the class.

When a member function is defined outside the class definition the function name is preceded by the?

The definition of member function outside the class differs from normal function definition, as the function name in the function header is preceded by the class name and the scope resolution operator (: :). The scope resolution operator informs the compiler what class the member belongs to.

How we can define member function outside the class Mcq?

ANSWER: Using scope resolution Scope resolution operator is used to define a function outside the class or when we want to use a global variable but also has a local variable with the same name.

How is inline member function defined outside the body of a class?

An equivalent way to declare an inline member function is to either declare it in the class with the inline keyword (and define the function outside of its class) or to define it outside of the class declaration using the inline keyword.