2.25作业
作业:
编写一个如下场景:
有一个英雄Hero类,私有成员,攻击,防御,速度,生命值,以及所有的setget方法
编写一个 武器 Weapon 类,拥有私有成员攻击力,以及set get 方法
编写一个 长剑 Sword 类,继承自武器类,拓展属性 生命值,以及set get 方法
编写一个 匕首Blade类,继承自武器类,拓展属性 速度,以及set get 方法
编写一个 斧头 Axe类,继承自武器类,拓展属性 防御力,以及set get 方法
武器Weapon类里面,要求有一个多态函数,叫做 equip 函数
英雄Hero类里面,要求有一个公开函数,equipWeapon(Weapon*w)
实现功能:英雄既可以装备长剑,也可以装备短剑,也可以装备斧头,但是要求装备不同的武器,英雄需要获得不同的属性加成
#include <iostream>
#include <cstring>
using namespace std;
class Weapon
{
private:
int weapon_attack;
public:
Weapon(int weapon_attack = 0)
:weapon_attack(weapon_attack)
{}
void setWeapon_attack(int weapon_attack){this->weapon_attack = weapon_attack;}
int getWeapon_attack(){return weapon_attack;}
virtual int* equip(){return NULL;}
};
class Hero
{
private:
string user;
int attack;
int defense;
int speed;
int health;
public:
Hero(string user = NULL, int attack = 100,int defense = 100,int speed = 100,int health = 100)
:user(user),attack(attack),defense(defense),speed(speed),health(health)
{}
void setUser(string user){this->user = user;}
void setAttack(int attack){this->attack = attack;}
void setDefense(int defense){this->defense = defense;}
void setSpeed(int speed){this->speed = speed;}
void setHealth(int health){this->health = health;}
string getUser(){return user;}
int getAttack(){return attack;}
int getDefense(){return defense;}
int getSpeed(){return speed;}
int getHealth(){return health;}
void equipWeapon(Weapon* w)
{
cout<<"玩家信息"<<endl;
cout<<"------------------------------------------------"<<endl;
cout<<"姓名\t"<<"攻击力\t"<<"防御力\t"<<"速度\t"<<"生命值"<<endl;
cout<<user<<"\t"<<attack<<"\t"<<defense<<"\t"<<speed<<"\t"<<health<<endl;
cout<<"-------------------------------------------------"<<endl;
int *attributes = w->equip();
cout<<endl;
cout<<endl;
cout<<"综合属性"<<endl;
cout<<"------------------------------------------------"<<endl;
cout<<"姓名\t"<<"攻击力\t"<<"防御力\t"<<"速度\t"<<"生命值"<<endl;
cout<<user<<"\t"<<attack+attributes[0]<<"\t"<<defense+attributes[1]<<"\t"<<speed+attributes[2]<<"\t"<<health+attributes[3]<<endl;
cout<<"-------------------------------------------------"<<endl;
}
};
class Sword:public Weapon
{
public:
void setWeapon_attack(int weapon_attack){Weapon::setWeapon_attack(weapon_attack);}
int getWeapon_attack(){return Weapon::getWeapon_attack();}
int* equip()
{
cout<<"武器名:"<<"长剑"<<endl;
cout<<"攻击力: "<<"+100"<<endl;
cout<<"拓展属性: "<<"生命值"<<"+70"<<endl;
setWeapon_attack(100);
int* attributes = new int[4];
attributes[0] = getWeapon_attack();
attributes[1] = 0;
attributes[2] = 0;
attributes[3] = 70;
return attributes;
}
};
class Blade:public Weapon
{
public:
void setWeapon_attack(int weapon_attack){Weapon::setWeapon_attack(weapon_attack);}
int getWeapon_attack(){return Weapon::getWeapon_attack();}
int* equip()
{
cout<<"武器名: "<<"匕首"<<endl;
cout<<"攻击力: "<<"+50"<<endl;
cout<<"拓展属性: "<<"速度"<<"+100"<<endl;
setWeapon_attack(50);
int* attributes = new int[4];
attributes[0] = getWeapon_attack();
attributes[1] = 0;
attributes[2] = 100;
attributes[3] = 0;
return attributes;
}
};
class Axe:public Weapon
{
public:
void setWeapon_attack(int weapon_attack){Weapon::setWeapon_attack(weapon_attack);}
int getWeapon_attack(){return Weapon::getWeapon_attack();}
int* equip()
{
cout<<"武器名: "<<"斧头"<<endl;
cout<<"攻击力: "<<"+75"<<endl;
cout<<"拓展属性: "<<"防御力"<<"+100"<<endl;
cout<<"------------------------------------------------"<<endl;
cout<<"姓名\t"<<"攻击力\t"<<"防御力\t"<<"速度\t"<<"生命值"<<endl;
cout<<user<<"\t"<<attack<<"\t"<<defense<<"\t"<<speed<<"\t"<<health<<endl;
cout<<"-------------------------------------------------"<<endl;
setWeapon_attack(75);
int* attributes = new int[4];
attributes[0] = getWeapon_attack();
attributes[1] = 100;
attributes[2] = 0;
attributes[3] = 0;
return attributes;
}
};
int main()
{
Hero m("张三");
Weapon* ptr = new Sword;
m.equipWeapon(ptr);
return 0;
}
效果