#include <windows.h>
#include <tchar.h>

#include <typeinfo.h> // Compile Option: /GR
#include <iostream>

using namespace std;

class GameObject
{
public:
 virtual void Collide(GameObject& otherObject) = 0;
 virtual ~GameObject();
};

class SpaceShip: public GameObject
{
public:
 virtual void Collide(GameObject& otherObject);
};

class SpaceStation: public GameObject
{
public:
 virtual void Collide(GameObject& otherObject);
};


class Asteroid: public GameObject
{
public:
 virtual void Collide(GameObject& otherObject);
};

GameObject::~GameObject()
{
}

void SpaceShip::Collide(GameObject& otherObject)
{
 const type_info& objectType = typeid(otherObject);

 if(objectType == typeid(SpaceShip) )
 {
  cout << "The space ship is colliding with a space ship";
  cout << endl;
 }
 else if (objectType == typeid(SpaceStation) )
 {
  cout << "The space ship is colliding with a space station";
  cout << endl;  
 }
 else if (objectType == typeid(Asteroid) )
 {
  cout << "The space ship is colliding with a asteroid";
  cout << endl;  
 }
 else
 {
  cout << "The space ship is colliding with a unknown object";
  cout << endl;
  cout << " Shall we throw a unknown object type exception";
  cout << endl;
 }
}

void SpaceStation::Collide(GameObject& otherObject)
{
 const type_info& objectType = typeid(otherObject);

 if(objectType == typeid(SpaceShip) )
 {
  cout << "The space station is colliding with a space ship";
  cout << endl;
 }
 else if (objectType == typeid(SpaceStation))
 {
  cout << "The space station is colliding with a space station";
  cout << endl;  
 }
 else if (objectType == typeid(Asteroid))
 {
  cout << "The space station is colliding with a asteroid";
  cout << endl;  
 }
 else
 {
  cout << "The space station is colliding with a unknown object";
  cout << endl;
  cout << " Shall we throw a unknown object type exception";
  cout << endl;
 }
}

void Asteroid::Collide(GameObject& otherObject)
{
 const type_info& objectType = typeid(otherObject);

 if(objectType == typeid(SpaceShip) )
 {
  cout << "The asteroid is colliding with a space ship";
  cout << endl;
 }
 else if (objectType == typeid(SpaceStation))
 {
  cout << "The asteroid is colliding with a space station";
  cout << endl;  
 }
 else if (objectType == typeid(Asteroid))
 {
  cout << "The asteroid is colliding with a asteroid";
  cout << endl;  
 }
 else
 {
  cout << "The asteroid is colliding with a unknown object";
  cout << endl;
  cout << " Shall we throw a unknown object type exception";
  cout << endl;
 }
}

int _tmain(int argc, _TCHAR* argv[])
{
 SpaceShip       ship;
 SpaceStation    station;
 Asteroid asteroid;

 GameObject *p=&ship;

 ship.Collide(station);
 ship.Collide(asteroid);
 ship.Collide(ship);
 ship.Collide(*p);

 return 0;
}
-----------------------------------------------------------------------
The space ship is colliding with a space station
The space ship is colliding with a asteroid
The space ship is colliding with a space ship
The space ship is colliding with a space ship

Posted by 셈말짓기 :

#include <windows.h>
#include <tchar.h>

#include <typeinfo>
#include <iostream>

using namespace std;

class SpaceShip;
class SpaceStation;
class Asteriod;

class GameObject
{
public:
 virtual void Collide(GameObject& otherObject) = 0;

public:
 virtual void Collide(SpaceShip& otherObject) = 0;
 virtual void Collide(SpaceStation& otherObject) = 0;
 virtual void Collide(Asteriod& otherObject) = 0;

 virtual ~GameObject();
};

class SpaceShip: public GameObject
{
public:
 virtual void Collide(GameObject& otherObject);
public:
 virtual void Collide(SpaceShip& otherObject);
 virtual void Collide(SpaceStation& otherObject);
 virtual void Collide(Asteriod& otherObject);
};

class SpaceStation: public GameObject
{
public:
 virtual void Collide(GameObject& otherObject);
public:
 virtual void Collide(SpaceShip& otherObject);
 virtual void Collide(SpaceStation& otherObject);
 virtual void Collide(Asteriod& otherObject);
};


class Asteriod: public GameObject
{
public:
 virtual void Collide(GameObject& otherObject);
public:
 virtual void Collide(SpaceShip& otherObject);
 virtual void Collide(SpaceStation& otherObject);
 virtual void Collide(Asteriod& otherObject);
};

