codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

40 / 87 topics
38OOP Concepts Overview39Classes and Objects40Class Methods41Constructors & Constructor Overloading42Destructors43Access Modifiers / Specifiers44Encapsulation45Abstraction46Friend Functions and Friend Classes47Operator Overloading
Tutorials/C++ Programming/Class Methods
⚡C++ Programming

Class Methods

Updated 2026-05-12
30 min read

Class Methods

In object-oriented programming (OOP), methods are functions that belong to a class. They define the behaviors of objects created from the class. Understanding how to define and use methods is crucial for creating effective C++ programs. In this tutorial, we'll explore defining methods inside and outside of classes, const member functions, and static member functions.

Introduction

Methods in C++ are functions that operate on the data (attributes) of a class. They can modify the object's state or provide information about it. Defining methods within a class makes the code more organized and encapsulated, which is a core principle of OOP. We'll also discuss const member functions, which promise not to modify the object, and static member functions, which belong to the class rather than any specific object.

Defining Methods Inside Classes

When methods are defined inside a class, their implementation is typically provided directly within the class definition. This approach keeps the method's declaration and definition together, making the code easier to read and maintain.

Example: Method Defined Inside Class

C++
1#include <iostream>
2using namespace std;
3
4class Rectangle {
5 int width, height;
6public:
7 void setValues(int w, int h) {
8 width = w;
9 height = h;
10 }
11 int area() {
12 return width * height;
13 }
14};
15
16int main() {
17 Rectangle rect;
18 rect.setValues(3, 4);
19 cout << "Area: " << rect.area() << endl; // Output: Area: 12
20 return 0;
21}
Output

In this example, the methods setValues and area are declared inside the class but defined outside it using Rectangle::.

Const Member Functions

A const member function is a method that promises not to modify any data members of the object. It can only read the data members, not change them. Declaring a method as const provides an important guarantee about the behavior of the function.

Example: Const Member Function

C++
1#include <iostream>
2using namespace std;
3
4class Rectangle {
5 int width, height;
6public:
7 void setValues(int w, int h) {
8 width = w;
9 height = h;
10 }
11 int area() const { // This method promises not to modify the object
12 return width * height;
13 }
14};
15
16int main() {
17 const Rectangle rect(3, 4);
18 cout << "Area: " << rect.area() << endl; // Output: Area: 12
19 return 0;
20}
Output

In this example, the add method is a static member function of the Math class. It can be called using the class name without creating an instance of Math.

Practical Example

Let's create a complete program that demonstrates defining methods inside and outside classes, const member functions, and static member functions.

C++
1#include <iostream>
2using namespace std;
3
4class Circle {
5 double radius;
6public:
7 void setRadius(double r) {
8 radius = r;
9 }
10 double getArea() const {
11 return 3.14159 * radius * radius;
12 }
13 static double calculateCircumference(double r) {
14 return 2 * 3.14159 * r;
15 }
16};
17
18int main() {
19 Circle myCircle;
20 myCircle.setRadius(5);
21 cout << "Area: " << myCircle.getArea() << endl; // Output: Area: 78.53975
22 cout << "Circumference: " << Circle::calculateCircumference(5) << endl; // Output: Circumference: 31.4159
23 return 0;
24}
Output
Area: 78.53975
Circumference: 31.4159

In this program, we define a Circle class with methods to set the radius, calculate the area (a const member function), and calculate the circumference (a static member function). The main function demonstrates using these methods.

Summary

  • Methods are functions that belong to a class.
  • Methods can be defined inside or outside the class using the scope resolution operator (::).
  • Const member functions promise not to modify any data members and can be called on const objects.
  • Static member functions belong to the class, not any specific object, and cannot access non-static data members directly.

What's Next?

In the next topic, we'll explore constructors and constructor overloading. Constructors are special methods that initialize objects of a class when they are created. Understanding how to define and use constructors is essential for effective C++ programming.


PreviousClasses and ObjectsNext Constructors & Constructor Overloading

Recommended Gear

Classes and ObjectsConstructors & Constructor Overloading