0

In the second class I want to only add numbers to string, and I am getting the error "main.cpp:38:19: error: ‘virtual void NumericInput::add(char)’ is private within this context 38 | input->add('1');' for every time I class add for Numeric object. What did I do wrong here, isn't everything already public? Thank you!!

#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
using namespace std;

class TextInput
{
    public:
    string s="";
    
    virtual void add(char c)
    {
        s+=c;
    }
    string getValue()
    {
        return s;
    }
};



class NumericInput : public TextInput
{
    //modified
    void add(char c) 
    {
        if(isdigit(c))
        {
            s+=c;
        }
    }
};

int main()
{
    NumericInput* input = new  NumericInput();
    input->add('1');
    input->add('a');
    input->add('0');
    cout<<input->getValue();
}
Stella
  • 1
  • 3
  • Either make your `NumericInput` a struct or make your method public – lakeweb Dec 11 '21 at 17:05
  • _"isn't everything already public?"_ The keyword `class` makes every member `private` unless explicitly designated otherwise. If you instead used `struct`, members would default to `public`. [C/C++ Struct vs Class](https://stackoverflow.com/questions/2750270/c-c-struct-vs-class) – Drew Dormann Dec 11 '21 at 17:07
  • BTW, the `std::sting` class initializes to the empty string; so you don't need to. – Thomas Matthews Dec 11 '21 at 18:02

2 Answers2

1

All declarations in a class are private by default if there's no public access specifier. You should also override your virtual function in NumericInput.

Andreas
  • 31
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 11 '21 at 23:57
0

Add public before in NumericInput

class NumericInput : public TextInput
{
public:
    //modified
    void add(char c) 
    {
        if(isdigit(c))
        {
            s+=c;
        }
    }
};
  • hey! It would be super helpful for Stella (and future readers) if you explained what that does! – Marcus Müller Dec 11 '21 at 17:05
  • Thank you so much!!!! For future reference, do I have to make all classes public to be able to call their methods in main like that? – Stella Dec 11 '21 at 17:06
  • 1
    Yes. I actually always use `struct`. With time that extra `public:` line gets in the way. The only difference between struct and class is exactly the default scope. –  Dec 11 '21 at 17:10
  • @Stella you don't have to make all classes public, and always using `struct` is a bad idea. *Visibility* (which is what `public:`, `protected:` and `private:` control) are important tools to keep your code maintainable: by controlling which parts of a classes internals are visible to its users and its children, you can set what the *API* is, and that is very important when making changes to one part of a program without having to change all other parts (because when nothing in the visible parts change, you don't have to change anything that uses it). Please don't just generally use `struct`! – Marcus Müller Dec 11 '21 at 17:13
  • @Jellyboy that's why, when you look at any good corpus of C++ code that's more than a handful of classes, the developers take care to make as little `public` as possible (and `struct`s are usually only used if you need classes used as pure data containers without inheritance) – Marcus Müller Dec 11 '21 at 17:15
  • @MarcusMüller That is only your style. struct is part of standard C++. –  Dec 11 '21 at 17:21
  • @Jellyboy yes, it's in standard C++; but only using struct as advice to a beginner is simply not a good idea; honestly, C++ is complicated in many ways, but visibility is not that complicated, but very useful. – Marcus Müller Dec 11 '21 at 18:30
  • @MarcusMüller I'm sort of an iconoclast. I like to challenge long held ideas and "do not use struct" is one of them. I have written an article pondering the pros and cons and although a full replacement of `class` with `struct` is out of question, the line can be drawn at a different place. In many cases the simplicity of C structs, as opposed to the bloat brought by `class` boilerplate with getters and setters, outweights the drawbacks. –  Dec 11 '21 at 18:59