required files
- weapon.cpp
- weapon.hpp
- vehicle.cpp
- vehicle.hpp
- soldier.cpp
- soldier.hpp
- gun.cpp
- gun.hpp
- riffle.cpp
- riffle.hpp
- shotgun.cpp
- shotgun.hpp
- jeep.cpp
- jeep.hpp
- blackbird.cpp
- blackbird.hpp
- sniper.cpp
- sniper.hpp
- infantry.cpp
- infantry.hpp
- captain_america.cpp
- captain_america.hpp
The C++ army
For this exercise, we will have to build a code that relies a lot on polymorphism and inheritance.
Just image you are developing a video game where you can reuse code as much as possible and the behavior is really predictable.
We will have to build different units for our army which will be divided into different categories as follow:
- Weapon
- Vehicle
- Soldier
You will use those categories as class names
// Example
class Weapon {};
class Vehicle {};
class Soldier {};
Weapon
methods
public virtual int Weapon::countAmmunition() = 0
which will return the number of ammunition availablepublic virtual void Weapon::fire() = 0
will printBANG with my <weapon_name>\n
and reduce the number of ammunition by -1. If ammunition is equal to 0 then display\n
only.public virtual string Weapon::model() = 0
which will return the name of your weapon
attributes
None
Vehicle
methods
public int Vehicle::countSoldier()
returns the number of soldier in the vehicle.public virtual void Vehicle::action() = 0
public void Vehicle::addSoldier(Soldier *)
add a soldier to our vehicule.public std::vector<Soldier*> Vehicle::unload()
will return avector
of soldiers and set the number of soldiers back to 0
attributes
private std::vector<Soldier*>
Soldier
methods
Soldier::Soldier(string name, string rank)
public void Soldier::scream()
will printAAAAHHH\n
public virtual void Soldier::identification()
will print<rank> <solider_name> Sir!\n
public virtual void Soldier::fireLeft()
willWeapon::fire()
left weapon. If no weapon is set, then do nothing.public virtual void Soldier::fireRight()
willWeapon::fire()
right weapon. If no weapon is set, then do nothing.public virtual void Soldier::setRight(Weapon *)
public virtual void Soldier::setLeft(Weapon *)
attributes
private Weapon* left
private Weapon* right
private string name
private string rank
Since the described classes all have at least one pure virtual method, we can't instantiate them directly without have a g++ error.
Gun
The Gun
class will inherit from a Weapon
class, it will have at least
protected int ammunition
protected string name
public Gun(string name, int ammo)
Riffle
The Riffle
class will inherit from a Gun
. The constructor will have to be like Riffle::Riffle()
and set the name to riffle and the ammo count to 10 automatically.
This class will only contain a constructor
ShotGun
The ShotGun
class will inherit from a Gun
. The constructor will have to be like ShotGun::ShotGun()
and set the name to shot_gun and the ammo count to 5 automatically.
This class will only contain a constructor
Jeep
The Jeep
class will inherit from a Vehicle
. The constructor will have to be Jeep::Jeep()
.
We will have to implement the method action()
which will print Fast and Furious sucks\n
BlackBird
The BlackBird
class will inherit from a Vehicle
. The constructor will have to be like BlackBird::BlackBird()
.
We will have to implement the method action()
which will print On my way guys! OVER\n
Sniper
The Sniper
class will inherit from a Soldier
. The constructor will have to be Sniper::Sniper(string name)
. The default rank will be sniper
.
Infantry
The Infantry
class will inherit from a Soldier
. The constructor will have to be Infantry::Infantry(string name)
. The default rank will be sergeant
.
CaptainAmerica
The CaptainAmerica
class will inherit from a Soldier
. The constructor will have to be CaptainAmerica::CaptainAmerica()
.
identification()
will print I am Captain America but I wish I was Batman.\n
setLeft(Weapon *)
and setRight(Weapon *)
will only print I don't need it, I have my vibranium shield.
fireLeft()
and fireRight()
will only print PLEASE HELP ME SpiderMan!!!\n
even he has weapons attached to his hands or not.
'C++ > 학교 숙제' 카테고리의 다른 글
session_4_ex infint.hpp (완성) (0) | 2018.04.22 |
---|---|
session_6_ex 답 (0) | 2018.04.22 |
string 문자열 뺄셈계산 (0) | 2018.04.15 |
string 문자열 덧셈계산 (0) | 2018.04.14 |
session_4_ex infint.h(객체 맴버변수가 string 일때) (0) | 2018.04.11 |
댓글