GeertArien / c-accelerated

Exercise files for the end-of-chapter exercises in the book 'C++ Accelerated' (Andrew Koenig and Barbara E. Moo).
27 stars 8 forks source link

Could you explain your header files please? #1

Open Miaozu-Humphrey opened 4 years ago

Miaozu-Humphrey commented 4 years ago

I am a new C++ learner following Accelerated C++ and very grateful for your solutions. I can understand most parts of your code. But I am still confused about header files. What do these #ifndef or #define statements mean? You write header for every CPP file and I can't find out why we need them. Could you explain these header files please and why we need them?

ifndef GUARD_0_3_H

define GUARD_0_3_H

void ex0_3();

endif // !GUARD_0_3_H

alexxxius commented 4 years ago

Hi, this statemets are to include header only one time if other classes include same header

GeertArien commented 4 years ago

ifndef and #define are preprocessor symbols, preprocessing is a step that happens before compilation and consists of mostly textual substitution. The way they are used here is commonly called a header guard. Cause they prevent the header content from being included more than once in another source file.

I admit that the way I used the header files here are a bit overkill. Because the compiler will only complain about duplicate definitions and not duplicate declarations. At the time I made these exercises I was still pretty new to c++ and was following best practices very strictly.

You can find a detailed explanation about header guards here: https://www.learncpp.com/cpp-tutorial/header-guards/

And in case you still have questions about why we need header files: https://www.learncpp.com/cpp-tutorial/header-files/

Miaozu-Humphrey commented 4 years ago

ifndef and #define are preprocessor symbols, preprocessing is a step that happens before compilation and consists of mostly textual substitution. The way they are used here is commonly called a header guard. Cause they prevent the header content from being included more than once in another source file.

I admit that the way I used the header files here are a bit overkill. Because the compiler will only complain about duplicate definitions and not duplicate declarations. At the time I made these exercises I was still pretty new to c++ and was following best practices very strictly.

You can find a detailed explanation about header guards here: https://www.learncpp.com/cpp-tutorial/header-guards/

And in case you still have questions about why we need header files: https://www.learncpp.com/cpp-tutorial/header-files/

I really appreciate that you can answer. I understand header guard now. And your reference website is amazing! Thank you!

sohanglal commented 7 months ago

@AartiKushwaha your Domain Registrar is Go Daddy, LLC

sohanglal commented 7 months ago

Fraud Alert: Fake GitHub Job Opportunity Email

sohanglal commented 7 months ago

https://github.com/orgs/community/discussions/109171 Frauds happening everywhere