/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
Simple factory based on make_unique
*******************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class Animal
{
public:
Animal () {}
Animal (string name):_name (name) {}
virtual ~ Animal () {}
virtual void action () = 0;
string name () { return _name; }
private:
string _name = {"anonymous"};
};
class Cat:public Animal
{
public:
//constructor with two arguments
Cat (const string & name, const string & color)
:Animal (name), _color (color) {}
void action ()
{
cout << "I am a cat, my name is " << name ()
<< " and my fur color is " << _color << endl;
}
private:
string _color = {"undefined"};
};
class Bug:public Animal
{
public:
//constructor with three arguments
Bug (const string & name, const string & color, int legs)
:Animal (name), _color (color), _legs (legs) {}
void action ()
{
cout << "I am a bug, my name is " << name ()
<< " and my skin color is " << _color
<< " and I jump on " << _legs << " legs"<<endl;
}
private:
string _color = {"undefined"};
int _legs = { 0 };
};
class UnknownFormOfLife:public Animal
{
public:
void action ()
{
cout << "I am an unknown form of life" << endl;
}
};
int main ()
{
vector < shared_ptr < Animal >> animals;
animals.push_back(make_unique<Cat>("Charlie","black"));
animals.push_back(make_unique<Bug>("Tom","green",8));
animals.push_back(make_unique<UnknownFormOfLife>());
for (auto a : animals)
a->action();
}