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);


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;




The sequence of performance is illustrated as following:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s