SOW - C++ - CSO - Chapter - 14 - 9e - Tagged
SOW - C++ - CSO - Chapter - 14 - 9e - Tagged
9th Edition
Chapter 14
More about Classes
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
14.1
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Instance and Static Members
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
static member variable
Contents of Tree.h
1 // Tree class Static member declared here.
2 class Tree
3 {
4 private:
5 static int objectCount; // Static member variable.
6 public:
7 // Constructor
8 Tree()
9 { objectCount++; }
10
11 // Accessor function for objectCount
12 int getObjectCount() const
13 { return objectCount; } Static member defined here.
14 };
15
16 // Definition of the static member variable, written
17 // outside the class.
18 int Tree::objectCount = 0;
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Three Instances of the Tree Class, But Only
One objectCount Variable
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
static member function
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Modified Version of Tree.h
1 // Tree class
2 class Tree
3 {
4 private:
5 static int objectCount; // Static member
variable.
6 public:
7 // Constructor
8 Tree()
9 { objectCount++; }
10
11 // Accessor function for objectCount
12 static int getObjectCount() const
13 { return objectCount; }
14 };
15
16 // Definition of the static member variable,
written
17 // outside the class.
18 int Tree::objectCount = 0;
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
14.2
Friends of Classes
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Friends of Classes
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
friend Function Declarations
• Stand-alone function:
friend void setAVal(intVal&, int);
// declares setAVal function to be
// a friend of this class
• Member function of another class:
friend void SomeClass::setNum(int num)
// setNum function from SomeClass
// class is a friend of this class
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
friend Class Declarations
• Class as a friend of a class:
class FriendClass
{
...
};
class NewClass
{
public:
friend class FriendClass; // declares
// entire class FriendClass as a friend
// of this class
…
};
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
14.3
Memberwise Assignment
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Memberwise Assignment
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
14.4
Copy Constructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copy Constructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copy Constructors
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copy Constructors
13
object1 object2
value value
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Programmer-Defined Copy Constructor
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Programmer-Defined Copy Constructor
5 13
object1 object2
value value
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Programmer-Defined Copy Constructor
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
14.5
Operator Overloading
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Operator Overloading
• Operators such as =, +, and others can be redefined
when used with objects of a class
• The name of the function for the overloaded operator is
operator followed by the operator symbol, e.g.,
operator+ to overload the + operator, and
operator= to overload the = operator
• Prototype for the overloaded operator goes in the
declaration of the class that is overloading it
• Overloaded operator function definition goes with other
member functions
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
The this Pointer
• this: predefined pointer available to a class’s
member functions
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
The this Pointer
• Example, student1 and student2 are both
StudentTestScores objects.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
The this Pointer
• Likewise, the following statement causes the
getStudentName member function to operate on
student2:
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Operator Overloading
• Prototype:
void operator=(const SomeClass &rval)
parameter for
return function object on right
type name side of operator
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Invoking an Overloaded Operator
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Returning a Value
...
public:
double operator-(const point2d &right)
{ return sqrt(pow((x-right.x),2)
+ pow((y-right.y),2)); }
};
Point2d point1(2,2), point2(4,4);
// Compute and display distance between 2 points.
cout << point2 – point1 << endl; // displays 2.82843
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Returning a Value
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Notes on
Overloaded Operators
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Overloading Types of Operators
• ++, -- operators overloaded differently for
prefix vs. postfix notation
• Overloaded relational operators should return a
bool value
• Overloaded stream operators >>, << must
return reference to istream, ostream objects
and take istream, ostream objects as
parameters
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Overloaded [] Operator
• Can create classes that behave like arrays,
provide bounds-checking on subscripts
• Must consider constructor, destructor
• Overloaded [] returns a reference to object,
not an object itself
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
14.6
Object Conversion
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Object Conversion
• Type of an object can be converted to another type
• Automatically done for built-in data types
• Must write an operator function to perform conversion
• To convert an FeetInches object to an int:
FeetInches::operator int()
{return feet;}
• Assuming distance is a FeetInches object, allows
statements like:
int d = distance;
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
14.7
Aggregation
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Aggregation
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Aggregation
class StudentInfo
{
private:
string firstName, LastName;
string address, city, state, zip;
...
};
class Student
{
private:
StudentInfo personalData;
...
};
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
See the Instructor, TextBook, and
Course classes in Chapter 14.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
14.10
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Temporary Values
• Consider this code:
int x;
x = 2 * 6;
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Temporary Values
• Consider this: The square function is called,
and the value 5 is passed as an
1 int square(int a) argument.
2 {
3 return a * a; The square function calculates 5
4 }
* 5 and stores the result, 25, as a
5
6 int main() temporary value.
7 {
8 int x = 0; The temporary value is copied
9 (assigned) to the variable x.
10 x = square(5);
11 cout << x << endl; The temporary value is no longer
12 return 0; needed, so the system discards it.
13 }
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Lvalues and Rvalues
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Rvalue References
• Rvalue Reference: a reference variable that can refer only to
temporary objects that would otherwise have no name.
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Move Assignment vs. Copy Assignment
• From the Person class, in Chapter 14:
Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved
Move Constructor vs. Copy Constructor
• From the Person class, in Chapter 14:
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