Dremendo Tag Line

Static Methods in C++ Programming

OOPs in C++

In this lesson, we will understand what is Static Method in C++ Programming and how to create them along with some examples.

What are Static Methods in C++?

Static Methods in C++ are defined inside a class using the keyword static. These methods are bound to the class and not to the object, which means that these methods can be called using the class name, and calling the methods using the object name is not necessary. However, the object of the class can also access the static methods using the object name.

Static Methods are used to create a utility class, also known as the helper class, that contains only static methods and can be reused across the programs. It's not required to create an object of a utility class to access its static method because we can also access the static methods using its class name.



#include <iostream>
#include <stdio.h>

using namespace std;

class calc
    static float add(float a, float b)
        return a+b;

    static float subtract(float a, float b)
        return a-b;

    static float multiply(float a, float b)
        return a*b;

    static float divide(float a, float b)
        return a/b;

int main()
    float x,y;

    cout<<"Enter 2 numbers"<<endl;

    // Call all the static methods of the class calc one by one
    cout<<"Addition = "<<calc::add(x,y)<<endl;
    cout<<"Subtraction = "<<calc::subtract(x,y)<<endl;
    cout<<"Multiplication = "<<calc::multiply(x,y)<<endl;
    cout<<"Division = "<<calc::divide(x,y)<<endl;
    return 0;


Enter 2 numbers
Addition = 10
Subtraction = 6
Multiplication = 16
Division = 4