Although I have included header file multiple times without using include guards, the code compiles without no error

I have a question about include guards. I assume that if you include your header file in multiple source files you should get compilation error, so to prevent the error you need to use
#ifndef
#define SOME_THING_H
...
#endif
I have wrote this code and I included header file in application file and implementation file without using include guards, but it compiles perfectly without any error. I am using Dev C++ 4.9.9.2. Does anybody know if it is always necessary to use include guards? and if not way this code compiles?

// application file
//main.cpp
#include <iostream>
#include "DayOfYear.h"
using namespace std;


int main() {
DayOfYear today, birthday;

cout << "Enter today's date \n";
today.getMonth();
today.getDay();
cout << "Enter your birthday \n";
birthday.getMonth();
birthday.getDay();
cout << "Today is :";
today.output();
cout << "Your Birthday is :";
birthday.output();
if (today.month == birthday.month && today.day == birthday.day)
cout << "Happy Birthday" << endl;
else
cout << "Happy unbirthday" << endl;

system("pause");
return 0;

}

/******************************/

//header file
//DayOfYear.h
#include <iostream>

using namespace std;
class DayOfYear {
public:
void output();
int getMonth();
int getDay();
int month;
int day;
};

/***********************************/

//implementation file
//DayOfYear.cpp
#include <iostream>
#include "DayOfYear.h"
using namespace std;

void DayOfYear::output(){
cout << "month = " << month
<< ", day = " << day << endl;
}
int DayOfYear::getMonth(){
cout << "Enter month as a number : ";
cin >> month;
return month;

}
int DayOfYear::getDay(){
cout << "Enter day of the month : ";
cin >> day;
return day;

}

Last edited on
Include guards protect against the same header being included multiple times in the same source file.

See this article for details, explanations, and guidelines:

http://cplusplus.com/forum/articles/10627/
Thank you very much I got it.
One more question
If I keep all the member functions like DayOfYear::output() in the header file(DayOfYear.h), do I get linker error if I try to include that file in multiple source files?
If it's in the class body: no (implicit inline)
If it's marked with the inline keword: no (explicit inline)
If it's a template class or a template function: no

otherwise: yes
Last edited on
Problem solved. Thanks a lot
No problem! :P
Topic archived. No new replies allowed.