Advertisement
JoshuaNHardison

Program1Notes

Sep 16th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. PROG1
  2. will have multiple classes
  3. text
  4. Movie
  5. movies
  6. read program one grade sheet
  7. comment above functions
  8.  
  9. Text class //class has two private attributes
  10. const char* textArray //dynamically allocated
  11. int textLength
  12. Movie class //this is all one movie object. holds a public interface (public functions)
  13. Text* movieTitle //text* indicates it will point to the text class. receiving information from the text class
  14. Text* movieGenre
  15. int movieLength
  16. Text* movieRating
  17. int movieOscars = somenumber
  18. float movieStars = somenumber
  19. int movieYear = somenumber
  20. Movies class //has 3 attributes
  21. int maxMovies //how many movies can be held in the library
  22. int numMovies //how many are currently owned
  23. Movie** movieLibrary //two stars indicating the var is a pointer pointing to an array of pointers.
  24. //points to movie class most likely
  25. a driver will allow the user to add, edit, delete, print etc movies in their library
  26.  
  27.  
  28.  
  29.  
  30. prog1 ex games
  31.  
  32. game.h
  33. #ifnotdef GAME_H
  34. #define GAME_h
  35.  
  36. #include "Text.h"
  37.  
  38. class Game
  39. {
  40. private:
  41. Text* gameName;
  42. Text* gameDesc;
  43. float gameCost;
  44. int numPlayers;
  45. float gameDurationInHours;
  46.  
  47. public:
  48. Game (Text*, Text*, float, int, float);
  49. ~Game();
  50. void printGameDetails();
  51. void printGameName();
  52. void editGameDetails(); //most difficult
  53.  
  54. //accessor
  55. Text* getGameName()
  56. {
  57. return gameName;
  58. }
  59.  
  60. float getGameCost()
  61. {
  62. return gameCost;
  63. }
  64. //mutator
  65. void setGameName(Text* n)
  66. {
  67. gameName = n;
  68. }
  69. }
  70.  
  71.  
  72. #endif
  73.  
  74.  
  75.  
  76. game.cpp
  77. #include "Game.h"
  78.  
  79. Game::Game (Text* n, Text* d, float c, int p, float h);
  80. {
  81. gameName = n;
  82. gameDesc = d;
  83. gameCost = c;
  84. numPlayers = p;
  85. gameDurationInHours = h;
  86. }
  87.  
  88. Game::~Game()
  89. {
  90. delete gameName;
  91. delete gameDesc;
  92. }
  93.  
  94. void Game::printGameDetails()
  95. {
  96. cout << "NAME : ";
  97. gameName -> displayText();//displaytext is in text.cpp
  98. cout << endl;
  99. cout << "DESCRIPTION : ";
  100. gameDesc->displayText();
  101. cout << numPlayers;
  102. cout << gameCost;
  103. cout << gameDurationInHours;
  104. }
  105.  
  106. void Game::printGameName()
  107. {
  108. gameName->displayText();
  109. }
  110.  
  111. void Game::printGameDetailsToFile(ofstream &outfile)
  112. {
  113. char temp[1000];
  114. strncpy(temp, gameName->getText(), 1000)
  115. outFile << temp;
  116. strncpy(temp, gameDesc->getText(), 1000)
  117. outFile << temp;
  118.  
  119. outFile << numPlayers << endl;
  120. outFile << gameCost << endl;
  121. outFile << gameDurationInHours << endl;
  122. }
  123.  
  124. void Game::editGameDetails()
  125. {
  126. int choice;
  127.  
  128. do
  129. {
  130. cout << "what do you want to edit?\n";
  131. cout << "1. name\n";
  132. cout << "2. description\n";
  133. cout << "3."
  134. .....
  135. cin >> choice;
  136. //validate user choice
  137. switch(choice)
  138. {
  139. case 1: cout << "\nCurrent Name: ";
  140. gameName->displayText()
  141. cout << endl;
  142. cout << "New name: ";
  143. cin.getline (temp,1000);
  144. gameName->editText(temp);
  145. break;
  146. case 2: //same but for description etc
  147. case 3: cout <<"\ncurrent cost: " << gameCost <<endl;
  148. cout <<"new cost: "
  149. cin >> gameCost;
  150. }
  151. }while
  152. }
  153.  
  154.  
  155. Games.h
  156.  
  157. #ifnotdef GAMES_H
  158. #define GAMES_H
  159.  
  160. class Games
  161. {
  162. private:
  163. int maxGames;
  164. int numGames;
  165. Game ** gameLibrary;
  166. public:
  167. Games(int);
  168. ~Games();
  169.  
  170.  
  171. void resizeGameArray();
  172. void addGameToLibrary();
  173. void removeGameFromLibrary();
  174. void displayGames();
  175. void displayGameNames();
  176. void readGamesFromFile(char *filename);
  177. void saveToFile(char *filename);
  178. }
  179.  
  180. #endif
  181.  
  182. games.cpp
  183.  
  184. #include "Games.h"
  185. #include <iostream>
  186.  
  187. using namespace std;
  188.  
  189. Games::Games(int);
  190. {
  191. //dynamically allocate array
  192. gameLibrary = new Game*[max];
  193. maxGames = max;
  194. numGames = 0;
  195. }
  196.  
  197. Games::~Games()
  198. {
  199. for(int x=0; x<numGames; x++)
  200. {
  201. delete gameLibrary[x]
  202. }
  203. delete [] gameLibrary; //must be done in this order
  204. }
  205.  
  206. void Games::resizeGameArray()
  207. {
  208. int max = maxGames * 2;
  209. Game** newGameLibrary = new Game*[max];
  210. for(int x=0; x<numGames; x++)
  211. {
  212. newGameLibrary[x] = gameLibrary[x];
  213. }
  214. delete[] gameLibrary;
  215. gameLibrary = newGameLibrary;
  216. maxGames = max;
  217. }
  218.  
  219. void Games::addGameToLibrary()
  220. {
  221. char tempName[10000];
  222. char tempDesc[10000];
  223. float cost, gameDuration;
  224. int numPlayers;
  225.  
  226. //get info from user
  227. cout << "\n NAME: ";
  228. cin.getline(tempname, 10000);
  229. Text *name = new Text(tempName);
  230. cout << "\nDESCRIPTION: ";
  231. cin.getline (tempDesc, 10000);
  232. Text *description = new Text(tempDesc);
  233. cout << "\nCOST: ";
  234. cin >> cost;
  235. cout << "\nNUMBER OF PLAYERS: ";
  236. cin >> numPlayers;
  237. cuot << "\nDURATION IN HOURS: ";
  238. cin >> gameDuration;
  239.  
  240. //create a new game object
  241. Game *newGame = new Game(name, description, cost, numPlayers, gameDuration); //this MUST be in the same order as class
  242.  
  243. //add this game to the library
  244. if (numGames == maxGames)
  245. resizeGameArray();
  246. gameLibrary[numGames] = newGame;
  247.  
  248. numGames++;
  249. }
  250.  
  251. void Games::removeGameFromLibrary()
  252. {
  253. int gameChoice;
  254. if(numGames <= 1)
  255. {
  256. cout << "\n\nThere must be at least one game in library to delete\n";
  257. }
  258. else
  259. {
  260. cout << "\n\nChoose from the following games to remove: \n";
  261. displayGameNames();
  262. cout << "\nChoose a game between 1 & " << numGames << ": \n";
  263. cin >> gameChoice;
  264.  
  265. int gameIndexToBeRemoved = gameChoice-1;
  266. //delete the game
  267. delete gameLibrary[gameIndexToBeRemoved];
  268.  
  269. int numElementsToMoveBack = numGames-1;
  270. for(int x=gameIndexToBeRemoved; x<numElementsToMoveBack; x++)
  271. {
  272. gameLibrary[x] = gameLibrary[x+1];
  273. }
  274. gameLibrary[numElementsToMoveBack] = NULL;
  275.  
  276. numGames--;
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement