Tuesday, July 16, 2013

Template-inheritance

//bell.h
#ifndef __BELL__
#define __BELL__
#include "iBell.h"
#include "SoundMaker.h"
template<class T>
class Bell : public SoundMaker<T>, public iBell
   {
   private:
   protected:
   public:
      Bell() : SoundMaker() {sound = "Ding"}
      void switchHammer()
         {
         if(sound == "Ding")
            sound = "Dong";
         else
            sound = "Ding";
         }
   };

// base.h
#ifndef __BASE__
#define __BASE__
#include "iBase.h"
template<class T>
class Base : public iBase
   {
   private:
      static T count;

      T id;
   protected:
   public:
      Base() : id(count++) {}
      T getID() {return id;}
   };
#endif


// soundmaker.h
#include "Base.h"
#include "iSoundMaker.h"
template<class T>
class SoundMaker : public Base<T>, public iSoundMaker
   {
   private:
   protected:
      std::string sound;
   public:

      Soundmaker() : Base(), sound("Bang") {}
      void playSound() {std::cout << sound << "\n";}
   };
#endif

//speaker.h
#include "iSpeaker.h"
#include "SoundMaker.h"
template<class T>
class Speaker : public SoundMaker<T>, public iSpeaker
   {
   private:
   protected:
   public:
      Speaker() : SoundMaker() {}
      void increaseVolume()
         {
         sound += "!";
         }
   };
#endif