previous | index

How Multiple Inclusions Are Avoided

  • The first time item.H (shown at right) is included (by duds.H), the directive:
          #ifndef _ITEM_H	
    will not have been encountered, so the name _ITEM_H will not have been defined. Therefore, the next directive:
          #define _ITEM_H	
    will be processed, and the ItemInfo class will be defined.
  • The next time item.H is included (by for example shirt.H), the name _ITEM_H will have been defined, and so everything down to the directive:
          #endif	
    will be ignored by the compiler, thus avoiding defining the ItemInfo class multiple times.
     
#ifndef _ITEM_H
#define _ITEM_H

#include <iostream>
#include <string>
using namespace std;

class ItemInfo {
public:
  ItemInfo(double p, string c);
  double getPrice();
  string getColor();
  virtual void display();
private:
  double price;
  string color;
};

#endif/* _ITEM_H */

previous | index