Master Member Functions in C++

Master Member Functions in C++

6 mins read9.8K Views Comment
Updated on Nov 17, 2023 16:12 IST

If you are new to C++, it is crucial to learn about the Member function. With different types of member functions in C++ as well as the methods of declaration inside and outside classes, we discuss them in detail by including some easy-to-understand programming examples. We also recommend which is the best way of declaring member function today.  

2023_01_MicrosoftTeams-image-13 p1.jpg

In C++, a member function is a function that is associated with a specific class or object. These functions are defined within the class definition and provide the ability to perform operations on the class’s data members and interact with other objects of the same class. They play a crucial role in the object-oriented programming paradigm by allowing the encapsulation of implementation details and providing a way to validate and manipulate the data stored within an object. Members are typically included in a program to add functionality to the program. For example, adding members allows you to perform mathematical calculations on data. A typical C++ program would include many members to perform various tasks. In this article we will cover types of member function and also covers member functions inside and outside the function with programming example.

Table of contents

Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
4.24 K
6 weeks
– / –
15 days
– / –
– / –
– / –
1 hours
Free
2 hours
Free
1 hours
Free
1 hours

What is a member function?

In C++, a member function is a function that is associated with a specific object or class. Member functions are defined within the class definition, and they have access to the data members and other member functions of the class. They can be used to perform operations on the object’s data and interact with objects of the same class.

A member function can be invoked by using dot notation, which is the object’s name followed by the name of the member function and its parameters. For example, if we have a class called “Person” with a member function called “speak()”, we can call this function for an object of that class called “obj1” using the notation “obj1.speak()”.

When a member function is defined as “const”, it can be called on a constant object and will not modify the object.

One important thing to remember when working with member functions is that, by default, they do not have access to the non-static member variables of the class unless this pointer is used to reference them explicitly.

In the above figure, we have data members and member functions in class named student. 

Data members or class members are nothing, just simple variables. And Member functions are nothing, just simple functions we use in a program.

Encapsulation in C++ with a Real-Life Example
All About OOPs Concepts in C++
Classes and Objects in C++

Also explore:Top C++ Interview Questions and Answers for 2023

Types of member function in C++

Normal member functions

These are the most common type of member functions, they are defined within the class definition, and they have access to the data members and other member functions of the class.

Static member functions

These member functions are defined with the “static” keyword. They do not have access to the data members of the class and can only access static data members. They can be called without creating an object of the class.

Friend member functions

These member functions are defined outside the class definition. But they have access to the private and protected members of the class. They are declared inside the class definition using the “friend” keyword.

Must read: All About C++ Friend Function

Constant member functions

These member functions are defined with the “const” keyword. They can be called on a constant object and will not modify the object. They are useful for accessing the data members of an object without modifying them.

Virtual member functions

These member functions are defined with the “virtual” keyword. They are used in polymorphism when you want a base class pointer or reference to point to a derived class object.

To know more, check All About Virtual Functions in C++

Operator overloading member functions

These member functions are used to overload operators for a class, allowing you to use operators such as +, -, *, /, etc., with objects of that class.

To know more, check out Operator Overloading in C++.

Default, copy and move constructors and assignment operators

They are also types of member functions; they are special member functions that are automatically generated by the compiler when needed.

Also read Constructors in C++ and its Types.

Function Overloading in C++
Understanding Operators in C++
Exception handling in C++

Ways to declare number function

There are two ways to declare a Member function.

  1. Member function declared within the class.
  2. Member function declared outside the class.

We will now understand what will happen if we define member function within and outside the class. We will see this with programming examples.

Member Function inside the Class

Member functions are defined inside the class definition and have access to the data members and other member functions. They are associated with a specific class or object and can be used to perform operations on the class’s data members and interact with other objects of the same class.

To define a member function inside a class, you simply include the function’s signature and implementation within the class definition like this:


 
class MyClass {
public:
int x;
void assignX(int value) {
x = value;
}};
Copy code

Once a member function is defined inside a class, it can be called on an object of that class using the dot notation like this:


 
MyClassobj;
obj.assignX(5);
Copy code

You can define Member functions as private or protected members of a class, meaning they can only be accessed by other member functions or friends of the same class in which you declared a member function.

Example


 
#include<iostream>
class MyClass
{
public:
int data=5;
void printData()
{
std::cout << "Data " << data;
}};
int main()
{
MyClass obj;
obj.printData();
return 0;
}
Copy code

Instead of declaring the function outside the class and defining it later, the function printData is defined and declared within the class, which is known as an inline function. This program will also output “Data 5” when it runs.

Member Function outside the Class

In C++, a member function can be defined outside the class definition using the scope resolution operator (::) followed by the function name. This is known as an “out-of-class” or “off-class” definition. The function name must include the class name and the scope resolution operator before it and the function’s return type to indicate that it is a member function.

For example, if you have a class called “MyClass” with a member function called “myFunction,” the out-of-class definition might look like this:


 
void MyClass::myFunction() {
// function body
}
Copy code

Note: It’s important to remember that the out-of-class definition must match the original class declaration regarding the return type, parameters, and const qualification.

Example

Here is an example of a class called “MyClass” that has a member function called “printData” that is defined outside of the class:


 
#include<iostream>
class MyClass
{
public:
int data=5;
void printData();
};
void MyClass::printData() {
std::cout << "Data " << data;
}
int main()
{
MyClass obj;
obj.printData();
return 0;
}
Copy code

Output

What is a member function?

Data 5

This is a simple C++ program that defines a class called “MyClass” with a public member variable “data” and a public member function “printData”. The “main” function creates an object of the “MyClass” class and calls the “printData” function on that object, which prints the value of “data” to the console using the “cout” function from the “iostream” library. The program will output “Data 5” when it runs.

void MyClass::printData() is written to define the member function outside the class Myclass.

Conclusion

A C++ member function is a function that is used within a C++ class. Member functions can be defined inside or outside a class (although outside is recommended). Using member functions improves code modularity and improves code reusability. If you liked this article please like it and share it with your friends.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio