-3

I have a problem with some task. I need to write an derived class in which I need to be sure, that vector FVect cointains only characters <'a'; 'z'>.

class Something {
private:
   char FVect[3];

protected:
   virtual void setValue(int _idx, char _val) { FVect[_idx] = _val; }

public:
   Something() {};
};

I don't know exactly how to write a method in derived class (without making changes in class Something) since FVect is private.

Thanks for help.

  • Use `setValue` in the derived class? – Holt Apr 19 '18 at 14:38
  • Are you asking how to derive another class from `class Something` or are you asking how to ensure that `FVect` only contains characters from 'a' to 'z'? Your question is too un clear and too broad. Please elaborate and [edit]. – Jabberwocky Apr 19 '18 at 14:38
  • The term is *derived* class, not derivative. – Sebastian Redl Apr 19 '18 at 14:40
  • Thanks for answers. My task sounds exactly like this: "Write a definition of a class derived to 'Something' in which we are sure, that every element of vector FVect contains only <'a'; 'z'> character" – Karol Siegieda Apr 19 '18 at 15:05

1 Answers1

3

With your current setup, the only thing you can do is implement setValue() in the class derived from Something and raise an exception if _val is outside the valid values, otherwise calling the base class method if it is valid:

class Derived : public Something {
    ...
    void setValue(int _idx, char _val) {
        if ((_val < 'a') || (_val > 'z')) throw std::invalid_argument( "invalid character" );
        Something::setValue(_idx, _val);
    }
    ...
};
Marco Pantaleoni
  • 2,529
  • 15
  • 14