The 2 types of C++ Class Methods: Inside Class Definition and Outside Class Definition

Marika Lam
2 min readMay 25, 2022

Inside Class Definition

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;
}

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

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

Functions defined inside the class are treated as inline functions automatically if the function definition doesn’t contain looping statements or complex multiple line operations.

Outside Class Definition

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;
}

To define a function outside the class definition, you have to declare it inside the class and then define it outside of the class. This is done by specifiying the name of the class, followed the scope…

--

--