Can function main() be overloaded in C++?
The main() function can be overloaded in C++ by defining main as member function of a class.Since main is not a reserved word in many programming languages like C++,C# ,java etc, main can be declared as a variable or member function.But for overloading main() function it is necessary to define and declare main function inside a class.
Program to show Overloading of main() function.
#include <iostream>
using namespace std;
class Demo
{
public:
int main(char *a)
{
cout << a << "\n";
return 0;
}
int main(int a)
{
cout << a << "\n";
return 0;
}
int main(int a ,int b)
{
cout << a << " " << b;
return 0;
}
};
int main()
{
Demo ob;
ob.main("Overloading of main");
ob.main(3);
ob.main(7, 8);
return 0;
}
Output
Overloading of main
3
7 8
0 Comment(s)