Classes and Data Abstraction
Chapter
06
Introduction
n
OOP encapsulates data (attributes) and
functions (behavior) into packages called classes.
n
The data components of a class are called data
members and the function components of a class are called member functions (or
methods).
n
A class is like a blueprint. Out of class, a
programmer can create objects.
n
An instance of a user-defined class is called
an object.
Structure Definitions
n
Structures are aggregate data types built using elements of other types.
n
struct Time
{
int hour;
int minute;
int second;
};
Structure Definitions
n
The keyword struct introduces the structure definition.
n
The identifier Time is the structure tag that names the structure definition
and is used to declare variables of the structure type.
n
Time timeObject, timeArray[10], *timePtr, &timeRef = timeObject;
Accessing Members of Structures
n
Members of a structure (or a class) are
accessed using the member access operators—the dot operator (.) and the
arrow operator (->).
n
cout << timeObject.hour;
n
cout << timeRef.hour;
n
timePtr = &timeObject;
n
cout << timePtr->hour;
n
The expression timePtr->hour is equivalent
to (*timePtr).hour.
Implementing a User-defined Type Time With a Struct
n
struct Time
{
int hour;
int minute;
int second;
};
n
void printMilitary(const Time &t);
n
void printStandard(const Time &t);
n
… main function
n
… printMilitary function definition
n
… printStandard function definition
Implementing a Time Abstract Data Type with a Class
n
class CTime
{
public:
CTime(void);
void setTime(int h,
int m, int s);
void
printMilitary(void);
void
printStandard(void);
private:
int hour;
int minute;
int second;
};
Member access specifiers
n
Any data member or member function declared
after member access specifier public is accessible wherever the program
has access to an object of class CTime.
n
Any data member or member function declared
after member access specifier private is accessible only to member
functions and friends of the class CTime.
n
Member access specifiers are always followed by
a colon (:) and can appear multiple times and in any order in a class
definition.
Class Scope and Accessing Class Members
n
A class’s data members and member functions
belong to that class’s scope.
n
Nonmember functions are defined as file scope.
n
Within a class’s scope, class members are
immediately accessible by all of that class’s member functions and can be
referenced by name.
n
Outside a class’s scope, 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.
n
The operators used to access class members are:
n
dot member selection operator (.)
n
arrow member selection operator (->)
Separating Interface from Implementation
n
One of the fundamental principals of good
software engineering is to separate interface from implementation.
n
This makes it easier to modify programs.
n
As far as clients of a class are concerned,
changes in the class’s implementation do not affect the client as long as
the class’s interference originally provided to the client is unchanged.
Separating Interface from Implementation
n
Place the class declaration in a header file to
be included by any client that wants to use the class.
n
This forms the class’s public interface (and
provides the client with the function prototypes it needs to be able to call
the class’s member functions).
n
Place the definitions of the class member
functions in a source file.
n
This forms the implementation of the class.
Controlling Access to Members
n
The member access specifiers public, private
and protected are used to control access to a class’s data members
and member functions.
n
The labels public, private, and protected
may be repeated, but such usage is rare and can be confusing.
n
A class’s private members can be
accessed only by member functions and friends of that class.
n
The public members of a class may be
accessed by any function in the program.
Controlling Access to Members
n
The primary purpose of public members is
to present to the class’s clients a view of the services (behaviors) the
class provides.
n
This set of services forms the public
interface of the class.
n
The private members of a class as well
as the definitions of its public member functions are not accessible to
the clients of a class.
n
These components form the implementation of the
class.
Access Functions and Utility Functions
n
Not all member functions need be made public to serve as part of the
interface of a class.
n
Some member functions remain private and serve as utility functions to
the other functions of the class.
Initializing Class Objects: Constructors
n
When a class object is created, its members can
be initialized by that class’s constructor function.
n
A constructor is a class member function
with the same name as the class.
n
The programmer provides the constructor
which is then invoked automatically each time as object of that class is
created (instantiated).
n
Constructors
may be overloaded to provide a variety of means for initialized objects of a
class.
Using Default Arguments with Constructors
n
Constructors can contain default arguments.
n
By providing default arguments to the constructor, even if no values are
provided in a constructor call, the object is still guaranteed to be
initialized to a consistent state due to the default arguments.
Using Default Arguments with Constructors
n
A programmer-supplied constructor that defaults all its arguments is also a default
constructor, i.e., a constructor that can be invoked with no arguments.
n
There can be only one default constructor per class.
n
Declare default function argument values only in the function prototype.
Using Destructors
n
A destructor is a special member
function of a class.
n
The name of the destructor for a class
is the tilde (~) character followed by the class name.
n
A class’s destructor is called when an
object is destroyed–e.g., when program execution leaves the scope in which
an object of that class was instantiated.
Using Destructors
n
The destructor itself does not actually
destroy the object–it performs termination housekeeping before the system
reclaims the object’s memory so that memory may be reused to hold new
objects.
n
A destructor receives no parameters and
returns no value.
n
A class may have only one destructor –
destructor overloading is not allowed.
When Constructors and Destructors are Called
n
Constructors and destructors are called
automatically.
n
The order in which these function calls are
made depends on the order in which execution enters and leaves the scope in
which objects are instantiated.
n
Generally, destructor calls are made in the
reverse order of the constructor calls.
When Constructors and Destructors are Called
n
Constructors are called for objects defined in
global scope before any other function in that file begins execution.
n
The corresponding destructors are called when
main terminates or the exit function is called.
n
Constructors are called for automatic local
objects when execution reaches the point where the objects are defined.
n
Corresponding
destructors are called when the objects leave scope.
When Constructors and Destructors are Called
n
Constructors are called for static local objects only once when
execution reaches the point where the objects are defined.
n
Corresponding destructors are
called when main terminates or the exit function is called.
Using Data Members and Member Functions
n
A class’s private data members can be
manipulated only by member functions (and friends) of the class.
n
Classes often provide public member
functions to allow clients of the class to set (i.e., write) or get (i.e.,
read) the values of private data members.
n
If a data member is public, then the
data member may be read or written at will by any function in the program.
n
A reference to an object is an alias for the name of the object and hence may
be used on the left side of an assignment statement.
n
In this context, the reference makes a perfectly acceptable lvalue that can
receive a value.
A Subtle Trap: Returning a
Reference to a Private Data Member
n
One way to use this capability (unfortunately!)
is to have public member function of a class return a non-const reference to a
private data member of that class.
n
Never have a public member function return a
non-const reference (or a pointer) to a private data member.
n
Returning such a reference violates the
encapsulation of the class.
Assignment by Default Memberwise Copy
n
The assignment operate (=) can be used to assign an object to another object
of the same type.
n
Such assignment is by default performed by memberwise copy—each member of
one object is copied individually to the same member in another object.
Software Reusability
n
People who write object-oriented programs
concentrate on implementing useful classes.
n
There is a tremendous opportunity to capture
and catalog classes so that they can be accessed by large segments of the
programming community.
n
Many class libraries exist and others are being
developed worldwide.
Software Reusability
n
Significant problems must be solved, however,
before the full potential of software reusability can be realized.
n
We need:
n
cataloging schemes,
n
licensing schemes,
n
protection mechanisms to ensure that master copies of classes are not corrupted,
n
description schemes so that designers of new systems can determine if existing objects meet
their needs,
n
browsing mechanisms to determine what classes are available and how closely they meet
software developer requirements, and the like.
Problem 6.12 – Project 01
n
Create a class Rectangle. The class has attributes length and width, each of which
defaults to 1. It has member
functions that calculate the perimeter and the area of the rectangle.
It has set and get functions for both length and width.
The set functions should verify that length and width are each
floating-point numbers larger than 0.0 and less than 20.0.
|