Defining and Using Member Functions in C++


Member functions in C++ are functions defined within a class. They operate on the data members of the class and are used to define the behavior of objects. This article explains how to define and use member functions with examples.

Defining Member Functions

Member functions can be defined in two ways:

  • Inside the class definition: The function is defined directly within the class.
  • Outside the class definition: The function is declared in the class and defined later using the scope resolution operator ::.

Defining Member Functions Inside the Class

Example:

    #include <iostream>

    class Calculator {
    public:
        int add(int a, int b) {
            return a + b; // Function defined inside the class
        }
    };

    int main() {
        Calculator calc;
        std::cout << "Sum: " << calc.add(5, 3) << std::endl;
        return 0;
    }
        

Output:

    Sum: 8
        

Defining Member Functions Outside the Class

Example:

    #include <iostream>

    class Calculator {
    public:
        int multiply(int a, int b); // Function declared in the class
    };

    // Function defined outside the class
    int Calculator::multiply(int a, int b) {
        return a * b;
    }

    int main() {
        Calculator calc;
        std::cout << "Product: " << calc.multiply(4, 5) << std::endl;
        return 0;
    }
        

Output:

    Product: 20
        

Using Member Functions

To use member functions, you need to create an object of the class and call the function using the object. If the function is public, it can be accessed directly.

Example:

    #include <iostream>

    class Car {
    public:
        void start() {
            std::cout << "Car started." << std::endl;
        }
    };

    int main() {
        Car car1;
        car1.start(); // Calling member function
        return 0;
    }
        

Output:

    Car started.
        

Member Functions with Parameters

Member functions can take parameters to perform operations based on input values.

Example:

    #include <iostream>

    class Rectangle {
    public:
        int area(int length, int width) {
            return length * width; // Function with parameters
        }
    };

    int main() {
        Rectangle rect;
        std::cout << "Area: " << rect.area(10, 5) << std::endl;
        return 0;
    }
        

Output:

    Area: 50
        

Member Functions with Return Values

Member functions can return values based on the operations they perform.

Example:

    #include <iostream>

    class Circle {
    public:
        double area(double radius) {
            return 3.14 * radius * radius; // Function with return value
        }
    };

    int main() {
        Circle circ;
        std::cout << "Area: " << circ.area(7) << std::endl;
        return 0;
    }
        

Output:

    Area: 153.86
        

Member functions provide the functionality of a class by enabling it to manipulate and access its data members. They are an essential feature of C++ that brings the principles of encapsulation and modular programming into practice.





Advertisement