Hi
I was hoping someone could tell me if the create function I use in quiz.cpp between lines 27-37 is a factory function? I have googled for what a factory function is and am still a little confused but I think this is correct and just need validation please.
//Quiz.h
#ifndef QUIZ_HPP
#define QUIZ_HPP
#include <iostream>
#include <memory>
usingnamespace std;
enum QUIZ_TYPE {SPORTS, MUSIC};
class Quiz
{
public:
Quiz(){};
virtual ~Quiz(){};
virtualvoid AskQuestion() = 0;
static tr1::shared_ptr<Quiz> Create(QUIZ_TYPE choice);
};
#endif
//Quiz.cpp
#include "Quiz.h"
#include "SportsQuiz.h"
#include "MusicQuiz.h"
tr1::shared_ptr<Quiz> Quiz::Create(QUIZ_TYPE choice)
{
switch(choice)
{
case(SPORTS):
return tr1::shared_ptr<Quiz>(new SportsQuiz);
case(MUSIC):
return tr1::shared_ptr<Quiz>(new MusicQuiz);
// leaving out a default value for now
}
}
//SportsQuiz.h
#ifndef SPORTSQUIZ_HPP
#define SPORTSQUIZ_HPP
#include "Quiz.h"
class SportsQuiz: public Quiz
{
public:
SportsQuiz(){cout<<"Sports Constructor\n";}
~SportsQuiz(){cout<<"Sports Destructor\n";}
void AskQuestion(){cout<<"Ask Sports Question\n";}
};
#endif
//I have a .h file called MusicQuiz that does pretty much the same as SportsQuiz only replaces the word Sports with Music
//Main.cpp
#include "Quiz.h"
#include "SportsQuiz.h"
#include "MusicQuiz.h"
int main()
{
int choice = 0;
while(true)
{
cout<<"Please Select the type of Quiz to take\n";
cout<<"(0)SportsQuiz (1)MusicQuiz (2) Quit: ";
cin>>choice;
if(choice == 2) break;
tr1::shared_ptr<Quiz>spq(Quiz::Create(static_cast<QUIZ_TYPE>(choice)));
spq->AskQuestion();
}
return 0;
}
This code works and outputs the correct text of Ask Sports Question or music depending on what the user selects but is it a factory function or have I got the wrong end of the stick altogether?