GameObject::~GameObject()
{
}

void SpaceShip::Collide(GameObject& otherObject)
{
 otherObject.Collide(*this);
}

void SpaceShip::Collide(SpaceShip& otherObject)
{
 cout << "The space ship is colliding with a space ship";
 cout << endl;
}

void SpaceShip::Collide(SpaceStation& otherObject)
{
 cout << "The space ship is colliding with a space station";
 cout << endl;
}

void SpaceShip::Collide(Asteriod& otherObject)
{
 cout << "The space ship is colliding with a asteriod";
 cout << endl;
}

void SpaceStation::Collide(GameObject& otherObject)
{
 otherObject.Collide(*this);
}

void SpaceStation::Collide(SpaceShip& otherObject)
{
 cout << "The space station is colliding with a space ship";
 cout << endl;
}

void SpaceStation::Collide(SpaceStation& otherObject)
{
 cout << "The space station is colliding with a space station";
 cout << endl;
}

void SpaceStation::Collide(Asteriod& otherObject)
{
 cout << "The space station is colliding with a asteriod";
 cout << endl;
}

void Asteriod::Collide(GameObject& otherObject)
{
 otherObject.Collide(*this);
}

void Asteriod::Collide(SpaceShip& otherObject)
{
 cout << "The space ship is colliding with a space ship";
 cout << endl;
}

void Asteriod::Collide(SpaceStation& otherObject)
{
 cout << "The space ship is colliding with a space station";
 cout << endl;
}

void Asteriod::Collide(Asteriod& otherObject)
{
 cout << "The space ship is colliding with a asteriod";
 cout << endl;
}

int _tmain (int argc, _TCHAR* argv[])
{
 GameObject  *p1;
 GameObject  *p2;

 SpaceShip    ship;
 SpaceStation station;
 Asteriod     asteriod;

 p1 = &ship;
 p2 = &ship;     p1->Collide(*p2);
 p2 = &station;  p1->Collide(*p2);
 p2 = &asteriod; p1->Collide(*p2);

 return 0;
}
------------------------------------------------------
The space ship is colliding with a space ship
The space station is colliding with a space ship
The space ship is colliding with a space ship

Posted by 셈말짓기 :

//////////////////////////////////////////////////////////////////////////////////////
//
// 템플릿 클래스 메소드 특화
//
//
//////////////////////////////////////////////////////////////////////////////////////
#include <string>

#include <windows.h>

template <class CharType>
class Message
{
public:
 void Print (void);
 void Set   (std::basic_string < CharType > s);

public:
 std::basic_string < CharType > text_;
};

template <class CharType>
void Message<CharType>::Set (std::basic_string < CharType > s)
{
 text_ = s;
}

template <class CharType>
void Message<CharType>::Print (void)
{
 OutputDebugStringW (text_.c_str());
}

template <>
void Message<char>::Print (void)
{
 printf (text_.c_str());
}

int _tmain(int argc, _TCHAR* argv[])
{
 Message<wchar_t> b;

 b.Set (L"bbb\r\n");
 b.Print();


 Message<char> a;

 a.Set ("aaa\r\n");
 a.Print();


 return 0;
}
--------------------------------------------------------------------------------------
aaa (콘솔창)
--------------------------------------------------------------------------------------
bbb (디버깅창)





//////////////////////////////////////////////////////////////////////////////////////
//
// 템플릿 클래스 특화 (Specialization of template class)
//
//
//////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>

template<class T>
class Min
{
public:
 const T& Is (const T &a, const T &b)
 {
  std::cout << "template<class T> class Min::Is ("
      << static_cast<int>(a) << ", "
      << static_cast<int>(b) << ")"
      << std::endl;

  return (a<b) ? a: b;
 }
};

