-
Hajipur, Bihar, 844101
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.
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:
Attributes → variables that hold data.
Methods → functions that perform actions on that data.
For example, a Car class may have attributes like brand and speed, and methods like start(), accelerate(), and brake().
#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).
In C++, you can define class methods inside the class or outside 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.
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.
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.
Methods can take parameters, return values, or both—just like normal functions.
#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:
setData() sets the values of length and width.
area() calculates and returns the area.
This shows how methods interact with class data.
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.
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.
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.
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.
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.
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.
Write a C++ program to define a class Student with data members for name and marks. Add a method display() to show the student’s details.
Create a class Rectangle with methods setData(int, int) and area(). Use these methods to calculate and display the area of a rectangle.
Define a class Car with data members brand and speed. Add methods accelerate() and brake() to modify the speed and display it.
Write a C++ program that defines a class Employee with methods setDetails() and showDetails() to assign and print employee information.
Create a class Calculator with methods add(), subtract(), multiply(), and divide() that perform operations on two numbers.
Define a class BankAccount with private data members for account number and balance. Add methods for deposit(), withdraw(), and displayBalance().
Write a program that defines a class Time with methods to set hours and minutes, and another method to add two time objects.
Create a class Book that contains title and price. Define a method comparePrice(Book b) that compares two books and displays the one with a higher price.
Write a C++ program that demonstrates how a method defined outside the class (using ::) can access class data members.
Define a class Box with a method volume() that calculates and returns the volume. Call this method from main() using an object.