I study the topic of inheritance in the 2010 textbook workshop. There is an example that I took to study the issue of inheritance, where the base abstract class is used. I created an object from the abstract for the inherited class (correct if it is not right) and it fails me, the compiler stubbornly stands on the fact that there are errors.
Base class AString.h
#pragma once #ifndef ASTRING_H #define ASTRING_H #include<string> class AString { public: virtual ~AString() {} virtual const std::string& GetName() = 0; virtual const std::string& GetVal()const = 0; virtual int GetSize()const = 0; }; Inherited SymbString.h
#pragma once #include <string> #include "AString.h" class SymbString :public AString { public: SymbString(std::string _name) :name(_name) {} SymbString(std::string _name, std::string _val) :name(_name), val(_val) {} const std::string& GetName()const { return name; } const std::string& GetVal()const { return val; } int GetSize()const { return val.size(); } private: std::string name, val; }; And the file where you can create objects as much as the user wants:
.... AString* pNewObj; switch (item) { case 1: pNewObj = new SymbString(name, value);// тут подчеркивает и пишет "использование //объекта абстрактного типа класса "SymbString" не допускается чисто //виртуальная функция функцию "AString::GetName" не имеет оператора переопределения " break; case 2: if (!IsHexStrVal(value)) { cout << "Error!" << endl; return; } pNewObj = new HexString(name, value);// выводит тоже самое только для этого случая break; } What the compiler wrote ("using an abstract type object of the" SymbString "class does not allow the purely virtual function" AString::GetName "does not have an override operator") causes misunderstanding what they want from me. Is it SymbString for SymbString and HexString to create objects?
I rewrote the code word by word from the textbook.
It is also not clear what the first const before string in the next line means: virtual const std::string& GetVal()const = 0; what is his role?