Ran across this question today:write a class Tool which will have a function void type() that every  derived class should implement . A function Action() that every  derived class can override . function init() which is available to  only Tool and variable Name which will tell which class`s instance is  this objectHere is my solution (based on other solutions I've found):#include <typeinfo>  class Tool{      public:           string name;           Tool() {                 name = typeid(*this).name();            };           virtual void type() = 0;            virtual void Action();         private:           void init();    };Could someone double check this please?
C++ Class Writing Interview Query
c++
I would strongly recommend making name field const. Also, typeid(*this).name() won't look nice and pretty in some compilers.You should probably try something like:class Tool {public:  const std::string name;  <...>protected:  Tool(const char* className) : name(className) {}private:  <...>}BTW, do NOT expect typeid(*this).name(); to be called for deriven object. Even virtual functions won't help, so you can't write something like virtual const char* getMyName() = 0;, because any kind of virtual behavior will not work in constructor.