4

I want to split my code into multiple files. At this moment I have somethink like this but every time I need to include libraries and headers into each of these file.
Is it better way to do this? main.cpp

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include "modules/intro.cpp"
#include "modules/login.cpp"

using namespace std;

int main() {
    introModule();
    login();

    system("pause");
}

intro.cpp

#include <iostream>

using namespace std;

    void introModule() {
        // content of intro file
    }

login.cpp

#include <iostream>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include "menu.cpp"

using namespace std;


#define ENTER           13
#define BACKSPACE        8


char passInputCharacter;
char password[20];
const char *accessPassword = "123";

int passInputCharacterPosition = 0;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

void login() {
    // content of login file
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Vertisan
  • 720
  • 3
  • 14
  • 34

1 Answers1

7

You should not include cpp files, only header files. Header files basically declare the interfaces of the corresponding cpp files. Therefore, for each cpp file, create an additional header file that contains function declarations only:

intro.h:

void introModule();

login.h

void login();

Then include the required header files in the cpp files:

In main.cpp:

#include "modules/intro.h"
#include "modules/login.h"

In intro.cpp:

#include "intro.h"

In login.cpp:

#include "login.h"
Andrii Magalich
  • 132
  • 2
  • 8
Frank Puffer
  • 8,135
  • 2
  • 20
  • 45