class actor//基类
{
protected:
int m_iHealth;
const int m_iSpeed;//为了方便,这个值使用延时值,越大表示速度越慢
const int m_iHitPoint;
char m_strName[260];
public:
actor(const char* strName,const int iHealth,const int iSpeed,const int iHitpoint);
int& Health(){ return m_iHealth; };
const char* getName(){ return m_strName; };
virtual void hit(actor *Actor) = 0;
bool isAlive();
};
actor::actor(const char* strName,const int iHealth,const int iSpeed,const int iHitpoint):
m_iHealth(iHealth),
m_iSpeed(iSpeed),
m_iHitPoint(iHitpoint)
{
strcpy(m_strName,strName);
}
bool actor::isAlive()
{
return (m_iHealth>0);
}
/////////////////////////////////////////////////////////
//类fighter
class fighter :public actor
{
public:
fighter(const char* strName);
virtual void hit(actor *Actor);
private:
};
fighter::fighter(const char* strName):
actor(strName,100,20,20)
{
}
void fighter::hit(actor *Actor)
{
Sleep(m_iSpeed);
if(!isAlive())
{
return ;
}
if(Actor&&Actor->isAlive())
{
//这里我们用一个函数做了左值,因为函数返回的是引用
if(isAlive())
Actor->Health() = Actor->Health() - m_iHitPoint;
}
}
/////////////////////////////////////////////////////////
//类knight
class knight :public actor
{
public:
knight(const char* strName);
virtual void hit(actor *Actor);
private:
};
knight::knight(const char* strName):
actor(strName,150,20,25)
{
}
void knight::hit(actor *Actor)
{
Sleep(m_iSpeed);
if(!isAlive())
{
return ;
}
if(Actor&&Actor->isAlive())
{
if(isAlive())
Actor->Health() = Actor->Health() - m_iHitPoint;
}
}
/////////////////////////////////////////////////////////
//类warrior
class warrior :public actor
{
public:
warrior(const char* strName);
virtual void hit(actor *Actor);
private:
};
warrior::warrior(const char* strName):
actor(strName,150,20,25)
{
}
void warrior::hit(actor *Actor)
{
Sleep(m_iSpeed);
if(!isAlive())
{
return ;
}
if(Actor&&Actor->isAlive())
{
if(isAlive())
Actor->Health() = Actor->Health() - m_iHitPoint;
}
}
|
相关推荐
C++ 编程杂谈之一:编译器 在编程领域中,选择合适的编译器是非常重要的。编译器是把 C++ 代码生成为可执行文件的工具,而不是语言本身。VC(Visual C++)和 BCB(Borland C++ Builder)是两个常用的编译器。 在...
"主流编程语言的选择和学习杂谈" 本文主要介绍了当前主流编程语言的特点、发展趋势和学习方法,为新手提供了学习参考。 一、Java 语言 Java 语言是由 Sun 公司开发的,目前由 Java Community Process 控制。Java ...
《程序设计经验杂谈》是针对C和C++编程的一份经典资料,以CHM(Microsoft Help Compiler)格式呈现,这种格式通常用于汇集大量的技术文档和教程。CHM文件是微软开发的帮助文件系统,它将多个HTML页面、图像和其他...
- **面向对象编程基础**:学习类和对象的概念,封装、继承和多态性等面向对象编程的核心技术。 - **深入探索高级主题**:如模板、多重继承、异常处理等更复杂的编程概念和技术。 #### 具体章节知识点解析 ##### 第...
8. **指针与面向对象编程**:介绍C++中指针如何在类和对象中工作,特别是作为成员变量和成员函数返回值的情况。 9. **指针在实际项目中的应用**:通过案例分析,展示指针在解决实际问题和优化算法性能上的重要性。 ...
C++或Java则适合深入理解面向对象编程。 3. **算法与数据结构**:程序设计的核心是解决问题,这往往涉及算法的选择和应用。基础算法如排序(冒泡、快速、归并)、查找(线性、二分)以及递归等,是每个程序员必备的...
在象C++这样的面向对象的语言中,要进行测试的基本单元是类。对Ada语言来说,开发人员可以选择是在独立的过程和函数,还是软件测试中单元测试和测试驱动开发(TDD)杂谈在一种传统的结构化编程语言中,比如C,要进行...