Ok so, this IS a homework assignment. I'm not asking for the answer. I need help on how to get started. Any help would be greatly appreciated. The assignment is to revise some code from a previous assignment, but I'm having trouble with figuring it out. The instructions are:
You will revise your code from Programming Assignment 4 in the following ways:
1. Place your fairy data into a text file. You will open the file and read it in order to construct your fairies.
2. Make all inherited methods virtual. You will call all methods using a base class pointer.
3. Dynamically create your fairies and store the pointer to each fairy in a vector. The vector type is the base class.
4. You may access each fairy from the vector using either the brackets operator, i.e., array access, or an iterator.
5. When you are finished, delete each fairy object.
The original code below creates a game where the user encounters different fairies. Each fairy acts differently. Some reward points, some take away points. I'm just having trouble revising my code. Can anyone help please?
Here is my code (its rather long)
main
#include <stdio.h>
#include <iostream>
#include "Fairy.h"
#include "GoodFairy.h"
#include "EvilFairy.h"
#include "EquiFairy.h"
using namespace std;
int main()
{
GoodFairy f1("Bob");
EquiFairy f2("Joe");
EvilFairy f3("Billy-Bob");
GoodFairy f4("John");
EquiFairy f5("Joe-Bob");
int totalPoints = 0;
totalPoints += f1.talkToFairy();
totalPoints += f2.talkToFairy();
totalPoints += f3.talkToFairy();
totalPoints += f4.talkToFairy();
totalPoints += f5.talkToFairy();
cout << "Your total score is: " << totalPoints <<endl;
if(totalPoints < 0)
cout << "COMMON BRO! IN THE NEGATIVES??" << endl;
else if(totalPoints == 0)
cout << "I give you credit for trying" << endl;
else if(totalPoints > 0)
cout << "OH YES! THERE WE GO! A TRUE GAMER, SCORIN BIG!" << endl;
else
cout << "error has been encountered yo, try again!" << endl;
cout << endl;
cout << "Thank you for playing!" << endl;
return 0;
}
Fairy.h
#ifndef FAIRY_H
#define FAIRY_H
#include <iostream>
using namespace std;
class Fairy
{
protected:
string name;
int decision;
int amountOfPoints;
int Fairywishes;
public:
Fairy(string);
~Fairy();
int talkToFairy();
virtual void askForWish();
virtual void helpFairy();
void sayBye();
};
#endif
Fairy.cpp
#include "Fairy.h"
#include <iostream>
using namespace std;
Fairy::Fairy( string n )
{
name = n;
amountOfPoints = 0;
Fairywishes = 0;
}
Fairy::~Fairy()
{}
int Fairy::talkToFairy()
{
cout << endl;
cout << "Hello there. My name is " <<name.data() << " and I am mystical fairy!" <<endl;
do{
cout << endl;
cout << "How can I assist you today?" << endl;
cout << "1. Can I get a wish?" << endl;
cout << "2. Do you need any help?" << endl;
cout << "3. I think that's all for today. Bye!" << endl;
cout << endl;
cin >> decision;
switch(decision)
{
case 1:
askForWish();
break;
case 2:
helpFairy();
break;
case 3:
sayBye();
break;
default:
cout << "Please enter 1, 2, or 3 to make you decision: " << endl;
}
} while(decision != 3);
return amountOfPoints;
}
void Fairy::askForWish()
{
cout << "Sure :)" << endl;
}
void Fairy::helpFairy()
{
cout << "Sure :)" << endl;
}
void Fairy::sayBye()
{
cout << "Okay then. See ya next time!" << endl;
}
GoodFairy.h
#ifndef GOODFAIRY_H
#define GOODFAIRY_H
#include "Fairy.h"
class GoodFairy : public Fairy
{
protected:
bool help;
public:
GoodFairy(string);
~GoodFairy();
void askForWish();
void helpFairy();
void sayBye();
};
#endif
GoodFairy.cpp
#include "GoodFairy.h"
GoodFairy::GoodFairy(string w) : Fairy(w), help(false)
{}
GoodFairy::~GoodFairy()
{}
void GoodFairy::askForWish()
{
if(Fairywishes < 5)
{
cout << "You wish has been granted" << endl;
cout << "You have earned 10 points! :)" << endl;
amountOfPoints += 10;
Fairywishes++;
}
else
{
cout << "I'm out of magic dust! No more wishes :(" << endl;
}
}
void GoodFairy::helpFairy()
{
if (!help)
{
cout << "Yeah! I do need some help as a matter of fact." << endl;
cout << "You have earned 50 point! :D" << endl;
amountOfPoints += 50;
help = true;
}
else
{
cout << "Umm...nah...I got this yo." << endl;
}
}
void GoodFairy::sayBye()
{
cout << "Okay, Goodbye!" << endl;
}
EvilFairy.h
#ifndef EVILFAIRY_H
#define EVILFAIRY_H
#include "Fairy.h"
class EvilFairy : public Fairy
{
public:
EvilFairy(string);
~EvilFairy();
void askForWish();
void helpFairy();
void sayBye();
};
#endif
EvilFairy.cpp
#include "EvilFairy.h"
EvilFairy::EvilFairy(string w) : Fairy(w)
{}
EvilFairy::~EvilFairy()
{}
void EvilFairy::askForWish()
{
cout << "No. I will not grant you a wish...ever." << endl;
cout << "You have lost 10 points :(" << endl;
amountOfPoints -= 10;
}
void EvilFairy::helpFairy()
{
cout << "Me? Need help? Never! I'm the smartest fairy ever!" << endl;
cout << "You have lost 50 points :(" << endl;
amountOfPoints -=50;
}
EquiFairy.h
#ifndef EQUIFAIRY_H
#define EQUIFAIRY_H
#include "Fairy.h"
class EquiFairy : public Fairy
{
public:
EquiFairy(string);
~EquiFairy();
void askForWish();
void helpFairy();
void sayBye();
int randomDecision();
};
#endif
EquiFairy.cpp
#include "EquiFairy.h"
EquiFairy::EquiFairy(string w) : Fairy(w)
{}
EquiFairy::~EquiFairy()
{}
void EquiFairy::askForWish()
{
if(Fairywishes < 3)
{
int x = randomDecision();
if(x == 1)
{
cout << "Sure..why not? You wish has been granted." << endl;
cout << "You have earned 10 points! :)" << endl;
amountOfPoints += 10;
Fairywishes++;
}
else if(x == 2)
{
cout << "I am so not in the mood to grant you a wish right now..." << endl;
cout << "You have lost 10 points! :(" << endl;
amountOfPoints -= 10;
}
}
else
{
cout << "Umm...if I recall correctly...I already granted you 3 wishes. No more!" << endl;
}
}
void EquiFairy::helpFairy()
{
cout << "I'll think about it" << endl;
}
int EquiFairy::randomDecision()
{
int i = (rand()%2)+1;
return i;
}