Chapter 5 Data Hiding and Member Functions

来源:互联网 发布:软件测试论坛 编辑:程序博客网 时间:2024/05/19 16:27
Chapter 5 Data Hiding and
Member Functions
itscerobnumMFn
• 5.1 Member Functions
• 5.2 Access: Private
and Public
• 5.3 Classes4
• 5.4 Class scope
• 5.4.1 Scope
Resolution Operator : :
• 5.4.2 Nested Classes
• 5.5 Example:
Revisiting the Flush
• 5.6 static Member
• 5.7 The this Pointer
• 5.8 static and const
Member Functions
• 5.8.1 Mutable
• 5.9 Containers and
Item Access
• 5.10 Pragmatics
• Summary
• Exercises
Chapter 5 Data Hiding and
Member Functions
itscerobnumMFn
• The original name given by Stroustrup to
his language was "C with classes."
• A class
c l as s
is an extension of the idea of struct
found in C.
• A class packages a data type with its
associated functions and operators.
• In C++,
C + ,
structures may have member
functions, and also may have parts of their
description hidden.
Chapter 5 Data Hiding and
Member Functions
itscerobnumMFn
• We will use the terms client
c l i e n t
to mean a user
of an ADT, and manufacturer
m an u f ac t u r e r
to mean the
provider of the ADT.
– black box
– user interface
– internal implementation
Chapter 5 Data Hiding and
Member Functions
itscerobnumMFn
• OOP needs a data hiding mechanism. In
C++ this is supported by the keyword class
and the associated access keywords pub1ic,
private, and protected. The private parts are
hidden from client code, and the public
parts are available. It is possible to change
the hidden representation, but not to change
the public access or functionality.
5.1 Member Functions
• The concept of struct is augmented in C++
to allow functions to be members. The
function declaration is included in the
structure declaration, and is invoked by
using access methods for structure members.
• Demo on page 139: chstac2.h
– The member functions are written much as
other functions. One difference is that they can
use the data member names as is.
5.2 Access: Private and Public
• Member functions that are defined within
the struct are implicitly inline.
• To define a member function outside the
struct, the function is not implicitly inline.
• The scope resolution operator allows
member functions from the various struct
types to have the same names.
• The grouping of operations with data
emphasizes their "objectness." Objects have
a description and behavior.
5.2 Access: Private and Public
5.2 Access: Private and Public
• The concept of s t r u c t is augmented in C++ to
allow pub1 i c and private members.
• Inside a struct, the use of the keyword private
followed by a colon restricts the access to the
members that follow this construct.
• The private members can
be used by only a few
categories of functions, whose privileges include
access to these members. These functions include
the member functions of the struct.
• Demo on page 142: ch_stac3.h
5.2 Access: Private and Public
• Demo on page 143: ch_stac3.cpp
– Hiding data is an important component of OOP.
It allows for more easily debugged and
maintained code because errors and
modifications are localized.
– Client programs need only be aware of the
type's interface specification.
5.3 Classes
• Classes in C++ are introduced by the
keyword class
c l as s
. They are a form of struct
whose default privacy specification is
private. Thus, struct and class can be used
interchangeably with the appropriate access
specifications.
• Demo on page 144: complex2.cpp
5.3 Classes
• Here we are using the fact that the default access
to class
members is private.
• It will be our style to prefer class
to struct
unless
all members are data members with public access.
It will also be our style to use access keywords
explicitly and to place public members first and
private members last.
• We call this the "need-to-know“ style, because
everyone needs to know the public interface, but
only the class provider needs to know the private
implementation details.
5.4 Class scope
• Class adds a new set of scope rules to those
of the kernel language. (See Section
3.8,
"Scope and Storage Class," on page 71.)
One point of classes is to provide an
encapsulation technique
e n c aps u l at i on t e c h n i qu e
.
5.4.1 Scope Resolution Operator : :
• The scope resolution operator is the highest
precedence operator in the language. It
comes in two forms:
• Its unary form is used to uncover or access a
name that has external scope and has been
hidden by local or class scope:
– Demo on page 146: how_many.cpp
5.4.1 Scope Resolution Operator : :
• Its binary form is used to disambiguate
names that are reused within classes, as
shown below. Later we will use this form
with inheritance and namespaces.
5.4.2 Nested Classes
• Classes, like blocks and namespaces, can
nest. These are all scopes and nesting
allows local hiding of names and local
allocation of resources. This is often the
case when a class is needed as part of the
implementation of a larger construct.
• Demo on page 147: nested.cpp
– ::C
– X::C
– X::Y::C
5.4.2 Nested Classes
• Notice that C++ allows you to nest function
definitions by using class nesting. This is a
restricted form of function nesting. The
member functions must be defined inside
the local class, and cannot be referred to
outside this scope.
5.5 Example: Revisiting the Flush
• The poker program in Section 4.6,
"Example: A Flush," on page 119, will be
written using classes to represent the needed
data types and functionality.
• Demo on page poker2.cpp
5.5 Example: Revisiting the Flush
5.5 Example: Revisiting the Flush
5.5 Example: Revisiting the Flush
5.5 Example: Revisiting the Flush
5.6 static Member
• Data members can be declared with the
storage class modifier static.
• A data member that is declared static is
shared by all variables of that class and is
stored in only one place.
• Non-static data members are created for
each instance of the class.
• Without static data members, data that was
required by all instances of a class would
have to be global.
5.6 static Member
• Since a static member is independent of a
particular instance, it can be accessed in the
form
class-name
c l as s - n am e
: : identifier
i de n t i f i e r
5.6 static Member
• A
static member of a global class must be
explicitly declared and defined in file scope.
5.6 static Member
• A
static member of a global class must be
explicitly declared and defined in file scope.
5.6 static Member
• Pointer and dot operator access is
misleading and gives no indication that the
member is static.
• Newly allowed is static const initialization
within the class declaration:
5.7 The this Pointer
• The keyword this
t h i s
denotes an implicitly
declared self-referential pointer. It can be
used only in a non-static member function.
In a static member function the implicit
arguments are not available.
• Demo on page 152: c_pair.cpp
– The t h i s keyword provides for a built-in selfreferential
pointer.
5.8 static and const Member
Functions
• C++ allows s t a t i c and const member
functions.
• Syntactically, a s t a t i c member function
has the modifier s t a t i c precede the return
type inside the class declaration. A
definition outside the class must not have
this modifier:
5.8 static and const Member
Functions
• Syntactically, a const member function has
the modifier const follow the argument list
inside the class declaration. A definition
outside the class must also have this
modifier:
5.8 static and const Member
Functions
• The const and static member function
implementation can be understood in terms of this
pointer access.
• The implicit arguments can be thought of as a list
of arguments accessible through the this pointer.
In contrast, a static member function does not get
the implicit arguments.
• A
const member function cannot modify its
implicit arguments. Writing out const member
functions and const parameter declarations is
called const-correctness.
c o n s t - c o r e c t n e s .
Const-correctness is an
important aid in writing code.
5.8 static and const Member
Functions
• Demo on page 154: salary.cpp
5.8.1 Mutable
• The keyword mutabl e allows data members
of class variables that have been declared
const to remain modifiable. This reduces the
need to cast away constness using
const_cast<>. This is a relatively new
feature and is not implemented on all C++
compilers.
• Demo on page 156: mutable.cpp
5.9 Containers and Item Access
• Container classes such as stacks and twodimensional
dynamic arrays are very useful data
types.
• Two-Dimensional Dynamic Arrays, on page 126,
can be repackaged as a class and the ability to
access and modify individual elements of these
objects can be added.
• Demo on page 157: twod.cpp
• The member functions, by acting implicitly on an
object, require one less parameter than the
corresponding nonmember function
implementation.
5.9 Containers and Item Access
5.9 Containers and Item Access
• The access order for classes has
traditionally been private first
5.10 Pragmatics
• In the original form of C++,
C + ,
the access
keywords private and protected did not exist.
By default, member access for class was
private; therefore, the private members had
to come first.
• Our style of public first is becoming the
norm.
• Data members should in general be private.
5.10 Pragmatics
• Generally, data is part of an implementation
choice. It should be accessed through public
member functions. Such member functions
are called accessor
ac c e s or
functions.
f u n c t i on s .
• Accessor functions should be declared const.
• In 00 design the public members are usually
functions, and are thought of as the type's
interface.
i n t e r f ac e .
Summary
• 1.
The original name given by Stroustrup to
his language was "C with classes."
• 2. The structure type allows the programmer
to aggregate components into a single
named variable.
• 3.
The concept of struct
is augmented in
C++ to allow functions to be members.
• 4. Member functions that are defined within
the struct
are implicitly inline.
Summary
• 5. The scope resolution operator allows
member functions from different struct
• types to have the same names.
• 6. A
struct
in C++ allows data hiding.
Inside a struct
,
the keyword private
followed by a colon restricts the access of
the members that follow this construct. The
private
members are used by the struct's
member functions.
Summary
• 7. Classes in C++ are a form of struct
whose
default access specification is private.
Thus
struct
and cl
ass
can be used interchangeably with
the appropriate access specification.
• 8.
Data members can be declared with the storage
class modifier static .
A data member that is
declared static
is shared by all variables of that
class and is stored in one place only.
• 9. Classes can be nested. The inner class is inside
the scope of the outer class. This is not in accord
with C semantics. 
原创粉丝点击