简体   繁体   中英

Inheritance/Polymorphism - Am I forced to use "protected" variables?

I am currently working with Big C++ 2nd Edition, using Code::Blocks 17.12, on the chapter for inheritance

The book introduces the protected variable type for cases in which you want to allow a derived class to access said variable.

The book also forewarns that protected elements suffer from some of the pitfalls of a public variable: in the worst cases derived class members can corrupt the base class data

The book demos the use of a protected member in a clock program in a dedicated section in which they introduce this, but in the final code they ultimately went with setting the variable to private and then using some get_ helper functions to access this private data. This combo of private and using helper functions always returned errors in my IDE and I wasn't able to pull that off

Take this simple example I created for P8.1, a employee record for a programmer, with an employee base class and programmer derived class. I created the following ctor, with the variables name and sal set to protected status in the base class

Programmer::Programmer(string p_name, double p_sal)
    :Employee(get_name(), get_sal())
{
    name=p_name;
    sal=p_sal;
}

With this code, the program works perfectly.

Based on the textbook, if the variables name and sal were set to private status in the base, then I should be able to execute the code also, granted that I am using a get_ helper function I created to accessed the data.

Can anyone explain what the issue is here? Should I be ok with using protected or is there truly a way to keep all of my variables private for classes?

I also found this on https://docs.microsoft.com/en-us/cpp/cpp/protected-cpp?view=vs-2019

Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.

I have not covered static so far, So I ultimately tried a bunch of different combinations with pointers and references, none of which worked either.

I am trying to understand when to use protected vs when to use private basically and the book isn't being clear on that. Any ideas?

For some reason I thought when calling the base class constructor you needed to provide helper functions to access the private data.

Since the base ctor is already public, and by feeding it the parameters for the derived class ctor it will construct the derived class obj properly, allowing get_ func's to read its private variables

Programmer::Programmer(string p_name, double p_sal)
    :Employee(p_name, p_sal)
{}

I wasn't understanding this in the book, thank you all for helping clarify

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM