Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How can we write main as a class in C++?

  • Difficulty Level : Medium
  • Last Updated : 07 Feb, 2020

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:

Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII

Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!

 






// 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:




// 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:




// 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()



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!