Wednesday, September 21, 2011

C++ PROTECTED CLASS DATA ACCESS SPECIFIER









REVISED: Sunday, March 3, 2013




CONTENTS:
I.     C++ PROTECTED CLASS DATA ACCESS SPECIFIER INTRODUCTION
II.    C++ ACCESS SPECIFIERS
III.   C++ INHERITANCE
IV.  C++ BINARY SCOPE RESOLUTION OPERATOR ::
V.   C++ PRIVATE VERSUS PROTECTED MEMBERS
VI.  C++ PROTECTED CLASS DATA ACCESS SPECIFIER SUMMARY

YOU WILL LEARN:
1. C++ class data member access specifiers.
2. C++ class data member access rights.
3. C++ guidelines for defining base class data member access rights.
4. C++ private versus protected class data members.
5. To define a C++ base class as a base for other C++ classes.

I. C++ PROTECTED CLASS DATA ACCESS SPECIFIER INTRODUCTION


Welcome to the “C++ Protected Class Data Access Specifier Tutorial.”

II. C++ ACCESS SPECIFIERS


A class data access specifier is one of the following three keywords: private, protected, or public.


Class member functions are functions declared within a class definition. Class data access specifiers determine the data member access rights of base-class and derived-class member functions.


A class is a user defined type, a logical abstraction, and a key concept in C++. By default, functions and data members declared within a class are private to that class. Protected access is an intermediate level of data protection between private and public.

III. C++ INHERITANCE


Inheritance involves an object acquiring the characteristics of another object.

When designing a program, one goal of a programmer is to create a base-class that has the core characteristics required by all of the planned derived-class objects. A derived-class contains characteristics specific to the derived-class and all of the characteristics of the base-class.


A class that is inherited is referred to as a base-class. The class that does the inheriting is called the derived-class. A derived class can be used as a base-class for another derived-class, and so forth, achieving multiple inheritances.


The syntax to establish inheritance is as follows:

class derivedClassName : classDataAccessSpecifier baseClassName


When a class inherits another class the function members and data members of the base class become members of the derived class. The only members of a base class that are not inherited by the derived class are the constructors, destructor, and any member functions overloading the assignment operator.


Base-classes should be designed as a base for other classes. Think of it as defining one class based on another class. The new class you are defining is the derived-class.

A.  The following should be kept in mind when designing base-classes:


1. Private data members of a base-class are accessible only from within other members of the same base-class; from within other members of their friends; and never accessible from within member functions of a derived-class.


It is a generally accepted programming practice to make all data members of a class private and to use non-private get and set functions to manipulate and validate the data members.


2. Public data members are accessed from within their own member functions; accessed from within their derived-class’ member functions, and accessed from within their friends member functions.


3. Protected data members are accessible from within members of their base-class; from within members of the friends of their base-class; and from within members and friends of any classes derived from that base-class.


Once an access specifier has been used, it remains in effect until either another access specifier is encountered or the end of the class declaration is reached. It is a generally accepted programming practice to have only one private, public, and protected section within each class. However, you can change access specifiers as often as you like.

IV. C++ BINARY SCOPE RESOLUTION OPERATOR ::


The binary scope resolution operator is :: two colons. The binary scope resolution operator :: defines the class and scope of a member function in the same manner as if it had originally been included within the class definition.


The syntax for the binary scope resolution operator is as follows:

className::memberName


Base-class members are accessed in derived-classes by qualifying their names with the base-class name and the :: binary scope resolution operator.


V. C++ PRIVATE VERSUS PROTECTED MEMBERS


A protected access specifier is normally only considered when a program contains inheritance. Inheritance is a major piece of object oriented programming (OOP) because it helps the programmer design hierarchical classifications.


A.  Two problems are created when protected data members are used.


1. Base-class protected data members can be assigned invalid values by derived-class objects because the derived-class object does not have to use a set function to change the value of the base-class’s protected data.


2. Derived-class member functions that depend on base-class implementation are fragile, or brittle, because a small change in the base class can break derived class implementation requiring the modification of all derived classes.

VI. C++ PROTECTED CLASS DATA ACCESS SPECIFIER SUMMARY


Avoid using protected data members in a base class.

When possible make all data members of a class private and use non-private set and get functions to manipulate and validate the data members. 

YOU HAVE LEARNED:
1. C++ class data member access specifiers.
2. C++ class data member access rights.
3. C++ guidelines for defining base class data member access rights.
4. The difference between C++ private versus protected class data members.
5. How to use C++ to define a base class as a base for other classes.


Elcric Otto Circle


<

-->


-->


-->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment