< Previous | Contents | Next >

Introducing the Simple Boss 2.0

Program

The Simple Boss 2.0 program is another version of the Simple Boss program from earlier in this chapter. The new version, Simple Boss 2.0, looks exactly the same to the user, but the code is a little different because I put some restrictions on base class members. If you want to see what the program does, take a look back at Figure 10.2.

You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 10 folder; the filename is simple_boss2.cpp.

//Simple Boss 2.0

//Demonstrates access control under inheritance


#include <iostream> using namespace std;


class Enemy

{

public:

Enemy();

void Attack() const;


protected:

int m_Damage;

};


Enemy::Enemy(): m_Damage(10)

{}


void Enemy::Attack() const

{

cout << "Attack inflicts " << m_Damage << " damage points!\n";

}


class Boss : public Enemy

{

public:

Boss();

void SpecialAttack() const;

Controlling Access under Inheritance 339



private:

int m_DamageMultiplier;

};


Boss::Boss():

m_DamageMultiplier(3)

{}


void Boss::SpecialAttack() const

{

cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";

}


int main()

{

cout << "Creating an enemy.\n"; Enemy enemy1;

enemy1.Attack();


cout << "\nCreating a boss.\n"; Boss boss1;

boss1.Attack(); boss1.SpecialAttack();


return 0;

}