C++ : Define a new class Fighter that inherits from the Spaceship class below. - Add a private member variable: int firePower; - Create a default constructor which initializes firePower to 10 and initializes name to "X-wing" - Create a member function in Fighter void warp(). This function should increase position by 10, and decrease health by 5. - Redefine function wasHit(). Instead of decrementing health by 5, decrement it by 2. class Spaceship { protected: string name; int health; int position; public: Spaceship() { name = ""; health = 100; position = 0; } void moveLeft() { position--; } void moveRight() { position++; } void wasHit() { health -= 5; }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
C++ : Define a new class Fighter that inherits from the Spaceship class below.
- Add a private member variable:
int firePower;
- Create a default constructor which initializes firePower to 10 and initializes name to "X-wing"
- Create a member function in Fighter void warp(). This function should increase position by 10, and decrease health by 5.
- Redefine function wasHit(). Instead of decrementing health by 5, decrement it by 2.
class Spaceship {
protected:
string name;
int health;
int position;
public:
Spaceship()
{ name = "";
health = 100;
position = 0; }
void moveLeft()
{ position--; }
void moveRight()
{ position++; }
void wasHit()
{ health -= 5; }
};
Trending now
This is a popular solution!
Step by step
Solved in 2 steps