Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROG1
- will have multiple classes
- text
- Movie
- movies
- read program one grade sheet
- comment above functions
- Text class //class has two private attributes
- const char* textArray //dynamically allocated
- int textLength
- Movie class //this is all one movie object. holds a public interface (public functions)
- Text* movieTitle //text* indicates it will point to the text class. receiving information from the text class
- Text* movieGenre
- int movieLength
- Text* movieRating
- int movieOscars = somenumber
- float movieStars = somenumber
- int movieYear = somenumber
- Movies class //has 3 attributes
- int maxMovies //how many movies can be held in the library
- int numMovies //how many are currently owned
- Movie** movieLibrary //two stars indicating the var is a pointer pointing to an array of pointers.
- //points to movie class most likely
- a driver will allow the user to add, edit, delete, print etc movies in their library
- prog1 ex games
- game.h
- #ifnotdef GAME_H
- #define GAME_h
- #include "Text.h"
- class Game
- {
- private:
- Text* gameName;
- Text* gameDesc;
- float gameCost;
- int numPlayers;
- float gameDurationInHours;
- public:
- Game (Text*, Text*, float, int, float);
- ~Game();
- void printGameDetails();
- void printGameName();
- void editGameDetails(); //most difficult
- //accessor
- Text* getGameName()
- {
- return gameName;
- }
- float getGameCost()
- {
- return gameCost;
- }
- //mutator
- void setGameName(Text* n)
- {
- gameName = n;
- }
- }
- #endif
- game.cpp
- #include "Game.h"
- Game::Game (Text* n, Text* d, float c, int p, float h);
- {
- gameName = n;
- gameDesc = d;
- gameCost = c;
- numPlayers = p;
- gameDurationInHours = h;
- }
- Game::~Game()
- {
- delete gameName;
- delete gameDesc;
- }
- void Game::printGameDetails()
- {
- cout << "NAME : ";
- gameName -> displayText();//displaytext is in text.cpp
- cout << endl;
- cout << "DESCRIPTION : ";
- gameDesc->displayText();
- cout << numPlayers;
- cout << gameCost;
- cout << gameDurationInHours;
- }
- void Game::printGameName()
- {
- gameName->displayText();
- }
- void Game::printGameDetailsToFile(ofstream &outfile)
- {
- char temp[1000];
- strncpy(temp, gameName->getText(), 1000)
- outFile << temp;
- strncpy(temp, gameDesc->getText(), 1000)
- outFile << temp;
- outFile << numPlayers << endl;
- outFile << gameCost << endl;
- outFile << gameDurationInHours << endl;
- }
- void Game::editGameDetails()
- {
- int choice;
- do
- {
- cout << "what do you want to edit?\n";
- cout << "1. name\n";
- cout << "2. description\n";
- cout << "3."
- .....
- cin >> choice;
- //validate user choice
- switch(choice)
- {
- case 1: cout << "\nCurrent Name: ";
- gameName->displayText()
- cout << endl;
- cout << "New name: ";
- cin.getline (temp,1000);
- gameName->editText(temp);
- break;
- case 2: //same but for description etc
- case 3: cout <<"\ncurrent cost: " << gameCost <<endl;
- cout <<"new cost: "
- cin >> gameCost;
- }
- }while
- }
- Games.h
- #ifnotdef GAMES_H
- #define GAMES_H
- class Games
- {
- private:
- int maxGames;
- int numGames;
- Game ** gameLibrary;
- public:
- Games(int);
- ~Games();
- void resizeGameArray();
- void addGameToLibrary();
- void removeGameFromLibrary();
- void displayGames();
- void displayGameNames();
- void readGamesFromFile(char *filename);
- void saveToFile(char *filename);
- }
- #endif
- games.cpp
- #include "Games.h"
- #include <iostream>
- using namespace std;
- Games::Games(int);
- {
- //dynamically allocate array
- gameLibrary = new Game*[max];
- maxGames = max;
- numGames = 0;
- }
- Games::~Games()
- {
- for(int x=0; x<numGames; x++)
- {
- delete gameLibrary[x]
- }
- delete [] gameLibrary; //must be done in this order
- }
- void Games::resizeGameArray()
- {
- int max = maxGames * 2;
- Game** newGameLibrary = new Game*[max];
- for(int x=0; x<numGames; x++)
- {
- newGameLibrary[x] = gameLibrary[x];
- }
- delete[] gameLibrary;
- gameLibrary = newGameLibrary;
- maxGames = max;
- }
- void Games::addGameToLibrary()
- {
- char tempName[10000];
- char tempDesc[10000];
- float cost, gameDuration;
- int numPlayers;
- //get info from user
- cout << "\n NAME: ";
- cin.getline(tempname, 10000);
- Text *name = new Text(tempName);
- cout << "\nDESCRIPTION: ";
- cin.getline (tempDesc, 10000);
- Text *description = new Text(tempDesc);
- cout << "\nCOST: ";
- cin >> cost;
- cout << "\nNUMBER OF PLAYERS: ";
- cin >> numPlayers;
- cuot << "\nDURATION IN HOURS: ";
- cin >> gameDuration;
- //create a new game object
- Game *newGame = new Game(name, description, cost, numPlayers, gameDuration); //this MUST be in the same order as class
- //add this game to the library
- if (numGames == maxGames)
- resizeGameArray();
- gameLibrary[numGames] = newGame;
- numGames++;
- }
- void Games::removeGameFromLibrary()
- {
- int gameChoice;
- if(numGames <= 1)
- {
- cout << "\n\nThere must be at least one game in library to delete\n";
- }
- else
- {
- cout << "\n\nChoose from the following games to remove: \n";
- displayGameNames();
- cout << "\nChoose a game between 1 & " << numGames << ": \n";
- cin >> gameChoice;
- int gameIndexToBeRemoved = gameChoice-1;
- //delete the game
- delete gameLibrary[gameIndexToBeRemoved];
- int numElementsToMoveBack = numGames-1;
- for(int x=gameIndexToBeRemoved; x<numElementsToMoveBack; x++)
- {
- gameLibrary[x] = gameLibrary[x+1];
- }
- gameLibrary[numElementsToMoveBack] = NULL;
- numGames--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement