← Back to C++ Course | Chapter 8: OOP Core | Lesson 3 of 14

C++ Class Methods

A class method (member function) is a function declared inside a class that operates on that class's objects, defined either inline within the class body or separately using the scope resolution operator.
Syntax
cpp
class ClassName {
public:
  return_type method_name(parameters) {
    // body
  }
};

ObjectName.method_name(arguments);

What is a Class Method?

A class method, also called a member function, is a function declared as part of a class, and every object of that class can call it, giving the method automatic access to that specific object's data.

Example: What is a Class Method?

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Greeter
class Greeter {
// Members below can be accessed from outside the class
public:
	void greet() { std::cout << "Hello!" << std::endl; }
};

// Program execution starts in main()
int main() {
	Greeter g;
	g.greet();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Defining a Method Inline

A method's body can be written directly inside the class definition, called an inline definition, which is convenient for short methods but keeps the class declaration longer.

Example: Defining a Method Inline

cpp
#include <iostream>

class Box {
public:
	int getSize() { return 10; } // defined inline, right inside the class
};

int main() {
	Box b;
	std::cout << b.getSize() << std::endl;
	return 0;
}

Defining a Method Outside the Class

A method can be declared inside the class but defined later outside it, using the class name followed by :: (the scope resolution operator) to indicate which class the definition belongs to.

Example: Defining a Method Outside the Class

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Box
class Box {
// Members below can be accessed from outside the class
public:
	int getSize();
};

int Box::getSize() {
	// Send 20 back to the caller
	return 20;
}

// Program execution starts in main()
int main() {
	Box b;
	// Print to the console with cout
	std::cout << b.getSize() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Methods that Return a Value

A method can return a value just like a regular function, computed from the calling object's own member variables, letting the object provide information about its own state.

Example: Methods that Return a Value

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Circle
class Circle {
	// Declare radius and set it to 2.0
	double radius = 2.0;
// Members below can be accessed from outside the class
public:
	double area() { return 3.14159 * radius * radius; }
};

// Program execution starts in main()
int main() {
	Circle c;
	// Print to the console with cout
	std::cout << c.area() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Methods that Take Parameters

A method can accept its own parameters in addition to having access to the object's member variables, letting it combine external input with the object's internal state.

Example: Methods that Take Parameters

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Calculator
class Calculator {
// Members below can be accessed from outside the class
public:
	int add(int a, int b) { return a + b; }
};

// Program execution starts in main()
int main() {
	Calculator calc;
	// Print to the console with cout
	std::cout << calc.add(3, 4) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Defining a method outside the class without ClassName::.
  2. Forgetting to declare a method inside the class before defining it outside.
  3. Forgetting const on methods that do not change data, so they cannot be called on const objects.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.