what is #ifndef?

Oct 13, 2011 at 1:49am
What does the #ifndef mean and the # define ? not sure really what that is

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
29
30
31
32
33
#ifndef DATA_H
#define DATA_H
#include <iostream>
using namespace std;
class data
{
public:
	data();
	data(char* name, char* pccId, float gpa);
	data(const data& student);		//copy constructor: make current object a copy of "student"
	~data();						//destructor: release the dynamically allocated memory

	void getName(char* name) const;
	void getPccId(char* pccId) const;
	float getGpa(void) const;

	void setName(char* name);
	void setPccId(char* pccId);
	void setGpa(float gpa);

	// operator overloads
	bool operator<(const data& d);
	bool operator==(const data& d);
	const data& operator=(const data& student);
	friend ostream& operator<<(ostream& out, const data& student);

private:
	char*	name;
	char*	pccId;
	float	gpa;
};

#endif 
Oct 13, 2011 at 2:02am
Those are called preprocessor commands. In this case, they are being used to insure that the given header file doesn't get included more than once, to prevent multiple definition errors. To see what I mean, comment out all the lines starting with "#" except the include.

You may find this helpful: http://cplusplus.com/doc/tutorial/preprocessor/
Oct 13, 2011 at 2:10am
ahh i see ok thank you
Oct 13, 2011 at 4:59am
Much easier to use #pragma once though. Don't make a habit of using #ifndef because not only is it uglier code but it's not quite as fast compiling either.

1
2
3
#pragma once

// Stuff Here... 
Last edited on Oct 13, 2011 at 5:00am
Oct 13, 2011 at 6:10am
#pragma once is compiler specific
Topic archived. No new replies allowed.