Class factory to create derived classes c++

1.8k views Asked by At

I'm currently learning about class factory patterns with C++. I keep having errors while trying to implement the factory. Suppose I have an abstract class and two derived classes. What I want the factory to do is to create a new object of the base class like so: Ball *sc = new SoccerBall(); I am not sure on how to implement this, I have tried but of no avail. What do I need to fix?

class Ball
{
   public:
   Ball();
   virtual ~Ball();
   virtual int getSize() const = 0;
   virtual void setBallSize(int s) = 0;
   virtual string ballManufacturer() const = 0;
   protected:
   int ballSize;
}
class Soccerball:public Ball
{
   public:
   Soccerball();
   Soccerball(int size);
   ~Soccerball();
   int getSize() const;
   void setBallSize(int s);
   string ballManufacturer() const;
}
class Soccerball:public Ball
{
   public:
   Soccerball();
   Soccerball(int size);
   ~Soccerball();
   int getSize() const;
   void setBallSize(int s);
   string ballManufacturer() const;
}
class Basketball:public Ball
{
   public:
   Basketball();
   Basketball(int size);
   ~Basketball();
   int getSize() const;
   void setBallSize(int s);
   string ballManufacturer() const;
}
class BallFactory
{
   public:
   Ball* createBall(string s)
   {
       if(s == "Soccer")
       {
          return new Soccerball(5);
       }
       if(s == "Basket")
       {
          return new Basketball(6);
       }
   }
}
1

There are 1 answers

1
Nik On

This how your code will work, but above when you are posting a question you should provide "Short Self Contained Correct Code" and make easy for people to understand your problem easily.

#include <iostream>

using namespace std;

class Ball
{
  public:

    Ball()
    {
      cout<<"Ball ctr"<<endl;
    }

    virtual ~Ball()
    {

    }
    virtual int getSize() const = 0;
    virtual void setBallSize(int s) = 0;
    virtual string ballManufacturer() const = 0;

  protected:
    int ballSize;
};

class Soccerball:public Ball
{
  public:

    Soccerball()
    {
     cout<<"create Default Soccer Ball "<<endl;
    }

    Soccerball(int size)
    {
     cout<<"create Soccer Ball "<<size<<endl;
 }

    ~Soccerball()
    {

    }

    int getSize() const
    {
      return ballSize;
    }

    void setBallSize(int s)
    {
      ballSize = s;
    }

    string ballManufacturer() const
    {
      return "";
    }
};

class Basketball:public Ball
{
  public:

    Basketball()
    {
     cout<<"create default Baseket Ball "<<endl;
    }

    Basketball(int size)
    {
     cout<<"create Baseket Ball "<<size<<endl;
    }

 ~Basketball()
    {

    }

    int getSize() const
    {
      return ballSize;
    }

    void setBallSize(int s)
    {
      ballSize = s;
    }

    string ballManufacturer() const
    {
      return "";
    }
};

class BallFactory
{
  public:
//Factory method
    static Ball* createBall(string s)
    {
      if(s == "Soccer")
      {
        return new Soccerball(5);
      }
      if(s == "Basket")
      {
        return new Basketball(6);
      }
    }
};

int main()
{
  Ball* ptr = BallFactory::createBall("Soccer");

  return 0;
}

But you also need to understand how Factory design pattern works and how a namesake virtual constructor is created and why you would use a parameterized factory. Or could you use a template factory.