As it is already known that main() method is the entry point in any program in C++, hence creating a class named “main” is a challenge and is generally not possible. But this article explains how to write a class named “main” in C++.
What happens when we try to write a class named main?
Writing a class named main is not allowed generally in C++, as the compiler gets confused it with main() method. Hence when we write the main class, creating its object will lead to error as it won’t consider the ‘main’ as a class name.
Compilation Error in CPP code :- prog.cpp: In function 'int main()': prog.cpp:17:10: error: expected ';' before 'obj' main obj; ^ prog.cpp:18:5: error: 'obj' was not declared in this scope obj.print(); ^
How to successfully create a class named main?
When the class name is main, it is compulsory to use keyword class or struct to declare objects.
How to write a constructor or destructor named main?
Writing a constructor or destructor named main is not a problem, as it means the class name must be main. We have already discussed how to make a class named main above.
In constructor main() GFG In destructor main()
- Is it fine to write "void main()" or "main()" in C/C++?
- Difference between "int main()" and "int main(void)" in C/C++?
- Can main() be overloaded in C++?
- C/C++ program to print Hello World without using main() and semicolon
- How to convert a class to another class type in C++?
- Understanding "static" in "public static void main" in Java
- How to call some function before main() function in C++?
- std::uniform_int_distribution class in C++
- Scanner Class in Java
- C++ string class and its applications
- Friend class and function in C++
- String class in Java | Set 1
- What all is inherited from parent class in C++?
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- Computer Science Class XII (2016-17)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.