C - Programming - C - Program - Chapter 12.pdf
(
922 KB
)
Pobierz
CHAPTER
12
C
LASSES AND
D
ATA
A
BSTRACTION
IN THIS CHAPTER, YOU WILL:
n
Learn about classes
n
Learn about
private
,
protected
, and
public
members of
a class
n
Explore how classes are implemented
n
Examine constructors and destructors
n
Learn about the abstract data type (ADT)
n
Explore how classes are used to implement ADTs
n
Learn about information hiding
n
Explore how information hiding is implemented in C++
n
Learn about the
static
members of a class
642
|
Chapter 12: Classes and Data Abstraction
In Chapter 11, you learned how to group data items that are of different types by using a
struct
. The definition of a
struct
given in Chapter 11 is similar to the definition of a
C
-
struct
. However, the members of a C++
struct
can be data items as well as functions.
C++ provides another structured data type, called a class, which is specifically designed to
group data and functions. This chapter first introduces classes and explains how to use them,
and then discusses the similarities and differences between a
struct
and a
class
.
Chapter 11 is not a prerequisite for this chapter. In fact, a
struct
and a
class
have similar
capabilities, as discussed in the section ‘‘A
struct
versus a
class
’’ in this chapter.
Classes
Chapter 1 introduced the problem-solving methodology called object-oriented design
(OOD). In OOD, the first step is to identify the components, called objects. An object
combines data and the operations on that data in a single unit. In C++, the mechanism
that allows you to combine data and the operations on that data in a single unit is called a
class. Now that you know how to store and manipulate data in computer memory and
how to construct your own functions, you are ready to learn how objects are constructed.
This and subsequent chapters develop and implement programs using OOD. This chapter
first explains how to define a class and use it in a program.
A class is a collection of a fixed number of components. The components of a class are
called the members of the class.
The general syntax for defining a class is:
class
classIdentifier
{
classMembersList
};
where
classMembersList
consists of variable declarations and/or functions. That is, a
member of a class can be either a variable (to store data) or a function.
If a member of a class is a variable, you declare it just like any other
variable. Also, in the definition of the class, you cannot initialize a
variable when you declare it.
If a member of a class is a function, you typically use the function
prototype to declare that member.
If a member of a class is a function, it can (directly) access any member of the
class—member variables and member functions. That is, when you write
the definition of a member function, you can directly access any member
variable of the class without passing it as a parameter. The only obvious
condition is that you must declare an identifier before you can use it.
Classes
|
643
In C++,
class
is a reserved word, and it defines only a data type; no memory is
allocated. It announces the declaration of a class. Moreover, note the semicolon (
;
) after
the right brace. The semicolon is part of the syntax. A missing semicolon, therefore, will
result in a syntax error.
The members of a
class
are classified into three categories:
private
,
public
, and
protected
. This chapter mainly discusses the first two types,
private
and
public
.
In C++,
private
,
protected
, and
public
are reserved words and are called member
access specifiers.
Following are some facts about
public
and
private
members of a class:
By default, all members of a class are
private
.
If a member of a class is
private
, you cannot access it outside the class.
(Example 12-1 illustrates this concept.)
A
public
member is accessible outside the
class
. (Example 12-1
illustrates this concept.)
To make a member of a class
public
, you use the member access
specifier
public
with a colon, :.
Suppose that we want to define a class to implement the time of day in a program.
Because a clock gives the time of day, let us call this
class
clockType
. Furthermore, to
represent time in computer memory, we use three
int
variables: one to represent the
hours, one to represent the minutes, and one to represent the seconds.
Suppose these three variables are:
int
hr;
int
min;
int
sec;
We also want to perform the following operations on the time:
1. Set the time.
2. Retrieve the time.
3. Print the time.
4. Increment the time by one second.
5. Increment the time by one minute.
6. Increment the time by one hour.
7. Compare the two times for equality.
To implement these seven operations, we will write seven functions—
setTime
,
getTime
,
printTime
,
incrementSeconds
,
incrementMinutes
,
incrementHours
,and
equalTime
.
From this discussion, it is clear that the
class
clockType
has 10 members: three
member variables and seven member functions.
1
2
644
|
Chapter 12: Classes and Data Abstraction
Some members of the
class
clockType
will be
private
; others will be
public
.
Deciding which member to make
public
and which to make
private
depends on the
nature of the member. The general rule is that any member that needs to be accessed
outside the class is declared
public
; any member that should not be accessed directly by
the user should be declared
private
. For example, the user should be able to set the
time and print the time. Therefore, the members that set the time and print the time
should be declared
public
.
Similarly, the members to increment the time and compare the time for equality should
be declared
public
. On the other hand, to prevent the direct manipulation of the
member variables
hr
,
min
, and
sec
, we will declare them
private
. Furthermore, note
that if the user has direct access to the member variables, member functions such as
setTime
are not needed. The second part of this chapter (beginning with the section
‘‘Information Hiding’’) explains why some members need to be
public
and others
should be
private
.
The following statements define the
class
clockType
:
class
clockType
{
public
:
void
setTime(
int
,
int
,
int
);
void
getTime(
int
&,
int
&,
int
&)
const
;
void
printTime()
const
;
void
incrementSeconds();
void
incrementMinutes();
void
incrementHours();
bool
equalTime(
const
clockType&)
const
;
private
:
int
hr;
int
min;
int
sec;
};
In this definition:
The
class
clockType
has seven member functions:
setTime
,
getTime
,
printTime
,
incrementSeconds
,
incrementMinutes
,
incrementHours
, and
equalTime
. It has three member variables:
hr
,
min
, and
sec
.
The three member variables—
hr
,
min
, and
sec
—are
private
to the
class and cannot be accessed outside the class. (Example 12-1 illustrates
this concept.)
The seven member functions—
setTime
,
getTime
,
printTime
,
incrementSeconds
,
incrementMinutes
,
incrementHours
, and
equalTime
—can directly access the member variables (
hr
,
min
, and
sec
). In other words, when we write the definitions of these functions,
Classes
|
645
we do not pass these member variables as parameters to the member
functions.
In the function
equalTime
, the formal parameter is a constant reference
parameter. That is, in a call to the function
equalTime
, the formal para-
meter receives the address of the actual parameter, but the formal parameter
cannot modify the value of the actual parameter. You could have declared
the formal parameter as a value parameter, but that would require the formal
parameter to copy the value of the actual parameter, which could result in
poor performance. (See the section ‘‘Reference Parameters and Class
Objects (Variables)’’ in this chapter for an explanation.)
The word
const
at the end of the member functions
getTime
,
printTime
, and
equalTime
specifies that these functions cannot
modify the member variables of a variable of type
clockType
.
The
private
and
public
members can appear in any order. If you want, you can declare
the
private
members first and then declare the
public
ones. The section ‘‘Order of
public
and
private
Members of a Class’’ in this chapter discusses this issue.
In the definition of the
class
clockType
, all member variables are
private
and all
member functions are
public
. However, a member function can also be
private
. For
example, if a member function is used only to implement other member functions of the
class, and the user does not need to access this function, you make it
private
.
Similarly, a member variable of a class can also be
public
.
Note that we have not yet written the definitions of the member functions of the class.
You will learn how to write them shortly.
The function
setTime
sets the three member variables—
hr
,
min
,and
sec
—toagivenvalue.
The given values are passed as parameters to the function
setTime
. The function
printTime
prints the time, that is, the values of
hr
,
min
,and
sec
. The function
incrementSeconds
increments the time by one second, the function
incrementMinutes
increments the time
by one minute, the function
incrementHours
increments the time by one hour, and the
function
equalTime
compares two times for equality.
Note that the function
equalTime
has only one parameter, although you need two
things to make a comparison. We will explain this point with the help of an example in
the section ‘‘Implementation of Member Functions,’’ later in this chapter.
1
2
Unified Modeling Language Class Diagrams
A class and its members can be described graphically using a notation known as the
Unified Modeling Language (UML) notation. For example, Figure 12-1 shows the
UML class diagram of the
class
clockType
.
Plik z chomika:
Januszek66
Inne pliki z tego folderu:
Back Seat_ A Mumbai Tale - Aditya Kripalani.mobi
(755 KB)
Brief Wondrous Life of Oscar Wao, The - Junot Diaz.opf
(3 KB)
Don't Make Me Think, Revisited_ - Steve Krug.mobi
(9256 KB)
M. T. Anderson - Norumbegan 03 - The Empire of Gut and Bone # (v5.0).epub
(2209 KB)
M. T. Anderson - Norumbegan 02 - The Suburb Beyond the Stars # (v5.0).epub
(2105 KB)
Inne foldery tego chomika:
Dokumenty
Galeria
LUDLUM ROBERT
Midi - Kar
Mszał Rzymski PL
Zgłoś jeśli
naruszono regulamin