您的位置:首页 > 其它

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

2012-11-27 15:20 405 查看
普通函数重载

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


结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