条款31 让函数根据一个以上的对象类型来决定如何虚化

普通函数重载

#include<iostream>
using namespace std;
class SpaceShip;
class SpaceStation;
class Asteriod;
class GameObject
{
      public:

};

class SpaceShip: public GameObject
{
      public:
};

class SpaceStation: public GameObject
{
      public:
};

class Asteriod: public GameObject
{
      public:
};

void collide(GameObject &ob1,GameObject &ob2)
{
     cout<<"GameObject &ob1=>GameObject &ob2"<<endl;
}

void collide(GameObject& ob1,SpaceShip& ob2)
{
     cout<<"GameObject &ob1=>SpaceShip &ob2"<<endl;
}

void collide(SpaceStation& ob1,Asteriod& ob2)
{
     cout<<"SpaceStation &ob1=>Asteriod &ob2"<<endl;
}

void collide(SpaceShip &ob1,SpaceStation &ob2)
{
     cout<<"SpaceShip &ob1=>SpaceStation &ob2"<<endl;
}

void TestCollide(GameObject &ob1,GameObject &ob2)
{
     collide(ob1,ob2);
}
int main()
{
    SpaceShip sp;
    SpaceStation ss;
    TestCollide(sp,ss);//对象切片 
    cout<<"==============================="<<endl;
    collide(sp,ss);//调用重载函数 
    getchar();
    return 0;
}

结果:

条款31所述方法实现多态

#include<iostream>
using namespace std;
class SpaceShip;
class SpaceStation;
class Asteriod;
class GameObject
{
      public:
       virtual void collide(GameObject& otherObject) = 0;
       virtual void collide(SpaceShip& otherObject) = 0;
       virtual void collide(SpaceStation& otherObject) = 0;
       virtual void collide(Asteriod& otherObject) = 0;
};

void GameObject::collide(GameObject& otherObject)
{
     cout<<"Base::collide"<<endl;
}

class SpaceShip: public GameObject
{
      public:
      virtual void collide(GameObject& otherObject)
      {
              cout<<"SpaceShip::collide"<<endl;
              otherObject.collide(*this);
      }
      virtual void collide(SpaceShip& otherObject)
      {
              cout<<"SpaceShip::collide(SpaceShip& otherObject)"<<endl;
      }

      virtual void collide(SpaceStation& otherObject)
      {
              cout<<"SpaceShip::collide(SpaceStation& otherObject)"<<endl;
      }
      virtual void collide(Asteriod& otherObject)
      {
              cout<<"SpaceShip::collide(Asteriod& otherObject)"<<endl;
      }
};

class SpaceStation: public GameObject
{
      public:
      virtual void collide(GameObject& otherObject)
      {
              cout<<"SpaceStation::collide"<<endl;
              otherObject.collide(*this);
      }
      virtual void collide(SpaceShip& otherObject)
      {
              cout<<"SpaceStation::collide(SpaceShip& otherObject)"<<endl;
      }

      virtual void collide(SpaceStation& otherObject)
      {
              cout<<"SpaceStation::collide(SpaceStation& otherObject)"<<endl;
      }
      virtual void collide(Asteriod& otherObject)
      {
              cout<<"SpaceStation::collide(Asteriod& otherObject)"<<endl;
      }
};

class Asteriod: public GameObject
{
      public:
      virtual void collide(GameObject& otherObject)
      {
              cout<<"Asteriod::collide"<<endl;
              otherObject.collide(*this);
      }
      virtual void collide(SpaceShip& otherObject)
      {
              cout<<"Asteriod::collide(SpaceShip& otherObject)"<<endl;
      }

      virtual void collide(SpaceStation& otherObject)
      {
              cout<<"Asteriod::collide(SpaceStation& otherObject)"<<endl;
      }
      virtual void collide(Asteriod& otherObject)
      {
              cout<<"Asteriod::collide(Asteriod& otherObject)"<<endl;
      }
};

void TestCollide(GameObject &ob1,GameObject &ob2)
{
    ob1.collide(ob2);
}

int main()
{
    SpaceShip oShip;
    SpaceStation oStation;
    Asteriod oAst;
    GameObject& g1=oShip;
    GameObject& g2=oStation;
    GameObject& g3=oAst;
	TestCollide(g1,g1);
	cout<<"==============================="<<endl;
	TestCollide(g1,g2);
	cout<<"==============================="<<endl;
	TestCollide(g1,g3);
	cout<<"==============================="<<endl;
	TestCollide(g2,g3);
    getchar();
    return 0;
}

结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值