How to use of Protected Data Member in C++ with example
A protected member can be accessed by member functions in its own class or in any class derived from its own class. It can't be accessed from functions outside these classes, such as main(). This is just what we want. Hence the protected access specifier is used.
You would agree that through the derived class both we have increased the functionality of the increment class without modifying it. This is the power of inheritance.
Simple Example of Protected Data Member in C++
Explanation of the program
We don't want to make count public since that would allow it to be accessed through any function anywhere in the program and thereby eliminate the advantage of data hiding.
© 2016-2019, Codeverb.com - All Rights Reserved