类的实现、对象的定义和成员的调用
头文件
class Enemy
{
public:
int HP;
int Damage;
void Attack();
void TakeDamage();
};
源文件
void Enemy::Attack(){
cout<<"播放攻击动作"<<endl;
cout<<"播放攻击特效"<<endl;
cout<<"让别攻击的受伤"<<endl;
}
void Enemy::TakeDamage(){
cout<<"播放受伤动作"<<endl;
cout<<"播放受伤特效"<<endl;
cout<<"减少自身血量"<<endl;
}
主文件
int main()
{
Enemy enemy=Enemy();//我们使用自己定义的类声明的变量,可以成为对象
cout<<enemy.HP<<endl;
cout<<enemy.Damage<<endl;
enemy.Attack();
enemy.TakeDamage();
Enemy enemy2=Enemy();
string str;
cin>>str;
return 0;
}