C++ Class Methods Explained: How to Define and Use Member Functions (With Examples)
C++ Class Methods Explained: How to Define and Use Member Functions (With Examples)
Last updated on : November 26, 2026
In todays blog post we are going to cover a very important concept of class methods or member functions in C++ programming language. First of all let us talk about some basics of classes in C++ programming language. These classes are basically blue print, and objects of the classes are the instances of these classes. And these objects can access the members of the class which are class fileds or class methods. These fields are simply variables and these class methods or member functions are simply a function. Remember that these class methods can be defined inside or outside the class body. We alternatively use these terms class methods or member functions. These member functions can basically describe the behaviour of the class objects in C++ program. And these class methods or member functions are used in order to manipulate the data members of a class directly.
Now the main question arises that how can we access these member functions or class methods with the help of the class objects. As we learned in our previous blog posts that we can access or use the class fields or variables with the help of objects and the dot (.) syntax, and the same is true for accessing the class methods or member functions.
Now we are going to look at how to define class methods inside the class and outside the class body. The first one is the simplest way to define the class method or member function, which is inside the class body. We simply write the type of function i.e, void, int, string, etc. followed by the name of the class method or member function. After that we simply write the C++ code inside the opening and closing curly brackets.
In order to define a class method or member function outside the class body, first we need to declare the member function with in the class body, by simply writing the type of function void or int, etc, followed by the name of the function and after that simply write opening and closing round brackets and write semi-colon (;) after the closing round bracket. In this way we can simply declare a class method or member function inside a class body. Now it is time to define the class method outside the class body. In order to define the class method or member function outside the class body we use scope resolution operator (::) between the class name and the class method that is declared inside the class.
Using this simple syntax we can easily declare and define class methods or member functions outside the class body.
Basic syntax and working of class methods or member functions in C++ programming language
Syntax code example 1 (Method inside class definition)
class ClassName { public: void FunctionName() // Method inside class definition. { // Write coding part here. } };
Explanation of syntax 1
In the first coding syntax example we are going to cover the first way to define class methods inside the class body. In this syntax example we have simply created a class and named it as ClassName like this,
class ClassName
And with in this class body we have used public access specifier, and under this public access specifier we have defined a class method and named it as FunctionName() with return type as void like this,
public: void FunctionName() // Method inside class definition. { // Write coding part here. }
So this is how we can define a class method or member function inside the class body.
Syntax code example 2 (Method outside class definition)
class ClassName { public: void FunctionName(); // Method outside class definition. }; void ClassName::FunctionName() { // Write coding part here. }
Explanation of syntax 2
In the second C++ coding syntax we have used the second way to define a class method or member function outside the class body. In this coding syntax we have simply created a class and named it as ClassName like this,
class ClassName
With in the class body we have used a public access specifier under which we have declared a function named as FunctionName() like this,
public: void FunctionName(); // Method outside class definition.
Now keep this thing in mind that at this point we have just declared the function inside the class body. Now we are going to define the class method or member function outside the class body with the help of scope resolution operator (::) like this,
void ClassName::FunctionName()
We simply type the return type of the function, followed by the class name (ClassName), and after that we will use scope resolution (::) operator followed by the function name which is FunctionName(). And then we can simply write the function body with in the opening and closing curly brackets like this,
void ClassName::FunctionName() { // Write coding part here. }
This is how we can define a class method or member function outside the class body.
Simple coding examples of class methods or member functions in C++ programming language
C++ code example 1 (Method inside class definition)
#include<iostream> #include<conio.h> using namespace std; class MyClass { public: void MyMethod() { cout << "Hello Muhammad Fahad" << endl; } }; void main() { MyClass obj1; obj1.MyMethod(); _getch(); }
Explanation of code 1
In the first C++ coding example we are going to look at the first way of defining a class method or member function inside the class body. In this example we have simply created a class and named it as MyClass and with in this class body we have simply defined a class method or member function named as MyMethod() under the public access specifier like this,
class MyClass { public: void MyMethod() { cout << "Hello Muhammad Fahad" << endl; } };
After that with in the main() method we have simply declared an object of the class, MyClass and named it as obj1 like this,
MyClass obj1;Then we have simply called the class method (MyMethod()) or member function with the help of class object obj1 and the dot (.) syntax like this,
obj1.MyMethod();
So this is how we can define a class method or member function inside the class body.
Output of code 1
Hello Muhammad Fahad
C++ code example 2 (Method outside class definition)
#include<iostream> #include<conio.h> using namespace std; class MyClass { public: void MyMethod(); }; void MyClass::MyMethod() { cout << "Hello Muhammad Fahad" << endl; } void main() { MyClass obj1; obj1.MyMethod(); _getch(); }
Explanation of code 2
Now in the second C++ coding example we will look at how we can define a class method or member function outside of the class body in C++ programming language. In this example we have simply created a class and named it as MyClass. After that we have we have declared a class method inside MyClass and under the public access specifier like this,
class MyClass { public: void MyMethod(); };
Then after that we have defined the class method or member function outside the class body (MyClass) using the scope resolution operator (::) like this,
void MyClass::MyMethod() { cout << "Hello Muhammad Fahad" << endl; }
We simply write the return type(void) of function followed by the name of the class (MyClass) and after that we have used the scope resolution operator (::) followed by the name of the class method or member function like this,
void MyClass::MyMethod()
After that with in the main() method we have simply declared an object of MyClass and named it as obj1 and called the class method MyMethod() like this,
MyClass obj1;
obj1.MyMethod();So, this is how we can define a class method or member function outside the class body.
Output of code 2
Hello Muhammad Fahad
Congratulations! like always we have successfully implemented defining class methods inside and outside of the class body.
Comments
Post a Comment