#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 셈말짓기 :