Tuesday, July 16, 2013

template

//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

bell

#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";
         }
   };


#endif

Friday, May 31, 2013

Concatenate n Chars

// using library <cstrarg>

char* nstrcat(char* name, ...)
{
    int count =0, result_c =0;
    char* result_n = name ;
    char* i;
    while(*result_n)
        ++result_c;

    va_list ap;  
    va_start(ap, name);



    for( i = name; i != '\0'; i = va_arg(ap, char*)){
        for( count = 0; i[count] != '\0'; i++){
            result_n[result_c + count] = i[count]; 
        } 
        result_c += count;  
    }
    va_end(ap);
}

Reference:
http://stackoverflow.com/questions/14023024/strcat-for-formatted-strings