Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Member Functions of Classes in C++

Member Functions of Classes in Cpp

What are member function in C++?

A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. In other words member functions are the functions, which have their declaration inside the class definition and works on the data members of the class. The member function can be defined both inside or outside the class definition. If the member function is defined inside the class definition it can be defined directly, but if it is defined outside the class, then we have to use the scope resolution operator (::) along with class name along with function name.

Defining a member function.

Member functions can be defined:-

  1. Within the class.

  2. Outside the class.

1.) Defining member function within the class.

To define the function within class, we do not need to declare it first, we can directly define the function.

class Square
{
    public:
    int side;
    int getArea()
    {
        return side*side;      //Returns area of square.
    }
}

2.) Defining member function outside the class.

To define the member function outside the class definition then we must declare the function inside class definition and then define it outside. We have to use scope resolution operator (::) along with the class name and function name.

class Square
{
    public:
    int side;
    int getArea();
}

//Member function defined outside class definition.
int Square :: getArea()
{
    return side*side;
}

Calling Class Member Function in C++

After defining the member functions, the point comes, how to access a member function. We can access a member function within a class, from another member function or from main() function, depending upon our need. There is no limit on how many times we can call a member function. For calling a member function, we create a object of the class, and  we use dot (.) operator along with the name and parameter list of the function which we want to call.

Syntax for calling a member function

  • objectname.functionname(argument1, argument2…);

Let’s see a CPP code for calling a member function

CPP code for how to call a member function

#include <iostream>

using namespace std;
class Square
{
    public:
    int side;
    int getArea()
    {
        return side*side;      //returns volume of cube
    }
};

class Squares 
{ 
    public: int side; 
    int getArea1(); 
    
};
//Member function defined outside class definition. 
int Squares :: getArea1() 
{ 
    return side*side; 
    
}
int main()
{
    Square S1; Squares S2;
   
    S1.side=4;
    S2.side=5;// setting side value
    cout<< "Area of square = "<< S1.getArea();
    cout<< "\nArea of square = "<< S2.getArea1();
}
Output
Area of square 16
Area of square 25