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.
-
It cannot be reused anywhere in the program and cannot be called recursively.
-
It cannot be predefined and contains the main program for execution.
-
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.
-
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 Destructors, Function 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 Algorithms, Machine Learning, Deep Learning, Cloud 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!