Homework #6 Solution




  • Cyclic Association

  • Operator overloading

  • Separate compilation

  • Namespace


We are revisiting the world of hw03, adding one new feature: warriors can runaway!!! Of course, when a warrior runs away, he has to inform his noble. Which means he has to know who has hired him and be able to communicate with him. Which means, obviously, that he will need a pointer to his noble.

You will also:

  • Use separate compilation for all methods, giving each class its own header and implementation files.

  • Place the code in the WarriorCraft namespace.

  • Implement an output operator for Nobles.

Test Code and Output

Below is a test program and its output. The only change in the test program from the one in hw03 is that cheetah runs away, rather than getting fired. The output should also only be different in the one line of output corresoponding to cheetal running away.

#include “Noble.h”

#include “Warrior.h”

#include <iostream>

#include <vector>

#include <string>

using namespace std;

using namespace WarriorCraft;

int main() {

Noble art(“King Arthur”);

Noble lance(“Lancelot du Lac”);

Noble jim(“Jim”);

Noble linus(“Linus Torvalds”);

Noble billie(“Bill Gates”);

Warrior cheetah(“Tarzan”, 10);

Warrior wizard(“Merlin”, 15);

Warrior theGovernator(“Conan”, 12);

Warrior nimoy(“Spock”, 15);

Warrior lawless(“Xena”, 20);

Warrior mrGreen(“Hulk”, 8);

Warrior dylan(“Hercules”, 3);








cout << jim << endl;

cout << lance << endl;

cout << art << endl;

cout << linus << endl;

cout << billie << endl;


cout << art << endl;






Jim has an army of 1

Spock: 15

Lancelot du Lac has an army of 2

Conan: 12

Hercules: 3

King Arthur has an army of 2

Merlin: 15

Tarzan: 10

Linus Torvalds has an army of 1

Xena: 20

Bill Gates has an army of 1

Hulk: 8

Tarzan flees in terror, abandoning his lord, King Arthur

King Arthur has an army of 1

Merlin: 15

King Arthur battles Lancelot du Lac

Mutual Annihalation: King Arthur and Lancelot du Lac die at each other’s hands

Jim battles Lancelot du Lac

He’s dead, Jim

Linus Torvalds battles Bill Gates

Linus Torvalds defeats Bill Gates

Bill Gates battles Lancelot du Lac

Oh, NO! They’re both dead! Yuck!

Turn in

Since this will involve multiple files, turn in a zip file, called hw06.zip, containing all of your files.

Grader comments:

Never use a method which returns a vector.Whole point of encapsulation is lost.Please avoid that in the future(-2)


End the loop once the work is done i.e you need to break out of the loop once you find your intended warrior, this is good programming practice.(In your fire method) (-1)