You cannot initialize members that are not a direct member of your class. n not a direct member of deriv ; it is a direct member of base .
However, n is accessible to deriv , so you can always assign it in your deriv constructor, but you really have to initialize it in the base constructor.
In addition, you cannot have virtual constructors. Did you mean to use virtual destructors?
class base { public: base() : n(0) {}
EDIT (response to OP editing): You do not need virtual methods for this:
class CarPart { public: CarPart(const char* newName, int newPrice) : name(newName), price(newPrice) {} const std::string& GetName() const { return name; } int GetPrice() const { return price; } private: std::string name; int price; }; class Tire : public CarPart { public: Tire() : CarPart("Tire", 50) {} };
Assuming CarPart should be a name and price for all of your CarPart , that should be more than enough.
source share