cpt s 122 data structures classes: a deeper look …nroy/courses/cpts122/notes/lecture...nirmalya...
TRANSCRIPT
![Page 1: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/1.jpg)
Nirmalya Roy
School of Electrical Engineering and Computer ScienceWashington State University
Cpt S 122 – Data Structures
Classes: A Deeper LookPart -II
![Page 2: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/2.jpg)
Topics
const objects and const Member functions
Composition: Objects as Member of Classes
friend Functions and friend Classes
Using this pointer
static class Members
![Page 3: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/3.jpg)
const objects and const member functions prevent modifications of objects and enforce the principle of least
privilege.
Composition a form of reuse in which a class can have objects of other classes as
members.
Friendship enables a class designer to specify nonmember functions that can
access a class’s non-public members
The this pointer an implicit argument to each of a class’s non-static member
functions.
allows those member functions to access the correct object’s data members and other non-static member functions.
Motivate the need for static class members.
Introduction
![Page 4: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/4.jpg)
You may use keyword const to specify that an
object is not modifiable
any attempt to modify the object should result in a
compilation error.
C++ disallows member function calls for constobjects unless the member functions themselves are
also declared const.
True even for get member functions that do not modify
the object.
A member function is specified as const both in
its prototype and in its definition.
const Object and const Member Function
![Page 5: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/5.jpg)
![Page 6: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/6.jpg)
![Page 7: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/7.jpg)
![Page 8: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/8.jpg)
![Page 9: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/9.jpg)
![Page 10: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/10.jpg)
![Page 11: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/11.jpg)
![Page 12: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/12.jpg)
![Page 13: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/13.jpg)
![Page 14: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/14.jpg)
Member initializer syntax
All data members can be initialized using member initializer syntax,
const data members must be initialized using member initializers.
Member initializers appear between a constructor’s parameter list and the left brace that begins the constructor’s body.
Separated from the parameter list with a colon (:).
Each member initializer consists of the data member name followed by parentheses containing the member’s initial value.
const Object and const Member Function
![Page 15: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/15.jpg)
const Data Member
![Page 16: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/16.jpg)
const Data Member
![Page 17: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/17.jpg)
![Page 18: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/18.jpg)
![Page 19: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/19.jpg)
Composition
Sometimes referred to as a has-a relationship
A class can have objects of other classes as members
An object’s constructor can pass arguments to
member-object constructors via member initializers.
Composition: Objects as Member of Classes
![Page 20: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/20.jpg)
![Page 21: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/21.jpg)
![Page 22: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/22.jpg)
![Page 23: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/23.jpg)
![Page 24: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/24.jpg)
![Page 25: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/25.jpg)
![Page 26: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/26.jpg)
![Page 27: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/27.jpg)
![Page 28: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/28.jpg)
As you study class Date, notice that the class does not provide a constructor that receives a parameter of type Date.
Why can the Employee constructor’s member initializer list initialize the birthDate and hireDate objects by passing Date object’s to their Date constructors?
The compiler provides each class with a default copy constructor that copies each data member of the constructor’s argument object into the corresponding member of the object being initialized.
Composition: Objects as Member of Classes
![Page 29: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/29.jpg)
![Page 30: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/30.jpg)
![Page 31: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/31.jpg)
A friend function of a class is defined outside that
class’s scope
It has the right to access the non-public (and public)
members of the class.
Standalone functions, entire classes or member functions
of other classes may be declared to be friends of another
class.
Using friend functions can enhance performance.
Friendship is granted, not taken.
The friendship relation is neither symmetric nor
transitive.
friend Functions and friend Classes
![Page 32: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/32.jpg)
friend Functions
![Page 33: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/33.jpg)
friend Functions
![Page 34: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/34.jpg)
How do member functions know which object’s data
members to manipulate?
Every object has access to its own address through a pointer called this (a C++ keyword).
The this pointer is not part of the object itself.
The this pointer is passed (by the compiler) as an implicit
argument to each of the object’s non-static member functions.
Objects use the this pointer implicitly or explicitly to
reference their data members and member functions.
The type of the this pointer depends on the type of the
object and
the member function in which this is used is declared const.
Using the this Pointer
![Page 35: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/35.jpg)
![Page 36: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/36.jpg)
this Pointer
![Page 37: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/37.jpg)
this Pointer
![Page 38: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/38.jpg)
Another use of the this pointer is to enable cascaded
member-function calls
invoking multiple functions in the same statement
Modify class Time’s set functions setTime,
setHour, setMinute and setSecond such that each returns a reference to a Time object to
enable cascaded member function calls.
Using the this pointer
![Page 39: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/39.jpg)
![Page 40: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/40.jpg)
![Page 41: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/41.jpg)
![Page 42: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/42.jpg)
![Page 43: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/43.jpg)
![Page 44: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/44.jpg)
![Page 45: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/45.jpg)
![Page 46: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/46.jpg)
![Page 47: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/47.jpg)
![Page 48: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/48.jpg)
Why does the technique of returning *this as a reference work?
The dot operator (.) associates from left to right,
first evaluates t.setHour(18), then returns a reference to object t as the value of this function call.
The remaining expression is then interpreted as t.setMinute( 30 ).setSecond( 22 )
The t.setMinute( 30 ) call executes and returns a reference to the object t.
The remaining expression is interpreted as t.setSecond( 22 )
Using the this pointer
![Page 49: Cpt S 122 Data Structures Classes: A Deeper Look …nroy/courses/cpts122/notes/Lecture...Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University](https://reader033.vdocuments.mx/reader033/viewer/2022042000/5e6d28602a510c26634c59ca/html5/thumbnails/49.jpg)
In certain cases, only one copy of a variable should
be shared by all objects of a class.
A static data member is used for these and other
reasons.
Such a variable represents “class-wide” information.
Use static data members to save storage when a
single copy of the data for all objects of a class will
suffice.
static class members