Multiple files project in C++

It’s not uncommon that many projects contains different files. For example:

Project proj contains file add.h, add.cpp, and main.cpp. Here: add.h contains declarations of functions, add.cpp defines the functions while main.cpp has the main function.

add.h: starts with header guard

#ifndef ADD_H

#define ADD_H

int add(int x, int y);

int max(int x, int y);

#endif

add.cpp: defines the above functions

int add(int x, int y){

return x+y;

}

int max(int x, int y){

return (x>y)?x:y;

}

main.cpp: contains the main functions

#include <iostream>

#include “add.h”

using namespace std;

int main(){

int a=2,b=3;

cout<<add(2,3)<<endl;

cout<<max(2,3)<<endl;

}

The sequence of performance is illustrated as following:

Source

Advertisements

#header-guard, #h_file, #multiple-files-project