Class Scope and Accessing Class Members
A class's data members (variables declared in the class definition) and
member functions (functions declared in the class definition) belong to
that class's scope. Nonmember functions are defined at file scope.
Within a class's scope, class members are immediately accessible by all
of that class's member functions and can be referenced by name. Outside
a class's scope, public class members are referenced through one of
the handles on an object an object name, a reference to an object or a
pointer to an object. The type of the object, reference or pointer specifies
the interface (i.e., the member functions) accessible
Variables declared in a member function have block scope and are
known only to that function. If a member function defines a variable
with the same name as a variable with class scope, the class-scope
variable is hidden by the block-scope variable in the block scope. Such a
hidden variable can be accessed by preceding the variable name with the
class name followed by the scope resolution operator (::). Hidden global
variables can be accessed with the unary scope resolution operator
The dot member selection operator (.) is preceded by an object's name or
with a reference to an object to access the object's members.
The arrow member selection operator (->) is preceded by a pointer to an
object to access the object's members
Accessing a data member depends solely on the access control of
that data member. If its public, then the data member can be easily
accessed using the direct member access (.) operator with the object
of that class.
If, the data member is defined as private or protected, then we
cannot access the data variables directly. Then we will have to
create special public member functions to access, use or initialize the
private and protected data members. These member functions are
alsocalled Accessors and Mutator methodsor getter and setter functi
ons.
1.)Accessing Public Data Members
Following is an example to show you how to initialize and use the public
data members using the dot (.) operator and the respective object of
class.
2.)Accessing Private Data Members
To access, use and initialize the private data member you need to create
getter and setter functions, to get and set the value of the data member.
3.)Accessing Protected Data Members
Protected data members, can be accessed directly using dot (.) operator
inside the subclass of the current class, for non-subclass we will have to
follow the steps same as to access private data member.