O'Reilly logo

C++ For Dummies, 7th Edition by Stephen R. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 14

Protecting Members: Do Not Disturb

In This Chapter

arrow Declaring members protected

arrow Accessing protected members from within the class

arrow Accessing protected members from outside the class

Chapter 12 introduces the concept of the class. That chapter describes the public keyword as though it were part of the class declaration — just something you do. In this chapter, you find out about an alternative to public.

Protecting Members

The members of a class can be marked protected, which makes them inaccessible outside the class. The alternative is to make the members public. Public members are accessible to all.

remember.eps Please understand the term inaccessible in a weak sense. Any programmer can go into the source code, remove the protected keyword, and do whatever she wants. Further, any hacker worth his salt can code into a protected section of code. The protected keyword is designed to protect a programmer from herself by preventing inadvertent access.

Why you need protected members

To understand the role of protected, think about the goals of object-oriented programming:

  • To protect the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required