Question: Can we define struct after the main program in C++? When we define functions, we can declare the function before the main program and then write the function definition after the main function. I wanted to know if we can do something similar to this when defining structures. Thank you.

Answer: 
> 
> Can we define struct after the main program in C++?
> 
> 
> 

I presume that you mean the `main` function. Yes, we can define classes (including structs) after `main` function. A demo:

```
int main(){}
struct S{};

```

> 
> When we define functions, we can declare the function before the main program and then write the function definition after the main program. I wanted to know if we can do something similar to this when defining structures.
> 
> 
> 

Same applies to classes, you can (forward) declare them before a function, and define after. However, the uses of an incomplete (declared but not defined) classes are quite limited. You can define pointers and references to them, but you cannot create them, or call any member functions. A demo:

```
struct S;     // (forward) declaration of a class
S* factory(); // (forward) declaration of a function
int main(){
    S* s = factory(); // OK, no definition required
    // s->foo();      // not OK, S is incomplete
    // S s2;          // not OK
}
struct S{             // definition of a class
    void foo();       // declaration of a member function
};
S* factory() {
     static S s;      // OK, S is complete
     s.foo();         // OK, note how the member function can be called
                      // before it is defined, just like free functions
     return &s;
}
void S::foo() {}      // definition of a member function

```
**Edit:** As @user2079303 mentioned in the comments, I used the term 'forward declaration' incorrectly. I updated my answer accordingly.

You can forward-declare structs if you only want to store a pointer to that struct. However, you can only call methods on that pointer once the struct has been defined.

```
#include <iostream>

// forward declaration of struct
struct S;
// pointer can be defined after forward declaration
S * s;

void workOnSPointer();

int main()
{
    workOnSPointer();
}

// definition of struct
struct S
{
    S() : bar(42) {}
    void foo() { std::cout << "bar is " << bar << "\n"; }
    int bar;
};

// methods can only be called after definition
void workOnSPointer()
{
    S * s = new S();
    s->foo();
    delete s;
}

```

You can also forward-declare methods of structs in the struct definition, while defining these methods later.

```
#include <iostream>

// incomplete definition of struct S
// methods of the struct are only declared here
struct S
{
    S();
    void foo();
    int bar;
};

int main()
{
    S s;
    s.foo();
}

// definition of the struct's methods
S::S() : bar(42) {}
void S::foo() { std::cout << "bar is " << bar << "\n"; }

```

This definition with forward-declared methods is the main use of header (`.h`) files. You can define your struct in the header and include the header in the files where the struct is used. The definition of your methods goes into the source (`.cpp`) file. Applied to the code above this means:

File `S.h`

```
struct S
{
    S();
    void foo();
    int bar;
};

```

File `S.cpp`

```
#include "S.h"
#include <iostream>

S::S() : bar(42) {}
void S::foo() { std::cout << "bar is " << bar << "\n"; }

```

File `main.cpp`

```
#include "S.h"

int main()
{
    S s;
    s.foo();
}

```

If you compile `S.cpp` and `main.cpp` and then link the resulting object files, you will get the same behavior as with the code in the beginning.