mercredi 7 juin 2017

Creating a database to store different objects like books and games

I made a question last week about an issue i was having inside of my code, I've come back with a better representation of the problem. I have 2 classes called Book and Game. Below i will detail about what is inside of each class --Game.h is below

ifndef GAME_H
#define GAME_H
#include <iostream>

using namespace std;
class Game
{
    public:
        Game();
        virtual ~Game();
        void setName(string tmpName);
        void setConsole(string tmpConsole);
        string getName();
        string getConsole();
    protected:

    private:
        string console;
        string name;
};

Game.cpp is below

#include "Game.h"

Game::Game()
{
}
void Game::setName(string tmpName)
{
    this->name = tmpName;
}

void Game::setConsole(string tmpConsole)
{
    this->console = tmpConsole;
}
string Game::getName()
{
    return this->name;
}
string Game::getConsole()
{
    return this->console;
}
Game::~Game()
{
}

Book.h is below

#ifndef BOOK_H
#define BOOK_H
#include <iostream>

class Book
{
    public:
        Book();
        virtual ~Book();
        string getName();
        void setName(string tmpName);
        int getPage();
        void setPage(int tmpPage);
    protected:

    private:
        int pageNumber;
        string name;
};

Book.cpp is below

#include "Book.h"

Book::Book()
{
    //ctor
}
void Book::setName(string tmpName)
{
    this->name = tmpName;
}
void Book::setPage(string tmpPage)
{
    this->pageNumber = tmpPage;
}
string Book::getName()
{
    return this->name;
}
int Book::getPage()
{
    return this->pageNumber
}
Book::~Book()
{
    //dtor
}

I'm trying to create a database that contains multiple platforms such as books and games. This database will have to retain each of the two classes individual traits such as having a page number (BOOK) or having a console (GAME).

I originally thought of using multiple vectors of each object type. for example vector<Game> and vector<Book>.

#include <iostream>
#include <vector>
#include "Game.h"
#include "Book.h"
using namespace std;

int main()
{
    Book tmpBook;
    tmpBook.setName("Lord Of The Rings");
    tmpBook.setPage(564);
    Game tmpGame;
    tmpGame.setName("CSGO");
    tmpGame.getConsole("PC");
    vector<Game> games;
    vector<Book> books;
    books.push_back(tmpBook);
    games.push_back(tmpGame);
    for(int x = 0; x < books.size(); x++){
         cout << books.getName() << "     " << books.getPage() << endl;
    }
    for(int x = 0; x < games.size(); x++){
         cout << games.getName() << "     " << games.getConsole() << endl;
    }
    return 0;
}

When searching through the database, which would basically just contain those vectors. I realized that this entire program had alot of hard coding. If i wanted to add another platform like TvShow then i would have to create a vector<tvShow> then create and modify more functions to support tvShow.

I'm curious if there was someway for me to support those vectors inside another vector(Which would create a database). The reason i want to do that is so that I can use nested for(;;) loops to iterate through every object that has been created with the program.

Some way for me to be able to encompass all of those vectors neatly.

Maybe I have to use some inheritance or something elaborate contraption that i don't know existed. (I'm a novice/beginner). I just want this project to be scale-able later, because i plan to actually use this program in my day to day life while also developing it.

Aucun commentaire:

Enregistrer un commentaire