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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s