In Object-Oriented Programming, methods (also called member functions) are the actions that objects can perform.
While data members represent the state of an object, methods define its behavior.
In C++, methods are defined inside classes and are used to manipulate or access the class data. Understanding how to create and use class methods effectively is a key step in mastering object-oriented programming.
What Are Class Methods?
A class method is a function that belongs to a class.
It operates on the data members of that class and can be called through its objects.
Think of a class as a combination of:
For example, a Car class may have attributes like brand and speed, and methods like start(), accelerate(), and brake().
Example of a Simple Class with Methods
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int speed;
void start() {
cout << brand << " is starting..." << endl;
}
void accelerate(int value) {
speed += value;
cout << "Speed increased to " << speed << " km/h" << endl;
}
};
int main() {
Car c1;
c1.brand = "Honda";
c1.speed = 50;
c1.start();
c1.accelerate(20);
return 0;
}
Here, start() and accelerate() are methods that perform actions using the object’s data members (brand and speed).
Defining Methods Inside and Outside a Class
In C++, you can define class methods inside the class or outside the class.
1. Defining Methods Inside the Class
When a method is defined inside the class, it is automatically treated as an inline function (the compiler replaces the function call with the function code during compilation).
Example:
class Student {
public:
string name;
int marks;
void display() {
cout << "Name: " << name << ", Marks: " << marks << endl;
}
};
Here, display() is defined inside the class itself.
2. Defining Methods Outside the Class
To define methods outside the class, you must use the scope resolution operator (::).
Example:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int marks;
void display(); // declaration
};
// method defined outside the class
void Student::display() {
cout << "Name: " << name << ", Marks: " << marks << endl;
}
int main() {
Student s1;
s1.name = "Aditi";
s1.marks = 85;
s1.display();
return 0;
}
Using the :: operator makes the code more organized, especially when a class has multiple methods.
Accessing Methods Using Objects
To call a class method, you must first create an object and use the dot operator (.).
Example:
Student s1;
s1.display();
Here, s1 is an object of Student, and we use the dot operator to call its display() method.
Using Parameters and Return Values in Methods
Methods can take parameters, return values, or both—just like normal functions.
Example:
#include <iostream>
using namespace std;
class Rectangle {
public:
int length, width;
void setData(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
};
int main() {
Rectangle r1;
r1.setData(10, 5);
cout << "Area: " << r1.area() << endl;
return 0;
}
Here:
This shows how methods interact with class data.
Returning Objects from Methods
Sometimes, a method can return an object instead of simple data.
Example:
#include <iostream>
using namespace std;
class Box {
public:
int length;
Box(int l) {
length = l;
}
Box combine(Box b) {
Box temp(length + b.length);
return temp;
}
void show() {
cout << "Length: " << length << endl;
}
};
int main() {
Box b1(5), b2(10);
Box b3 = b1.combine(b2);
b3.show();
return 0;
}
Here, the combine() method returns a new object by combining data from two objects.
Private Methods
Methods can also be private.
Private methods can only be called by other methods within the same class, not from outside the class.
Example:
#include <iostream>
using namespace std;
class Account {
private:
int balance = 5000;
void updateLog() {
cout << "Transaction recorded." << endl;
}
public:
void withdraw(int amount) {
if (amount <= balance) {
balance -= amount;
cout << "Withdrawn: " << amount << endl;
updateLog(); // calling private method
} else {
cout << "Insufficient balance!" << endl;
}
}
};
int main() {
Account a1;
a1.withdraw(1500);
return 0;
}
Here, updateLog() is private and can only be accessed from inside the class.
Passing Objects as Parameters in Methods
You can pass one object to another method as an argument.
Example:
#include <iostream>
using namespace std;
class Time {
public:
int hours, minutes;
void setTime(int h, int m) {
hours = h;
minutes = m;
}
void addTime(Time t1, Time t2) {
minutes = t1.minutes + t2.minutes;
hours = t1.hours + t2.hours + (minutes / 60);
minutes = minutes % 60;
}
void display() {
cout << hours << " hrs " << minutes << " mins" << endl;
}
};
int main() {
Time t1, t2, t3;
t1.setTime(2, 45);
t2.setTime(3, 30);
t3.addTime(t1, t2);
t3.display();
return 0;
}
This example demonstrates how methods can operate using multiple objects.
Inline Methods
When a method is defined inside the class, it automatically becomes inline.
However, you can also explicitly make an outside method inline.
Example:
inline void Student::display() {
cout << "Inline method called" << endl;
}
Inline functions reduce function call overhead, but they’re best used for short functions.
Constant Methods
A constant method ensures that the function doesn’t modify any class data.
Example:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
void display() const {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
Adding const after the function declaration prevents it from changing member variables.
Summary of the Tutorial
In this tutorial, you learned:
-
What class methods are and how they define object behavior.
-
How to define methods inside and outside of a class.
-
How to use parameters, return values, and objects in methods.
-
How to work with private, inline, and constant methods.
-
How methods interact with data members and other objects.
Mastering class methods will make your C++ programs modular, organized, and easy to extend.
In the next chapter, we’ll learn about constructors — special methods used to initialize objects automatically.