Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split code into multiple files [closed]

Tags:

c++

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
}
like image 494
Vertisan Avatar asked Oct 20 '25 15:10

Vertisan


1 Answers

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"
like image 59
Frank Puffer Avatar answered Oct 22 '25 03:10

Frank Puffer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!