Simple Computer Game Design by using Class-inheritance and polymorphism
Question :
IUE LAB8 - Goruntulemek icin Dokununuz
Ornek Kod :
#include <iostream>
#include <string>
int completionPercentage= 0;
double timeLeft =10.0;
using namespace std;
class Activity{
protected :
static int healthPoint;
public :
/*Activity()
{
healthPoint=100;
}*/
static int getHealth()
{
return healthPoint;
}
virtual void done()=0;
};
int Activity::healthPoint=100;
class Fight : public Activity{
public :
void done()
{
timeLeft=timeLeft-1.5;
healthPoint=healthPoint-25;
completionPercentage=completionPercentage+20;
}
};
class SitDown : public Activity{
public :
void done()
{
timeLeft=timeLeft-1.0;
healthPoint=healthPoint+25;
}
};
class Drink : public Activity{
public :
void done()
{
timeLeft=timeLeft-0.5;
healthPoint=healthPoint+10;
}
};
int main()
{
int i,b=0;
Activity *p1=new Fight();
Activity *p2=new Drink();
Activity *p3=new SitDown();
for(i=5 ; i>4 ; i++)
{
cout << "Time Left : " << timeLeft << endl;
cout << "HealthPoint : " << p1->getHealth() << endl;
cout << "Percantage : " << completionPercentage << endl;
cout <<"islem sec : 1-Fight , 2-Drink , 3-SitDown 4-Exit " << endl;
cin >>b;
switch(b)
{
case 1 : p1->done(); break;
case 2: p2->done(); break;
case 3 : p3->done(); break;
}
if(b==4)
{
i=3;
}
}
cout << "You have just quit the game Here your results " << endl;
cout << "Time Left : " << timeLeft << endl;
cout << "HealthPoint : " << p1->getHealth() << endl;
cout << "Percantage : " << completionPercentage << endl;
}
Hiç yorum yok:
Yorum Gönder