Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Can function main() be overloaded in C++?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.61k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: