Function Overloading and Const Keyword in C++



In C++, function overloading and const keyword are used for different purposes. Function overloading provides different ways to call a function with different parameter types that make the program more readable. While the const keyword provides the ways of declaration such as variable, member variable, function parameters, member function, and return type.

What is Function Overloading?

Function overloading is the process of defining multiple functions having the same name but different parameter lists. It is also known as compile-time polymorphism.

Here, we have list of three points to describe function overloading in C++:

  • The parameter is defined using different types.
  • The parameter has a different number.
  • The sequence of parameters can be changed.

Syntax

Following is the basic syntax of different parameters based on same function name:

product(int a, int b)
product(double a, double b, double c)

Example of Function Overloading

In this example, we execute the C++ program based on all above points.

#include<iostream>
using namespace std;

// Function with different parameter types
void display(int x) {
   cout << "Integer: " << x << endl;
}

void display(double x) {
   cout << "Double: " << x << endl;
}

// Function with different number of parameters
void show(int x, int y) {
   cout << "Two integers: " << x << ", " << y << endl;
}

void show(int x) {
   cout << "Single integer: " << x << endl;
}

// Function with different parameter sequences
void print(char ch, int num) {
   cout << "Char: " << ch << ", Integer: " << num << endl;
}

void print(int num, char ch) {
   cout << "Integer: " << num << ", Char: " << ch << endl;
}

int main() {
   display(10);         // Call display(int)
   display(10.5);       // Call display(double)

   show(5);             // Call show(int)
   show(5, 10);         // Call show(int, int)

   print('A', 100);     // Call print(char, int)
   print(200, 'B');     // Call print(int, char)

    return 0;
}

The above syntax produces the following result:

Integer: 10
Double: 10.5
Single integer: 5
Two integers: 5, 10
Char: A, Integer: 100
Integer: 200, Char: B

What is const Keyword?

The const (Constant) is the keyword that used before the variable name. The value of const is fixed and cannot be change after intialization.

Various Ways to Intialize "const"

The following are list of const initialization in C++ program:

i. const with variable name

const var_name;

ii. const with function parameter

void print(const int x) {  
   // x cannot be modified inside the function
}

iii. const with member function

class MyClass {
public:
   void show() const {  
       // This function cannot modify member variables
   }
};

iv. const with return type

const int getVal() {
    return int_val/float_val/.../etc.;
}

Example of "const" Keyword

Following is an example to show the usage of const keyword.

#include<iostream>
using namespace std;

class MyClass {
private:
   // i. const with variable name
   const int id;  
public:
    MyClass(int val) : id(val) {}  
    // Initializing const variable in constructor
    // ii. const with function parameter
   void printValue(const int x) {  
       cout << "Function parameter (const int x): " << x << endl;
    }

   // iii. const with member function
   void show() const {  
    cout << "This is a const member function. id = " << id << endl;
    }
   
    // iv. const with return type
   const int getVal() const { 
        return id * 2;
    }
};

int main() {
   // i. const with variable name
   const int var_name = 100;  
   cout << "Constant variable: " << var_name << endl;

   MyClass obj(50);
   // Function call with const parameter    
   obj.printValue(20);  
   // Calling const member function
   obj.show();          
   cout << "Const return value from getVal(): " << obj.getVal() << endl;

   return 0;
}

The above program produces the following result:

Constant variable: 100
Function parameter (const int x): 20
This is a const member function. id = 50
Const return value from getVal(): 100
Updated on: 2025-06-13T14:02:47+05:30

564 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements