Chapter 10 Programming I
Chapter 10 Programming I
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
1
product or service or otherwise on a password-protected website for classroom
Objectives (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 2
product or service or otherwise on a password-protected website for classroom use.
Objectives (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 3
product or service or otherwise on a password-protected website for classroom use.
Classes (1 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 4
product or service or otherwise on a password-protected website for classroom use.
Classes (2 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 5
product or service or otherwise on a password-protected website for classroom use.
Classes (3 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 6
product or service or otherwise on a password-protected website for classroom use.
Classes (4 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 7
product or service or otherwise on a password-protected website for classroom use.
Unified Modeling Language Class Diagrams (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 8
product or service or otherwise on a password-protected website for classroom use.
Unified Modeling Language Class Diagrams (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 9
product or service or otherwise on a password-protected website for classroom use.
Variable (Object) Declaration
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 10
product or service or otherwise on a password-protected website for classroom use.
Accessing Class Members
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 11
product or service or otherwise on a password-protected website for classroom use.
Built-in Operations on Classes
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 12
product or service or otherwise on a password-protected website for classroom use.
Assignment Operator and Classes
FIGURE 10-3 myClock and yourClock before and after executing the
statement
myClock = yourClock;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 13
product or service or otherwise on a password-protected website for classroom use.
Class Scope (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 14
product or service or otherwise on a password-protected website for classroom use.
Class Scope (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 15
product or service or otherwise on a password-protected website for classroom use.
Functions and Classes
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 16
product or service or otherwise on a password-protected website for classroom use.
Reference Parameters and Class Objects (Variables) (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 17
product or service or otherwise on a password-protected website for classroom use.
Reference Parameters and Class Objects (Variables) (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 18
product or service or otherwise on a password-protected website for classroom use.
Implementation of Member Functions (1 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 19
product or service or otherwise on a password-protected website for classroom use.
Implementation of Member Functions (2 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 20
product or service or otherwise on a password-protected website for classroom use.
Implementation of Member Functions (3 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 21
product or service or otherwise on a password-protected website for classroom use.
Implementation of Member Functions (4 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 22
product or service or otherwise on a password-protected website for classroom use.
Accessor and Mutator Functions
• Accessor function: member function that only accesses the value(s) of member
variable(s)
• Mutator function: member function that modifies the value(s) of member
variable(s)
• Constant member function
• Member function that cannot modify member variables of that class
• Member function heading with const at the end
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 23
product or service or otherwise on a password-protected website for classroom use.
Order of public and private Members of a Class
• C++ has no fixed order in which to declare public and private members
• By default, all members of a class are private
• Use the member access specifier public to make a member available for
public access
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 24
product or service or otherwise on a password-protected website for classroom use.
Constructors (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 25
product or service or otherwise on a password-protected website for classroom use.
Constructors (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 26
product or service or otherwise on a password-protected website for classroom use.
Invoking a Constructor
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 27
product or service or otherwise on a password-protected website for classroom use.
Invoking the Default Constructor
• The statement:
clockType yourClock;
declares yourClock to be an object of type clockType and the default constructor
executes
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 28
product or service or otherwise on a password-protected website for classroom use.
Invoking a Constructor with Parameters
• Number and type of arguments should match the formal parameters (in the
order given) of one of the constructors
• Otherwise, C++ uses type conversion and looks for the best match
• Any ambiguity causes a compile-time error
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 29
product or service or otherwise on a password-protected website for classroom use.
Constructors and Default Parameters
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 30
product or service or otherwise on a password-protected website for classroom use.
Classes and Constructors: A Precaution
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 31
product or service or otherwise on a password-protected website for classroom use.
In-line Initialization of Data Members and the Default Constructor
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 32
product or service or otherwise on a password-protected website for classroom use.
Arrays of Class Objects (Variables) and Constructors
• If you declare an array of class objects, the class should have the default
constructor
• The default constructor is typically used to initialize each (array) class object
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 33
product or service or otherwise on a password-protected website for classroom use.
Destructors
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 34
product or service or otherwise on a password-protected website for classroom use.
Data Abstract, Classes, and Abstract Data Types
• Abstraction
• Separating design details from usage
• Separating the logical properties from the implementation details
• Abstraction also applicable to data
• Abstract data type (ADT): a data type that separates the logical properties from
the implementation details
• Three things associated with an ADT
• Type name: the name of the ADT
• Domain: the set of values belonging to the ADT
• Set of operations on the data
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 35
product or service or otherwise on a password-protected website for classroom use.
A struct versus a class (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 36
product or service or otherwise on a password-protected website for classroom use.
A struct versus a class (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 37
product or service or otherwise on a password-protected website for classroom use.
Information Hiding (1 of 3)
• Information hiding refers to hiding the details of the operations on the data
• The header file (or interface file) contains the specification details
• The header file has an extension h
• The implementation file contains the definitions of the functions to implement
the operations of an object
• This file has an extension cpp
• In the header file, include function prototypes and comments that briefly
describe the functions
• Specify preconditions and/or postconditions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 38
product or service or otherwise on a password-protected website for classroom use.
Information Hiding (2 of 3)
• Implementation file must include the header file via the include statement
• In the include statement:
• User-defined header files are enclosed in double quotes
• System-provided header files are enclosed between angular brackets
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 39
product or service or otherwise on a password-protected website for classroom use.
Information Hiding (3 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 40
product or service or otherwise on a password-protected website for classroom use.
Executable Code
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 41
product or service or otherwise on a password-protected website for classroom use.
More Examples of Classes
• Various examples of classes and how to use them in a program are presented
• Refer to Example 10-8 through Example 10-11
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 42
product or service or otherwise on a password-protected website for classroom use.
Inline Functions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 43
product or service or otherwise on a password-protected website for classroom use.
static Members of a Class (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 44
product or service or otherwise on a password-protected website for classroom use.
static Members of a Class (2 of 2)
• Multiple objects of a class each have their own copy of non-static member
variables
• All objects of a class share any static member of the class
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 45
product or service or otherwise on a password-protected website for classroom use.
Quick Review (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 46
product or service or otherwise on a password-protected website for classroom use.
Quick Review (2 of 3)
• The only built-in operations on classes are assignment and member selection
• Constructors guarantee that data members are initialized when an object is
declared
• A default constructor has no parameters
• The destructor automatically executes when a class object goes out of scope
• A class can have only one destructor
• The destructor has no parameters
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 47
product or service or otherwise on a password-protected website for classroom use.
Quick Review (3 of 3)
• An abstract data type (ADT) is a data type that separates the logical properties
from the implementation details
• A public static member, function or data, of a class can be accessed
using the class name and the scope resolution operator,
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 48
product or service or otherwise on a password-protected website for classroom use.