Yes, it is possible by declaring the main function as the member function in a class.
See the below example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> using namespace std; class MyTestClass { public: int main(int iVal) { cout << iVal << endl; return 0; } int main(char *pcCh) { cout << pcCh << endl; return 0; } int main(int iVal ,int iVal2) { cout << iVal << " " << iVal2 << endl; return 0; } }; int main() { MyTestClass obj; obj.main (12); obj.main ("Testing main function"); obj.main (30, 300); getchar (); return 0; } |
See the results:
1 2 3 4 5 |
12 Testing main function 30 300 |
When we use the main function like below, let us predict the results
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> using namespace std; int main(int iVal) { cout << iVal << endl; return 0; } int main(char *pcCh) { cout << pcCh << endl; return 0; } int main(int iVal ,int iVal2) { cout << iVal << " " << iVal2 << endl; return 0; } int main() { main (12); main ("Testing main function"); main (30, 300); getchar (); return 0; } |
The above program will give us the compilation errors like
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1>TestCPP.cpp(35): error C2731: 'main' : function cannot be overloaded 1> TestCPP.cpp(34) : see declaration of 'main' 1>TestCPP.cpp(40): error C2731: 'main' : function cannot be overloaded 1> TestCPP.cpp(39) : see declaration of 'main' 1>TestCPP.cpp(46): error C2731: 'main' : function cannot be overloaded 1> TestCPP.cpp(45) : see declaration of 'main' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== |
You may get different errors on different compilers.