online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/****************************************************************************** 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. Here is the best implementation, relying on if constexpr and a varadic template function *******************************************************************************/ #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; } }; struct Factory { //a counter, mostly to illustrade some side effect of construction static int num_constructed; //factory method template<typename A, typename... Ts> static unique_ptr<Animal> makeAnimal(Ts&&... args) { // this factory is more in line with library factory methos and used the type // of the specialized Animal object to resolve the implementation unique_ptr<Animal> temp(nullptr); if constexpr (is_same_v<A,Cat>) temp.reset(new Cat(std::forward<Ts>(args)...)); else if constexpr (is_same_v<A, Bug>) temp.reset(new Bug(std::forward<Ts>(args)...)); else temp.reset(new UnknownFormOfLife()); num_constructed++; return temp; } }; //initialise the static member variable int Factory::num_constructed=0; int main () { vector < shared_ptr < Animal >> animals; animals.push_back(Factory::makeAnimal<Cat>("Charlie","black")); animals.push_back(Factory::makeAnimal<Bug>("Tom","green",8)); animals.push_back(Factory::makeAnimal<void>()); //any other template type for (auto a : animals) a->action(); cout<<"Number of animals produced is "<<Factory::num_constructed<<endl; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue