How can we write main as a class in C++? Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Example: CPP // C++ program to declare // a class with name main #include <bits/stdc++.h> using namespace std; // Class named main class main { public: void print() { cout << "GFG"; } }; // Driver code int main() { // Creating an object of class main main obj; // Calling the print() method // of class main obj.print(); } Output: 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. Example: CPP // C++ program to declare // a class with name main #include <bits/stdc++.h> using namespace std; // Class named main class main { public: void print() { cout << "GFG"; } }; // Driver code int main() { // Creating an object of class main // Add keyword class ahead of main class main obj; // Calling the print() method // of class main obj.print(); } Output: GFG 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. Example: CPP // C++ program to declare // a class with name main #include <bits/stdc++.h> using namespace std; // Class named main class main { public: // Constructor main() { cout << "In constructor main()\n"; } // Destructor ~main() { cout << "In destructor main()"; } void print() { cout << "GFG\n"; } }; // Driver code int main() { // Creating an object of class main // Add keyword class ahead of main class main obj; // Calling the print() method // of class main obj.print(); } Output: In constructor main() GFG In destructor main() Comment More infoAdvertise with us Next Article How to print "GeeksforGeeks" with empty main() in C, C++ and Java? S shubh_89 Follow Improve Article Tags : C++ cpp-main Practice Tags : CPP Similar Reads What does main() return in C and C++? C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our t 3 min read Can main() be overloaded in C++? Predict the output of following C++ program. C highllight=12-5 #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 << " 2 min read How to call some function before main() function in C++? Since it is known that main() method is the entry point of the program. Hence it is the first method that will get executed by the compiler. But this article explains how to call some function before the main() method gets executed in C++. How to call some function before main() function? To call so 3 min read How to print "GeeksforGeeks" with empty main() in C, C++ and Java? Write a program that prints "GeeksforGeeks" with empty main() function. You are not allowed to write anything in main(). C language One way of doing this is to apply GCC constructor attribute to a function so that it executes before main() (See this for details). C #include <stdio.h> /* Apply 2 min read Difference between Base class and Derived class in C++ Base Class: A base class is a class in Object-Oriented Programming language, from which other classes are derived. The class which inherits the base class has all members of a base class as well as can also have some additional properties. The Base class members and member functions are inherited to 2 min read Storage Classes in C++ with Examples C++ Storage Classes are used to describe the characteristics of a variable/function. It determines the lifetime, visibility, default value, and storage location which helps us to trace the existence of a particular variable during the runtime of a program. Storage class specifiers are used to specif 6 min read Like