Can main() be overloaded in C++?

Predict the output of following C++ program.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main(int a)
{
    cout << a << "\n";
    return 0;
}
int main(char *a)
{
    cout << a << endl;
    return 0;
}
int main(int a, int b)
{
    cout << a << " " << b;
    return 0;
}
int main()
{
    main(3);
    main("C++");
    main(9, 6);
    return 0;
}

chevron_right


The above program fails in compilation and produces warnings and errors (See this for produced warnings and errors). You may get different errors on different compilers.

To overload main() function in C++, it is necessary to use class and declare the main as member function. Note that main is not reserved word in programming languages like C, C++, Java and C#. For example, we can declare a variable whose name is main, try below example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
int main()
{
    int main = 10;
    std::cout << main;
    return 0;
}

chevron_right


Ouput:

10

The following program shows overloading of main() function in a class.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
class Test
{
public:
    int main(int s)
    {
        cout << s << "\n";
        return 0;
    }
    int main(char *s)
    {
        cout << s << endl;
        return 0;
    }
    int main(int s ,int m)
    {
        cout << s << " " << m;
        return 0;
    }
};
int main()
{
    Test obj;
    obj.main(3);
    obj.main("I love C++");
    obj.main(9, 6);
    return 0;
}

chevron_right


The outcome of program is:

3
I love C++
9 6

This article is contributed by Pravasi Meet. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


7


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.