Page 1 of 1

C++ PROTECTED CLASS DATA ACCESS SPECIFIER TUTORIAL C++ PROTECTED CLASS DATA ACCESS SPECIFIER TUTORIAL Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 21 August 2009 - 12:44 AM

C++ PROTECTED CLASS DATA ACCESS SPECIFIER TUTORIAL



WHAT YOU WILL LEARN IN THIS TUTORIAL:
1. You will learn about class data member access specifiers.
2. You will learn about class data member access rights.
3. You will learn guidelines for defining base class data member access rights.
4. You will learn about private versus protected class data members.
5. You will learn to define a base class as a base for other classes.

• I. INTRODUCTION

Hello; nice to meet you! Welcome to the “C++ Protected Class Data Access Specifier Tutorial.”

• II. ACCESS SPECIFIER

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.

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.

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.

• III. 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.

• IV. 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.

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.

• V. 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.

Is This A Good Question/Topic? 2
  • +

Page 1 of 1