Question: I'm watching some video tutorials on C++ and i know you must define a function / class before it is used or called. But I like having my main() function at the top, and everything else below the main function. I know if i define a function below the main function I must declare it before it is used, but what about a class? What do I need to put above my main function to use my class below the main function.

```
#include <iostream>
using namespace std;

int main()
{
    ClassOne one;
    one.coolSaying();
    return 0;
}

class ClassOne
{
    public:
        void coolSaying()
        {
            cout << "Cool stuff yo!" << endl;
        }
};

```

I tried defining my class by placing this right before main():

```
class ClassOne;

```

but it doesn't work.

Answer: 
This is why header files are normally used in C++. When you're saying `ClassOne one`, the compiler needs to know what the class looks like to create an object of that type. It's not enough to know that the class exists somewhere (that is enough if all you want is a pointer). So the compiler needs to already have read the definition of the class.

Your class has to be defined before it is first used. Without putting it explicitly before `main`, the usual way is to create a header file. So you create `ClassOne.h` with the class declaration, and you have `#include "ClassOne.h` at the top of your file. In this situation the actual methods of the class would normally be in another source file, `ClassOne.cpp`.
(note: all other answers are correct, but you may find this useful)

I discovered this idiom to invert the order of `main` and secondary function classes. I use to share small code with colleagues, everybody expects the core of the code (i.e. `main`) to be on top so they can edit it quickly. It works with classes and functions (without need of declaration) of course. Usually I can leave the preamble (first `#include`s) because those have include guards in most cases.

```
#include <iostream>
using namespace std;

#ifdef please_see_definitions_below_main
int main()
{
    ClassOne one;
    one.coolSaying();
    return 0;
}
#else
class ClassOne
{
    public:
        void coolSaying()
        {
            cout << "Cool stuff yo!" << endl;
        }
};
#define please_see_definitions_below_main
#include __FILE__
#endif

```

I use the tag `please_see_definitions_below_main` so it serves as comment also, but if you don't like it you can use something shorter, like `AFTER`.