#ifndef, #define

Hello guys,

In my textbook (Data Structures and Algorithms - Goodrich), I see the use of the terms " #ifndef " and " #define ". What do these mean?

I am familiar with #include - that's for including header files.
Very helpful link.

Thanks
NewProgrammer
I now understand the meaning of the terms, but look at the first two lines of this code fragment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef CREDIT_CARD_H					// avoid repeated expansion
#define CREDIT_CARD_H

#include <string>						// provides string
#include <iostream>						// provides ostream

class CreditCard 
{
public:
	CreditCard(const std::string& no,	// constructor
				const std::string& nm, int lim, double bal = 0);
										// accessor functions
	std::string	getNumber()const	{ return number; }
	std::string getName() const		{ return name; }
	double		getBalance() const	{ return balance; }
	int			getLimit() const	{ return limit; }

	bool chargeIt(double price);		// make a charge
	void makePayment(double payment);	// make a payment
private:								// private member data
	std::string	number;					// credit card number
	std::string name;					// card owner's name
	int			limit;					// credit limit
	double		balance;				// credit card balance
};

std::ostream& operator<<(std::ostream& out, const CreditCard& c);
#endif 

CREDIT_CARD_H is obviously the identifier, but where is the replacement?
Last edited on
You can use #define without a "replacement", which is sort of like making a preprocessor Boolean variable. Line 1 simply checks if it has been #define'd at all.
Right. I see now. In this case, no replacement is necessary. Line 2 just needs to be encountered once, then the code is never expanded again.

Thanks.
Last edited on
Topic archived. No new replies allowed.