Table of contents
1.
Introduction
2.
Overloading
3.
The main() function
4.
Overloading main() without a class
5.
Overloading main() inside a class
6.
Frequently Asked Questions
6.1.
What is the difference between overloading and overriding?
6.2.
What are some of the functions that can not be overloaded?
7.
Conclusion
Last Updated: Mar 27, 2024

Overloading in main() function

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Any C++ program we write would be meaningless without a main() function. This is because the main() is where the execution begins. It is a global function that is called at program startup immediately after the non-local objects/ variables are initialised. Such an important function is bound to have properties that differentiate it from other functions in a program. In general, all functions in C++ can be overloaded. We shall look at the properties unique to the main() function and find out if the main() too can be overloaded.

Overloading

Overloading is a property which defines two or more members having the same name but with different functionality. C++ has functions and operator overloading features.  In function overloading, two or more functions having the same name differ in their parameters either by their count or data type. Function overloading increases the readability of a program and also aids with code reusability to save memory.

#include <iostream>    
using namespace std;    
class ADD {    
    public:
    static int add(int a,int b)
    {      
        return a + b;      
    } 
    static int add(int a, int b, int c)      
    {      
        return a + b + c;      
    }      
};     
int main(void) {    
    ADD a;                                                
    cout<<a.add(13,23)<<endl;      
    cout<<a.add(10,11,12);     
  return 0;    
}    
You can also try this code with Online C++ Compiler
Run Code

 

Output:

36
33

The main() function

As mentioned before, the main() function is where the execution begins. This alone makes it a unique function in C++. It has a few interesting properties associated with it. Let's see a few of them.

  1. It cannot be reused anywhere in the program and cannot be called recursively.
     
  2. It cannot be predefined and contains the main program for execution.
     
  3. The main() does not require a return statement to be specified. When the control reaches the end of a main() function, it is equivalent to encountering a ‘return 0’ statement.
     
  4. The main() function cannot be suspended and resumed like other functions. It gets executed in one go.
     

The first two properties come close to telling whether the main() function can be overloaded or not. The simple answer is that the main() function cannot be overloaded except in some cases.

Attempting to overload a main() function would throw a lot of errors and warnings. The main() function can be overloaded by using a class and then declaring main as a member function. Since main is not a keyword in C++, it can be used freely. The following examples can give a clear picture of how this can be done.

Overloading main() without a class

Since main() is a function that is to be used only once, overloading it produced a lot of errors and warnings. The error log mentions the conflicting declarations of the main() function.

#include <iostream>
using namespace std;
int main(int a)
{
    cout << a << "\n";
    return 0;
}

int main(int a, int b)
{
    cout << a << " " << b;
    return 0;
}

int main(int a, char b)
{
    cout << a << " " << b ;
    return 0;
}
int main()
{
    main(100);
    main(10,20);
    main(10,'A');
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

main.cpp:4:5: note: previous declaration 'int main(int)'
    4 | int main(int a)
      |     ^~~~
main.cpp:10:5: note: previous declaration 'int main(int, int)'
  10 | int main(int a, int b)
      |     ^~~~
main.cpp:16:5: error: conflicting declaration of C function 'int main(int, char)'
  16 | int main(int a, char b)
      |     ^~~~
main.cpp:4:5: note: previous declaration 'int main(int)'
    4 | int main(int a)
      |     ^~~~
main.cpp:21:5: error: conflicting declaration of C function 'int main()'
  21 | int main()
      |     ^~~~
main.cpp:16:5: note: previous declaration 'int main(int, char)'
  16 | int main(int a, char b)
      |     ^~~~
main.cpp:21:5: error: conflicting declaration of C function 'int main()'
  21 | int main()
      |     ^~~~
main.cpp:10:5: note: previous declaration 'int main(int, int)'
  10 | int main(int a, int b)
      |     ^~~~
main.cpp:21:5: error: conflicting declaration of C function 'int main()'
  21 | int main()
      |     ^~~~
main.cpp:4:5: note: previous declaration 'int main(int)'
    4 | int main(int a)
      |     ^~~~

 

Overloading main() inside a class

The program below is the modified version of the above program to remove errors. The Test_main() class contains three overloaded main functions. Since main is not a reserved word, it can be used to overload functions within the class only. These functions can be accessed in the main function outside the class via the object test.

#include <iostream>
using namespace std;
class Test_main
{
    public:
      int main(int a)
    {
        cout << a << "\n";
        return 0;
    }

      int main(int a, int b)
    {
        cout << a << " " << b << "\n";
        return 0;
    }

      int main(int a, char b)
    {
        cout << a << " " << b << "\n";
        return 0;
    }
};
int main()
{
    Test_main test;
    test.main(100);
    test.main(10,'A');
    test.main(1,2);
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

100
10 A
1 2


You can also practice with the help of Online C++ Compiler

Frequently Asked Questions

What is the difference between overloading and overriding?

Overloading is a compile-time polymorphism while overriding is a runtime polymorphism. Overloading does not necessarily require inheritance, whereas overriding does. In overloading, methods have the same name but different signatures. In overriding, methods have the same name and signatures.

What are some of the functions that can not be overloaded?

Static member functions can not be overloaded. Functions with the same types of arguments or parameters and functions with return values of different data types can not be overloaded.

Conclusion

In this article, we have extensively discussed how to overload the main() function in C++. We briefly touched upon the concept of overloading and got to know some of the unique properties of the main() function in C++. Lastly, we went over a few examples to understand how the main() function can be overloaded even though it is not possible in most cases.

Feeling curious? Coding Ninjas has you covered. Check out our articles on Constructors and DestructorsFunction overloading in C++ and Classes and Objects in C++Follow our Guided path for Programming in C++ here.

Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and AlgorithmsMachine LearningDeep LearningCloud Computing and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio! 

Looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations. Upvote our blogs if you find them insightful and engaging! Happy Coding!

Live masterclass