template<>
class Min <char>
{
public:
 const char& Is (const char &a, const char &b)
 {
  std::cout << "template<> class Min<char>::Is ( \'"
      << a << "\', "
      << b << "\' )"
      << std::endl;

  return (a<b) ? a: b;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 Min<int>  a;
 //Min<>     b; <- error
 Min<char> c;
 //Min<>     d; <- error

 a.Is (1,2);
 //b.Is (3,4);
 c.Is ('a','b');
 //d.Is ('c','d');

 return 0;
}
--------------------------------------------------------------------------------------
template<class T> class Min::Is (1, 2)
template<> class Min<char>::Is ( 'a', b' )





//////////////////////////////////////////////////////////////////////////////////////
//
// 함수 템플릿 특화 (Specialization of function template)
//
//
//////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>

template<class T>
const T& Min (const T &a, const T &b)
{
 std::cout << "template<class T> const T& Min ("
           << static_cast<int>(a) << ", "
     << static_cast<int>(b) << ")"
     << std::endl;

 return (a<b) ? a: b;
}

template<>
const char& Min <char> (const char &a, const char &b)
{
 std::cout << "template<> const char& Min <char> ( \'"
           << a << "\', "
     << b << "\' )"
     << std::endl;

 return (a<b) ? a: b;
}

int _tmain(int argc, _TCHAR* argv[])
{
 Min<int>  ( 1 ,  2 );
 Min<>     ( 3 ,  4 );
 Min<char> ('c', 'd');
 Min<>     ('a', 'b');

 return 0;
}
--------------------------------------------------------------------------------------
template<class T> const T& Min (1, 2)
template<class T> const T& Min (3, 4)
template<> const char& Min <char> ( 'c', d' )
template<> const char& Min <char> ( 'a', b' )

//
// cf>
//   함수 템플릿은 아래와 같이 정의 할 수 있지만
//   template <class T, int X>
//   void foo_func (T a)
//   {
//   }
//   아래와 같이 기본 템플릿 인수는 정의 할 수 없다.
//   template <class T, int X=0>
//   void foo_func (T a)
//   {
//   }
//   기본 템플릿 인수는 클래스 탬플릿에서만 사용이 가능하다.
//
//   또한, 부분특화 템플릿 함수는 사용 될 수 없다.
//





//////////////////////////////////////////////////////////////////////////////////////
//
// 템플릿 클래스 부분특화 (Partial specialization of template class)
//
//
//////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>

// partial specialization

class partial_specialization
{
public:
 partial_specialization ()
 {
  std::cout << "partial_specialization ()" << std::endl;
 }
 ~partial_specialization ()
 {
  std::cout << "~partial_specialization ()" << std::endl;
 }
};

template <class T1, class T2> class foo
{
public:
 foo ()
 {
  std::cout << "foo ()" << std::endl;
 }

 ~foo ()
 {
  std::cout << "~foo ()" << std::endl;
 }
};

template <class T1> class foo <T1, partial_specialization>
{
public:
 foo ()
 {
  std::cout << "foo() 2" << std::endl;
 }

 ~foo ()
 {
  std::cout << "~foo() 2" << std::endl;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 foo<int, int>                    a;
 foo<int, partial_specialization> b;

 return 0;
}
--------------------------------------------------------------------------------------
foo ()
foo() 2
~foo() 2
~foo ()

Posted by 셈말짓기 :
template<
    typename output_policy,
    typename language_policy
>
class HelloWorld
  : public output_policy,
    public language_policy
{
    using output_policy::Print;
    using language_policy::Message;
 
public:
 
    //behaviour method
    void Run()
    {
        //two policy methods
        Print( Message() );
    }
 
};
 
 
#include <iostream>
 
class HelloWorld_OutputPolicy_WriteToCout
{
protected:
 
    template< typename message_type >
    void Print( message_type message )
    {
        std::cout << message << std::endl;
    }
 
};
 
 
#include <string>
 
class HelloWorld_LanguagePolicy_English
{
protected:
 
    std::string Message()
    {
        return "Hello, World!";
    }
 
};
 
class HelloWorld_LanguagePolicy_German{
protected:
 
    std::string Message()
    {
        return "Hallo Welt!";
    }
 
};
 
 
int main()
{
 
/* example 1 */
 
    typedef
        HelloWorld<
            HelloWorld_OutputPolicy_WriteToCout,
            HelloWorld_LanguagePolicy_English
        >
            my_hello_world_type;
 
    my_hello_world_type hello_world;
    hello_world.Run(); //returns Hello World!
 
 
/* example 2 
 * does the same but uses another policy, the language has changed
 */
 
    typedef
        HelloWorld<
            HelloWorld_OutputPolicy_WriteToCout,
            HelloWorld_LanguagePolicy_German
        >
            my_other_hello_world_type;
 
    my_other_hello_world_type hello_world2;
    hello_world2.Run(); //returns Hallo Welt!
}
Posted by 셈말짓기 :

고객이 필요한 것

2008. 7. 22. 15:27 from 낙서판

사용자 삽입 이미지
Posted by 셈말짓기 :