Computer Science - Object Oriented Programming Using C++ - Discussion

Discussion Forum : Object Oriented Programming Using C++ - Section 1 (Q.No. 21)
21.
The #ifndef directive tests to see whether ________
a class has been defined
a variable has been given a value
a class has no variable definitions
any objects of the class have been instantiated
Answer: Option
Explanation:

#ifndef checks whether the given token has been #defined earlier in the file or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement. #ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.

Discussion:
3 comments Page 1 of 1.

ChampagneDaddy said:   9 years ago
#ifndef is used as a preprocessor guard against recursively including header files. Which is what this question is referring too (although not implicitly).

Implementation example:

#ifndef HEADER_FILE_NAME_H
#define HEADER_FILE_NAME_H
// All symbols here will only be declared once to the compiler.

#endif // HEADER_FILE_NAME_H

It has the same function as '#pragma once', which is the preferred method of guarding against recursive calls in C++.
(1)

Robert said:   10 years ago
#define A 5.

A is not really a class.

Himanshu said:   1 decade ago
Please illustrate with an example.

Post your comments here:

Your comments will be displayed after verification.