Predict the output of following C++ program.
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:
The following program shows overloading of main() function in a class.
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- What are the operators that can be and cannot be overloaded in C++?
- Is it fine to write "void main()" or "main()" in C/C++?
- Difference between "int main()" and "int main(void)" in C/C++?
- C/C++ program for calling main() in main()
- Functions that cannot be overloaded in C++
- Hiding of all overloaded methods with same name in base class
- How can we write main as a class in C++?
- How to print "GeeksforGeeks" with empty main() in C, C++ and Java?
- How to change the output of printf() in main() ?
- Executing main() in C/C++ - behind the scene
- Print "Hello World" with empty or blank main in C++
- What does main() return in C and C++?
- How to call some function before main() function in C++?
- What are Wild Pointers? How can we avoid?
- Can we call an undeclared function in C++?
- Can we use function on left side of an expression in C and C++?
- Can we access global variable if there is a local variable with same name?
- Can references refer to invalid location in C++?
- Can static functions be virtual in C++?
- Can a C++ class have an object of self type?
Improved By : nidhi_biet