🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C++ Notes
Topic #88

C++ Class Methods


Class Methods

Methods are functions that belongs to the class.

There are two ways to define functions that belongs to a class:

  • Inside class definition
  • Outside class definition

Define a Method Inside the Class

In the following example, we define a function inside the class, and we name it "myMethod".

Note: You access methods just like you access attributes; by creating an object of the class and using the dot syntax (.):

Inside Example

class MyClass {        // The class

public:
// Access specifier
    void myMethod() {  // Method/function
defined inside the
class
      cout << "Hello World!";

}
};

int main() {
  MyClass
myObj;     // Create an object of MyClass

myObj.myMethod();  // Call the method
  return 0;
}

Define a Method Outside the Class

Sometimes it is better to declare the method in the class and define it later (especially in large programs).

This is done by specifiying the name of the class, followed the scope resolution :: operator, followed by the name of the function:

Outside Example

class MyClass {        // The class

public:
// Access specifier
    void myMethod();   // Method/function
declaration
};

// Method/function definition outside the class
void
MyClass::myMethod() {
  cout << "Hello World!";
}

int main() {
  MyClass
myObj;     // Create an object of MyClass

myObj.myMethod();  // Call the method
  return 0;
}

Parameters

You can also pass values to methods just like regular functions:

Example

#include <iostream>
using namespace std;

class Car {

public:
    int speed(int maxSpeed);
};

int Car::speed(int maxSpeed) {
  return maxSpeed;
}

int main() {

Car myObj; // Create an object of Car
  cout << myObj.speed(200); //
Call the method with an argument
  return 0;
}

Challenge Task

Create a class Dog with a method bark() that prints "Woof!".

Then call that method from main() using an object of the class.

#include <iostream>
using namespace std;

class Dog {
  public:
    void bark() {
      cout << "Woof!";
    }
};

int main() {
  Dog myDog;
  myDog.bark();
  return 0;
}

Want to go beyond the notes?

Join CodingNow 2.0's C++ course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C++ Class Methods – FAQs

Quick answers about learning C++ Class Methods in C++.

This free note from CodingNow 2.0 explains C++ Class Methods in C++ — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C++ topic on CodingNow 2.0, including C++ Class Methods, is 100% free with no signup required.
With focused practice, most students grasp C++ Class Methods in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now