SOW - C++ - CSO - Chapter - 13 - 9e - Tagged
SOW - C++ - CSO - Chapter - 13 - 9e - Tagged
9th Edition
Chapter 13
Introduction to Classes
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.1
Procedural and Object-Oriented Programming
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Procedural and Object-Oriented
Programming
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Limitations of Procedural Programming
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Object-Oriented Programming
Terminology
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Classes and Objects
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Object-Oriented Programming
Terminology
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
More on Objects
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.2
Introduction to Classes
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Introduction to Classes
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Class Example
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Access Specifiers
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Class Example
Private Members
Public Members
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
More on Access Specifiers
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Using const With Member Functions
• const appearing after the parentheses in a
member function declaration specifies that the
function will not change any data in the calling
object.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Defining a Member Function
int Rectangle::setWidth(double w)
{
width = w;
}
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Accessors and Mutators
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.3
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Defining an Instance of a Class
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Program 13-1 (Continued)
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Program 13-1 (Continued)
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Program 13-1 (Continued)
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Avoiding Stale Data
• Some data is the result of a calculation.
• In the Rectangle class the area of a rectangle is calculated.
– length x width
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Pointer to an Object
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Dynamically Allocating an Object
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.4
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Why Have Private Members?
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Code outside the class must use the class's
public member functions to interact with the
object.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.5
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Separating Specification from
Implementation
– Place class declaration in a header file that serves as
the class specification file. Name the file
ClassName.h, for example, Rectangle.h
– Place member function definitions in
ClassName.cpp, for example, Rectangle.cpp
File should #include the class specification file
– Programs that use the class must #include the
class specification file, and be compiled and linked
with the member function definitions
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.6
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Inline Member Functions
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Rectangle Class with Inline Member
Functions
1 // Specification file for the Rectangle class
2 // This version uses some inline member functions.
3 #ifndef RECTANGLE_H
4 #define RECTANGLE_H
5
6 class Rectangle
7 {
8 private:
9 double width;
10 double length;
11 public:
12 void setWidth(double);
13 void setLength(double);
14
15 double getWidth() const
16 { return width; }
17
18 double getLength() const
19 { return length; }
20
21 double getArea() const
22 { return width * length; }
23 };
24 #endif
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Tradeoffs – Inline vs. Regular Member
Functions
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.7
Constructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Constructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Continues...
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Contents of Rectangle.ccp Version3
(continued)
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
In-Place Initialization
• If you are using C++11 or later, you can initialize a
member variable in its declaration statement, just as
you can with a regular variable.
• This is known as in-place initialization. Here is an
example:
class Rectangle
{
private:
double width = 0.0;
double length = 0.0;
public:
Public member functions appear here…
};
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Default Constructors
• A default constructor is a constructor that takes no
arguments.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.8
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Passing Arguments to Constructors
Rectangle(double, double);
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Passing Arguments to Constructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
More About Default Constructors
Rectangle r;
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Classes with No Default Constructor
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.9
Destructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Destructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Contents of InventoryItem.h Version1
(continued)
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Constructors, Destructors, and
Dynamically Allocated Objects
delete r;
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.10
Overloading Constructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Overloading Constructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Continues...
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Constructor Delegation
• Sometimes a class will have multiple constructors that perform a
similar set of steps. For example, look at the following Contact
class:
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Constructor Delegation
• Both constructors perform a similar operation: They assign values
to the name, email, and phone member variables.
• The default constructor assigns empty strings to the members, and
the parameterized constructor assigns specified values to the
members.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Constructor Delegation
• In C++ 11, it is possible for one constructor to call another
constructor in the same class.
• This is known as constructor delegation.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Only One Default Constructor and One
Destructor
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Member Function Overloading
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.11
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Using Private Member Functions
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.12
Arrays of Objects
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Arrays of Objects
InventoryItem inventory[40];
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Arrays of Objects
InventoryItem inventory[3] =
{ "Hammer", "Wrench", "Pliers" };
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Arrays of Objects
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Arrays of Objects
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Accessing Objects in an Array
inventory[2].setUnits(30);
cout << inventory[2].getUnits();
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Program 13-14 (Continued)
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
13.16
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
The Unified Modeling Language
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
UML Class Diagram
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Example: A Rectangle Class
class Rectangle
{
private:
double width;
double length;
public:
bool setWidth(double);
bool setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
};
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
UML Access Specification Notation
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
UML Data Type Notation
- width : double
- length : double
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
UML Parameter Type Notation
+ setWidth(w : double)
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
UML Function Return Type Notation
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
The Rectangle Class
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Showing Constructors and Destructors
Constructors
Destructor
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright
This work is protected by United States copyright laws and is provided solely
for the use of instructions in teaching their courses and assessing student
learning. dissemination or sale of any part of this work (including on the
World Wide Web) will destroy the integrity of the work and is not permit-
ted. The work and materials from it should never be made available to
students except by instructors using the accompanying text in their
classes. All recipients of this work are expected to abide by these
restrictions and to honor the intended pedagogical purposes and the needs of
other instructors who rely on these materials.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved