Rewrite the assignment 6 submission, the Mad Lib Game, so that there is a new object, a storyWordManager object, instantiated from a StoryWordManager class. As this assignment requires an understanding of both pointers and classes, you will need to read Chapter 7 and Chapter 8 of our "Beginning C++ Through Game Programming" textbook. Note that for this and all subsequent assignments, each class will have its own ".h" file and its own ".cpp" file. This provides a valuable separation of an object interface from its implementation, respectively. These files will be named "MyClass.h" and "MyClass.cpp", where "MyClass" is replaced with the name of that particular class. In other words, in this assignment, you will have two new files, StoryWordManager.h and StoryWordManager.cpp.
Note also that for this and all subsequent assignments, each ".h" file will prevent multiple inclusions by making use of the "#ifndef" preprocessor directive "trick". This trick is described in detail on Page 396 of our supplementary textbook, "C++ Primer Plus". The gist of this technique is to ensure that every ".h" file begins with
#ifndef MYCLASS_H_
#define MYCLASS_H_
and ends with #endif
where "MYCLASS" is replaced with the name of the class being defined. If you ever encounter a "multiple definition" compiler error, it is most likely due to your having forgotten to make use of this technique or "trick". The madLib object will be given a pointer to the storyWordManager object as a parameter that is passed into the MadLib constructor. This means that within your "main" method, you will need to instantiate the storyWordManager object before you instantiate the madLib object (since you can't use a pointer to an object that doesn't yet exist!).
The storyWordManager object's constructor method will welcome the user to the game, prompt the user for the necessary story words (or numbers), and accept input from the cin object. The madLib object is responsible for building the story via string concatenation within its "tellStory" function. The madLib object will use its storyWordManagerPtr member variable (initialized within the MadLib constructor) to invoke public "get" methods upon the storyWordManager object to access the necessary story words as the madLib object builds and "tells" its story.
the following is my code that I had for my previous assignment...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Mad-Lib 4.0
// Mad-Lib game with classes and pointers
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class StoryWordManager
{
public:
StoryWordManager();
string getName();
string getNoun();
string getBodyPart();
string getVerb();
int getNumber();
string m_Name;
string m_Noun;
string m_BodyPart;
string m_Verb;
int m_Number;
//private:
string m_Prompt;
string askText();
int askNum();
};
StoryWordManager::StoryWordManager()
{
cout << "Welcome to the Mad-Libs game.\n\n" << endl;
cout << "Answer the questions to help create a story:\n\n" << endl;
m_Prompt = "name";
m_Name = askText();
m_Prompt = "noun";
m_Noun = askText();
m_Prompt = "body part";
m_BodyPart = askText();
m_Prompt = "verb";
m_Verb = askText();
m_Prompt = "number";
m_Number = askNum();
}
string StoryWordManager::askText()
{
string text;
cout << "Please enter a " << m_Prompt << ":";
cin >> text;
return text;
}
int StoryWordManager::askNum()
{
int num;
cout << "Please enter a number:";
cin >> num;
return num;
}
string StoryWordManager::getName()
{
return m_Name;
}
string StoryWordManager::getNoun()
{
return m_Noun;
}
string StoryWordManager::getBodyPart()
{
return m_BodyPart;
}
string StoryWordManager::getVerb()
{
return m_Verb;
}
int StoryWordManager::getNumber()
{
return m_Number;
}
class MadLib
{
public:
MadLib(StoryWordManager*);
};
MadLib::MadLib(StoryWordManager* StoryWordManagerPtr)
{
cout << "\n\nHere's your story:\n\n";
cout << "The famous explorer ";
cout << (*StoryWordManagerPtr).getName();
cout << " had nearly given up a life-long quest to find\n";
cout << "The Lost City of ";
cout << (*StoryWordManagerPtr).getNoun();
cout << " when one day, the ";
cout << (*StoryWordManagerPtr).getNoun();
cout << " found the explorer.\n\n";
cout << "Surrounded by ";
cout << (*StoryWordManagerPtr).getNumber();
cout << " " << (*StoryWordManagerPtr).getNoun();
cout << ", a tear came to ";
cout << (*StoryWordManagerPtr).getName() << "'s ";
cout << (*StoryWordManagerPtr).getBodyPart() << ".\n";
cout << "After all this time, the quest was finally over. ";
cout << "And then, the ";
cout << (*StoryWordManagerPtr).getNoun() << "\n";
cout << "promptly devoured ";
cout << (*StoryWordManagerPtr).getName() << ". ";
cout << "\n\nThe moral of the story?... Be careful what you wish for";
}
int main()
{
StoryWordManager userInput;
userInput.askText();
userInput.askNum();
StoryWordManager* StoryWordManagerPtr;
return 0;
}
| |
I just don't understand how to go about disecting that into five different files (two object .h's, two object .cpp's, and a main .cpp).
I had something but it was only one .h and 2 .cpp's so it wouldn't have passed for something acceptable. (I also accidently hit save (when I was altering the code) so I really have nothing left from those three files either). BACK TO SQUARE ONE
Thanks for any help in advance. I have some projects coming up similar to this but I just need help breaking everything down so I can understand the future assignments (2 .h's, 2 .cpp's, 1 main .cpp).