C++ Class Member Functions

In C++, class methods, also known as member functions, are functions that are defined within a class and operate on the attributes and behavior of objects created from that class. They are an integral part of object-oriented programming (OOP) and play a central role in encapsulating the behavior associated with objects.

Declaration and Definition

Class methods are declared within the class definition and are associated with a specific class. They define the operations or behavior that objects of the class can perform. The declaration in the class specifies the method's name, return type, and parameters, while the definition (implementation) is provided outside the class. You can define methods within the class itself for simplicity or outside the class using the className::methodName syntax.

class MyClass { public: // Method declaration in the class void myMethod(int arg); // Method definition outside the class void myMethod(int arg) { // Method implementation } };

Access Control

Methods can have different access specifiers (public, private, or protected) to control their visibility and accessibility, just like attributes. Public methods are accessible from external code, private methods are limited to the class itself, and protected methods are accessible within the class and by derived classes.

class MyClass { public: // Public method void publicMethod() { // Method implementation } private: // Private method void privateMethod() { // Method implementation } };

Method Invocation

To invoke a class method, you create an object of the class and use the dot operator (.) to access and call the method on that object. The method operates on the specific instance of the object, allowing it to access and modify the object's attributes and perform actions associated with that object.

MyClass obj; // Create an object of the class obj.publicMethod(); // Call a public method on the object // Example with method that uses object's attribute class Circle { public: double radius; double calculateArea() { return 3.14159265359 * radius * radius; } }; Circle circle; circle.radius = 5.0; double area = circle.calculateArea(); // Call method to calculate the area

Class methods declaration

Class methods can be declared and defined in two ways:

  1. Inside the class definition: This is the most common way to declare and define class methods. The method definition is simply included within the class body.
  2. Outside the class definition: This is less common, but it can be useful in some cases. To declare a class method outside the class definition, you use the scope resolution operator (::) to specify the class to which the method belongs.
Example
// Class method defined inside the class definition: class Car { public: int speed; string color; void accelerate() { speed++; } void brake() { speed--; } }; // Class method defined outside the class definition: void Car::printSpeed() { cout << "The speed of the car is: " << speed << endl; } int main() { Car myCar; myCar.accelerate(); myCar.brake(); // Call the printSpeed() method, which is defined outside the class definition. Car::printSpeed(myCar); return 0; } //Output: The speed of the car is: 1

Conclusion

Class methods, also known as member functions, are functions defined within a class that operate on the attributes and behavior of objects created from that class. They encapsulate the behavior specific to objects and are invoked through object instances using the dot operator. Class methods play a central role in defining the actions that objects can perform and are a fundamental aspect of object-oriented programming in C++.