Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Syntax of Member function
2.
Member Function inside the Class
2.1.
C++
3.
Member Function outside the Class
3.1.
C++
4.
Accessing the Private Member Function of A Class
5.
Types of Member Functions in C++
5.1.
Non-static Member Functions
5.2.
Static Member Functions
6.
Frequently Asked Questions
6.1.
What is member function vs normal function in C++?
6.2.
What is member function and member variable in C++?
6.3.
How many member functions are there in C++?
6.4.
What is difference between member function and constructor?
7.
Conclusion
Last Updated: Jul 15, 2024
Easy

What is Member Function in C++?

Author Rahul Singh
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Member functions are functions and operators that are declared as a member of a class. It is declared inside the class in any of the visibility modes i.e. public and private, and it can access all the data members of the class.

Member functions, often referred to as methods, are functions that belong to a specific class in C++. They operate on the data members of the same class, promoting encapsulation - a vital characteristic of object-oriented programming. This ensures that data and the functions acting upon that data are housed together, offering higher security and data integrity.

Syntax of Member function

class MyClass {
private:
  int myVar;

public:
  void setMyVar(int a) { 
    myVar = a; 
  }
 int getMyVar() { 
    return myVar; 
  }
};


In the above example, setMyVar and getMyVar are member functions of MyClass.

Member Function inside the Class

A member function inside a class in C++ is a function declared and defined within the class definition. It has access to the class's private and protected members, allowing it to operate on the class's data directly. These functions are typically declared using the class's scope resolution operator (::).

  • C++

C++

#include <iostream>
using namespace std;

class MyClass {
public:
// Member function inside the class
void memberFunctionInside() {
cout << "Inside memberFunctionInside()" << endl;
}
};

int main() {
MyClass obj;

// Calling member function inside the class
obj.memberFunctionInside();

return 0;
}

 

Output:

Inside memberFunctionInside()
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Member Function outside the Class

A member function outside the class in C++ is a function declared inside the class but defined outside of it. It is declared using the class's name followed by the scope resolution operator (::) and the function name. Member functions defined outside the class have access to the class's public members but not its private or protected members.

  • C++

C++

#include <iostream>
using namespace std;

class MyClass {
public:
// Declaration of member function outside the class
void memberFunctionOutside();
};

// Definition of member function outside the class
void MyClass::memberFunctionOutside() {
cout << "Inside memberFunctionOutside()" << endl;
}

int main() {
MyClass obj;

// Calling member function outside the class
obj.memberFunctionOutside();

return 0;
}

 

Output:

Inside memberFunctionOutside()

Accessing the Private Member Function of A Class

In C++, private member functions are not directly accessible outside the class. They are intended to be used only within the class, providing a level of encapsulation. However, there are ways to access private member functions from outside the class, although these methods are generally considered bad practice and may compromise the principles of encapsulation and data hiding.

1. Friend Function: You can declare a function as a friend of the class, which grants it access to private members. This is done by using the friend keyword in the class declaration.

class MyClass {
private:
   void privateFunction() {
       // Private function implementation
   }
public:
   friend void externalFunction(MyClass& obj);
};
void externalFunction(MyClass& obj) {
   obj.privateFunction(); // Accessing private function
}

 

2. Public Member Function: Another approach is to create a public member function that, in turn, calls the private function. This way, external code can access the public function, which then internally calls the private function.

class MyClass {
private:
   void privateFunction() {
       // Private function implementation
   }
public:
   void publicFunction() {
       privateFunction(); // Accessing private function
   }
};

Types of Member Functions in C++

Member functions can be broadly categorized into two types:

Types of Member Functions

Non-static Member Functions

These are the standard member functions which are invoked using object instances.

Static Member Functions

These functions belong to the class rather than individual objects. They cannot access non-static data members or call non-static member functions directly.

Syntax of Static Member Functions

class MyClass {
public:
  static void staticFunction() { 
    // Can only access static members
  }
  
  void nonStaticFunction() { 
    // Can access both static and non-static members
  }
};

Frequently Asked Questions

What is member function vs normal function in C++?

Member functions in C++ are associated with a class and operate on class objects. Normal functions are standalone and not tied to any particular class.

What is member function and member variable in C++?

A member function in C++ is a function declared inside a class that operates on the class's data. It has access to the class's member variables and can manipulate them. Member variables are data fields declared within a class and are accessed and modified by member functions.

How many member functions are there in C++?

In C++, the number of member functions a class can have is unlimited. Classes can contain any number of member functions, including constructors, destructors, and regular member functions, depending on the requirements of the program.

What is difference between member function and constructor?

The key difference is that a constructor is a special member function responsible for initializing objects, while other member functions perform operations on the objects after initialization.

Conclusion

Member functions in C++ are instrumental in creating robust and efficient object-oriented programs. They encompass various types and characteristics, from constructors and destructors to inline and virtual functions. Each of these elements contributes to the overall functionality of a C++ class, enabling developers to create highly secure, versatile, and maintainable software. 

Recommended articles:


We hope this blog helped you to understand the concept of the Member Function in C++. You can refer to our guided paths on the Code360 platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Live masterclass