SlideShare a Scribd company logo
1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition
Chapter 12
Pointers, Classes, Virtual Functions, and Abstract Classes
2
Objectives (1 of 3)
• In this chapter, you will:
• Learn about the pointer data type and pointer variables
• Explore how to declare and manipulate pointer variables
• Learn about the address of the operator and the dereferencing operator
• Learn how pointers work with classes and structs
• Discover dynamic variables
© 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 product or service
or otherwise on a password-protected website for classroom
3
Objectives (2 of 3)
• Explore how to use the new and delete operators to manipulate dynamic variables
• Learn about pointer arithmetic
• Learn how to work with dynamic arrays
• Become familiar with the limitations of range-based for loops with dynamic arrays
• Explore how pointers work with functions as parameters and functions as return
values
© 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 product or service
or otherwise on a password-protected website for classroom
4
Objectives (3 of 3)
• Become familiar with the shallow and deep copies of data
• Discover the peculiarities of classes with pointer member variables
• Learn about virtual functions
• Become aware of abstract classes
• Examine the relationship between the address of operator 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 product or service
or otherwise on a password-protected website for classroom
5
Pointer Data Type and Pointer Variables
• A pointer variable is a variable whose content is a memory address
• No name is associated with the pointer data type in C++
© 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 product or service
or otherwise on a password-protected website for classroom
6
Declaring Pointer Variables (1 of 2)
• The general syntax to declare a pointer variable is:
• The statements below each declare a pointer:
• int *p;
• char *ch;
• These statements are equivalent:
• int *p;
• int* p;
• int * p;
© 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 product or service
or otherwise on a password-protected website for classroom
7
Declaring Pointer Variables (2 of 2)
• In the statement:
int* p, q;
• Only p is a pointer variable
• q is an int variable
• To avoid confusion, attach the character * to the variable name:
int *p, q;
int *p, *q;
© 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 product or service
or otherwise on a password-protected website for classroom
8
Address of Operator (&)
• Address of operator (&):
• A unary operator that returns the address of its operand
• Example:
int x;
int *p;
p = &x; //Assigns the address of x to p
© 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 product or service
or otherwise on a password-protected website for classroom
9
Dereferencing Operator (*)
• Dereferencing operator (or indirection operator):
• When used as a unary operator, * refers to object to which its operand points
• Example:
cout << *p << endl;
• Prints the value stored in the memory location pointed to by p
© 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 product or service
or otherwise on a password-protected website for classroom
10
Classes, structs, and Pointer Variables (1 of 3)
• You can declare pointers to other data types, such as a struct:
struct studentType
{
char name[26];
double gpa;
int sID;
char grade;
};
studentType student;
studentType* studentPtr;
• student is an object of type studentType
• studentPtr is a pointer variable of type studentType
© 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 product or service
or otherwise on a password-protected website for classroom
11
Classes, structs, and Pointer Variables (2 of 3)
• To store address of student in studentPtr:
studentPtr = &student;
• To store 3.9 in component gpa of student:
(*studentPtr).gpa = 3.9;
• ( ) used because dot operator has higher precedence than dereferencing operator
• Alternative: use member access operator arrow (->)
© 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 product or service
or otherwise on a password-protected website for classroom
12
Classes, structs, and Pointer Variables (3 of 3)
• Syntax to access a class (struct) member using the operator -> :
• Thus,
(*studentPtr).gpa = 3.9;
is equivalent to:
studentPtr->gpa = 3.9;
© 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 product or service
or otherwise on a password-protected website for classroom
13
Initializing Pointer Variables
• C++ does not automatically initialize variables
• Pointer variables must be initialized if you do not want them to point to
anything
• Initialized to the value 0 using the null pointer
• Or, use the NULL named constant
• The number 0 is the only number that can be directly assigned to a pointer
variable
• C++11 Standard includes a nullptr
© 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 product or service
or otherwise on a password-protected website for classroom
14
Dynamic Variables
• Dynamic variables are created during execution
• C++ creates dynamic variables using pointers
• new and delete operators: used to create and destroy dynamic variables
• new and delete are reserved words in C++
© 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 product or service
or otherwise on a password-protected website for classroom
15
Operator new (1 of 2)
• new has two forms:
• intExp is any expression evaluating to a positive integer
• new allocates memory (a variable) of the designated type and returns a pointer
to it
• The allocated memory is uninitialized
© 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 product or service
or otherwise on a password-protected website for classroom
16
Operator new (2 of 2)
• Example: p = new int;
• Creates a variable during program execution somewhere in memory
• Stores the address of the allocated memory in p
• To access allocated memory, use *p
• A dynamic variable cannot be accessed directly
• Because it is unnamed
© 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 product or service
or otherwise on a password-protected website for classroom
17
Operator delete
• Memory leak: previously allocated memory that cannot be reallocated
• To avoid a memory leak, when a dynamic variable is no longer needed, destroy it to
deallocate its memory
• delete operator: used to destroy dynamic variables
• Syntax:
© 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 product or service
or otherwise on a password-protected website for classroom
18
Operations on Pointer Variables (1 of 2)
• Assignment: value of one pointer variable can be assigned to another pointer of
same type
• Relational operations: two pointer variables of same type can be compared for
equality, etc.
• Some limited arithmetic operations
• Integer values can be added and subtracted from a pointer variable
• Value of one pointer variable can be subtracted from another pointer variable
© 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 product or service
or otherwise on a password-protected website for classroom
19
Operations on Pointer Variables (2 of 2)
• Pointer arithmetic can be very dangerous:
• Program can accidentally access memory locations of other variables and change their
content without warning
- Some systems might terminate the program with an appropriate error message
• Always exercise extra care when doing pointer arithmetic
© 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 product or service
or otherwise on a password-protected website for classroom
20
Dynamic Arrays (1 of 2)
• Dynamic array: array created during program execution
• Example:
int *p;
p = new int[10];
*p = 25; //stores 25 in the first memory location
p++; //to point to next array component
*p = 35; // stores 35 into the second memory location
© 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 product or service
or otherwise on a password-protected website for classroom
21
Dynamic Arrays (2 of 2)
• Can use array notation to access these memory locations
• Example:
p[0] = 25;
p[1] = 35;
• Stores 25 and 35 into the first and second array components, respectively
• An array name is a constant pointer
© 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 product or service
or otherwise on a password-protected website for classroom
22
Functions and Pointers
• Pointer variable can be passed as a parameter either by value or by reference
• As a reference parameter in a function heading, use &:
void pointerParameters(int* &p, double *q)
{
. . .
}
© 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 product or service
or otherwise on a password-protected website for classroom
23
Pointers and Function Return Values
• A function can return a value of type pointer:
int* testExp(...)
{
. . .
}
© 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 product or service
or otherwise on a password-protected website for classroom
24
Dynamic Two-Dimensional Arrays
• You can create dynamic multidimensional arrays
• Examples:
int *board[4]; //declares board to be an array of four
//pointers wherein each pointer is of type
for (int row = 0; row < 4; row++)
board[row] = new int[6]; //creates the rows of board
int **board; //declares board to be a pointer to a pointer
© 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 product or service
or otherwise on a password-protected website for classroom
25
Shallow versus Deep Copy and Pointers
• Shallow copy: when two or more pointers of the same types point to the same
memory
• They point to the same data
• Danger: deleting one deletes the data pointed to by all of them
• Deep copy: when the contents of the memory pointed to by a pointer are
copied to the memory location of another pointer
• Two copies of 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 product or service
or otherwise on a password-protected website for classroom
26
Classes and Pointers: Some Peculiarities (1 of 2)
• Example class:
class ptrMemberVarType
{
public:
.
.
.
private:
int x;
int lenP;
int *p;
};
• Example program statements:
ptrMemberVarType objectOne;
ptrMemberVarType objectTwo;
© 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 product or service
or otherwise on a password-protected website for classroom
27
Classes and Pointers: Some Peculiarities (2 of 2)
FIGURE 12-13 Objects objectOne and objectTwo
© 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 product or service
or otherwise on a password-protected website for classroom
28
Destructor
• If objectOne goes out of scope, its member variables are destroyed
• Memory space of a dynamic array stays marked as allocated, even though it cannot be
accessed
• Solution: in destructor, ensure that when objectOne goes out of scope, its
array memory is deallocated:
ptrMemberVarType::~ptrMemberVarType()
{
delete [] p;
}
© 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 product or service
or otherwise on a password-protected website for classroom
29
Assignment Operator
• After a shallow copy: if objectTwo.p deallocates memory space to which it
points, objectOne.p becomes invalid
FIGURE 12-15 Objects objectOne and objectTwo
• Solution: extend definition of the assignment operator to avoid shallow copying
of 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 product or service
or otherwise on a password-protected website for classroom
30
Copy Constructor (1 of 3)
• Default member-wise initialization:
• Initializing a class object by using the value of an existing object of the same type
• Example:
ptrMemberVarType objectThree(objectOne);
• Copy constructor: provided by the compiler
• Performs this initialization
• Leads to a shallow copying of the data if class has pointer member variables
© 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 product or service
or otherwise on a password-protected website for classroom
31
Copy Constructor (2 of 3)
• Similar problem occurs when passing objects by value
• Copy constructor automatically executes in three situations:
• When an object is declared and initialized by using the value of another object
• When an object is passed by value as a parameter
• When the return value of a function is an 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 product or service
or otherwise on a password-protected website for classroom
32
Copy Constructor (3 of 3)
• Solution: override the copy constructor
• For classes with pointer member variables, three things are normally done:
• Include the destructor in the class
• Overload the assignment operator for the class
• Include the copy 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 product or service
or otherwise on a password-protected website for classroom
33
Inheritance, Pointers, and Virtual Functions (1 of 3)
• Can pass an object of a derived class to a formal parameter of the base class
type
• Compile-time binding: the necessary code to call specific function is generated
by compiler
• Also known as static binding or early binding
• Virtual function: binding occurs at program execution time, not at compile time
• Declared with reserved word virtual
© 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 product or service
or otherwise on a password-protected website for classroom
34
Inheritance, Pointers, and Virtual Functions (2 of 3)
• Run-time binding:
• Compiler does not generate code to call a specific function: it generates information
to enable run-time system to generate specific code for the function call
• Also known as late binding or dynamic binding
• Note: cannot pass an object of base class type to a formal parameter of the
derived class type
© 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 product or service
or otherwise on a password-protected website for classroom
35
Inheritance, Pointers, and Virtual Functions (3 of 3)
• Values of a derived class object can be copied into a base class object
• Slicing problem: if derived class has more data members than base class, some
data could be lost
• Solution: use pointers for both base and derived class objects
© 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 product or service
or otherwise on a password-protected website for classroom
36
Classes and Virtual Destructors (1 of 2)
• Classes with pointer member variables should have the destructor
• Destructor should deallocate storage for dynamic objects
• If a derived class object is passed to a formal parameter of the base class type,
destructor of the base class executes
• Regardless of whether object is passed by reference or by value
• Solution: use a virtual destructor (base 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 product or service
or otherwise on a password-protected website for classroom
37
Classes and Virtual Destructors (2 of 2)
• Virtual destructor of a base class automatically makes the destructor of a
derived class virtual
• After executing the destructor of the derived class, the destructor of the base class
executes
• If a base class contains virtual functions, make the destructor of the base class
virtual
© 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 product or service
or otherwise on a password-protected website for classroom
38
Abstract Classes and Pure Virtual Functions (1 of 2)
• New classes can be derived through inheritance without designing them from
scratch
• Derived classes:
• Inherit existing members of base class
• Can add their own members
• Can redefine or override public and protected member functions
• Base class can contain functions that you would want each derived class to
implement
• However, base class may contain functions that may not have meaningful definitions
in the base 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 product or service
or otherwise on a password-protected website for classroom
39
Abstract Classes and Pure Virtual Functions (2 of 2)
• Pure virtual functions do not have definitions (bodies have no code)
• Example:
virtual void draw() = 0;
• An abstract class is a class with one or more virtual functions
• It can contain instance variables, constructors, and functions that are not pure virtual
• It must provide the definitions of the constructor and functions that are not pure
virtual
© 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 product or service
or otherwise on a password-protected website for classroom
40
Address of Operator and Classes (1 of 2)
• & operator can create aliases to an object
• Example:
int x;
int &y = x; //x and y refer to the same memory location
//y is like a constant pointer variable
y = 25; //sets the value of y (and of x) to 25
x = 2 * x + 30; //updates value of x and y
© 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 product or service
or otherwise on a password-protected website for classroom
41
Address of Operator and Classes (2 of 2)
• Address of operator can also be used to return the address of a private member
variable of a class
• However, if you are not careful, this operation can result in serious errors in the
program
© 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 product or service
or otherwise on a password-protected website for classroom
42
Quick Review (1 of 2)
• Pointer variables contain the addresses of other variables as their values
• Declare a pointer variable with an asterisk, *, between the data type and the variable
• Address of operator (&) returns the address of its operand
• Unary operator * is the dereferencing operator
• Member access operator (->) accesses the object component pointed to by a pointer
© 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 product or service
or otherwise on a password-protected website for classroom
43
Quick Review (2 of 2)
• Dynamic variable: created during execution
• Created using new
• Deallocated using delete
• Shallow copy: two or more pointers of the same type point to the same
memory
• Deep copy: two or more pointers of the same type have their own copies of the
data
• Binding of virtual functions occurs at execution time (dynamic or run-time
binding)
© 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 product or service
or otherwise on a password-protected website for classroom

More Related Content

What's hot

9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
Terry Yoast
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
Terry Yoast
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
AnushaNaidu
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
Terry Yoast
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
RushiBShah
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community
Rohit Radhakrishnan
 
Certification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxCertification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptx
Rohit Radhakrishnan
 
Certification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptxCertification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptx
Rohit Radhakrishnan
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
L05 Frameworks
L05 FrameworksL05 Frameworks
L05 Frameworks
Ólafur Andri Ragnarsson
 
L07 Frameworks
L07 FrameworksL07 Frameworks
L07 Frameworks
Ólafur Andri Ragnarsson
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 
L09 Frameworks
L09 FrameworksL09 Frameworks
L09 Frameworks
Ólafur Andri Ragnarsson
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test Automation
Gregory Solovey
 
Advice weaving in AspectJ
Advice weaving in AspectJAdvice weaving in AspectJ
Advice weaving in AspectJ
Sander Mak (@Sander_Mak)
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
Marian Wamsiedel
 

What's hot (17)

9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community
 
Certification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxCertification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptx
 
Certification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptxCertification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptx
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
L05 Frameworks
L05 FrameworksL05 Frameworks
L05 Frameworks
 
L07 Frameworks
L07 FrameworksL07 Frameworks
L07 Frameworks
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
 
L09 Frameworks
L09 FrameworksL09 Frameworks
L09 Frameworks
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test Automation
 
Advice weaving in AspectJ
Advice weaving in AspectJAdvice weaving in AspectJ
Advice weaving in AspectJ
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
 

Similar to 9781337102087 ppt ch12

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressions
pullaravikumar
 
Python Fundamentals
Python FundamentalsPython Fundamentals
Python Fundamentals
pullaravikumar
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
Terry Yoast
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha Projects
AmitaSuri
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
TanayKashid
 
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjdC+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
itzvenkatesh21
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentation
dgdotson
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentation
dgdotson
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
Dr. Ahmed Al Zaidy
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
Dr. Ahmed Al Zaidy
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7
Steve Guinan
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account Management
Dr. Ahmed Al Zaidy
 
Whitman_Ch06.pptx
Whitman_Ch06.pptxWhitman_Ch06.pptx
Whitman_Ch06.pptx
Siphamandla9
 
Chapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IChapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K I
Dr. Ahmed Al Zaidy
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
mshellman
 
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
sleeperharwell
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentation
dgdotson
 
Intro to Web Design 6e Chapter 5
Intro to Web Design 6e Chapter 5Intro to Web Design 6e Chapter 5
Intro to Web Design 6e Chapter 5
Steve Guinan
 
Excel Modules 4-7 Microsoft Excel Shelly Cashman.pptx
Excel Modules 4-7 Microsoft Excel Shelly Cashman.pptxExcel Modules 4-7 Microsoft Excel Shelly Cashman.pptx
Excel Modules 4-7 Microsoft Excel Shelly Cashman.pptx
LaRissaWilliams15
 

Similar to 9781337102087 ppt ch12 (20)

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressions
 
Python Fundamentals
Python FundamentalsPython Fundamentals
Python Fundamentals
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha Projects
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
 
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjdC+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentation
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentation
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account Management
 
Whitman_Ch06.pptx
Whitman_Ch06.pptxWhitman_Ch06.pptx
Whitman_Ch06.pptx
 
Chapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IChapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K I
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
 
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentation
 
Intro to Web Design 6e Chapter 5
Intro to Web Design 6e Chapter 5Intro to Web Design 6e Chapter 5
Intro to Web Design 6e Chapter 5
 
Excel Modules 4-7 Microsoft Excel Shelly Cashman.pptx
Excel Modules 4-7 Microsoft Excel Shelly Cashman.pptxExcel Modules 4-7 Microsoft Excel Shelly Cashman.pptx
Excel Modules 4-7 Microsoft Excel Shelly Cashman.pptx
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
Terry Yoast
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
Terry Yoast
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
Terry Yoast
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
Terry Yoast
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
Terry Yoast
 

More from Terry Yoast (17)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
 

Recently uploaded

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 

Recently uploaded (20)

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 

9781337102087 ppt ch12

  • 1. 1 C++ Programming: From Problem Analysis to Program Design, Eighth Edition Chapter 12 Pointers, Classes, Virtual Functions, and Abstract Classes
  • 2. 2 Objectives (1 of 3) • In this chapter, you will: • Learn about the pointer data type and pointer variables • Explore how to declare and manipulate pointer variables • Learn about the address of the operator and the dereferencing operator • Learn how pointers work with classes and structs • Discover dynamic variables © 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 product or service or otherwise on a password-protected website for classroom
  • 3. 3 Objectives (2 of 3) • Explore how to use the new and delete operators to manipulate dynamic variables • Learn about pointer arithmetic • Learn how to work with dynamic arrays • Become familiar with the limitations of range-based for loops with dynamic arrays • Explore how pointers work with functions as parameters and functions as return values © 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 product or service or otherwise on a password-protected website for classroom
  • 4. 4 Objectives (3 of 3) • Become familiar with the shallow and deep copies of data • Discover the peculiarities of classes with pointer member variables • Learn about virtual functions • Become aware of abstract classes • Examine the relationship between the address of operator 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 product or service or otherwise on a password-protected website for classroom
  • 5. 5 Pointer Data Type and Pointer Variables • A pointer variable is a variable whose content is a memory address • No name is associated with the pointer data type in C++ © 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 product or service or otherwise on a password-protected website for classroom
  • 6. 6 Declaring Pointer Variables (1 of 2) • The general syntax to declare a pointer variable is: • The statements below each declare a pointer: • int *p; • char *ch; • These statements are equivalent: • int *p; • int* p; • int * p; © 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 product or service or otherwise on a password-protected website for classroom
  • 7. 7 Declaring Pointer Variables (2 of 2) • In the statement: int* p, q; • Only p is a pointer variable • q is an int variable • To avoid confusion, attach the character * to the variable name: int *p, q; int *p, *q; © 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 product or service or otherwise on a password-protected website for classroom
  • 8. 8 Address of Operator (&) • Address of operator (&): • A unary operator that returns the address of its operand • Example: int x; int *p; p = &x; //Assigns the address of x to p © 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 product or service or otherwise on a password-protected website for classroom
  • 9. 9 Dereferencing Operator (*) • Dereferencing operator (or indirection operator): • When used as a unary operator, * refers to object to which its operand points • Example: cout << *p << endl; • Prints the value stored in the memory location pointed to by p © 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 product or service or otherwise on a password-protected website for classroom
  • 10. 10 Classes, structs, and Pointer Variables (1 of 3) • You can declare pointers to other data types, such as a struct: struct studentType { char name[26]; double gpa; int sID; char grade; }; studentType student; studentType* studentPtr; • student is an object of type studentType • studentPtr is a pointer variable of type studentType © 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 product or service or otherwise on a password-protected website for classroom
  • 11. 11 Classes, structs, and Pointer Variables (2 of 3) • To store address of student in studentPtr: studentPtr = &student; • To store 3.9 in component gpa of student: (*studentPtr).gpa = 3.9; • ( ) used because dot operator has higher precedence than dereferencing operator • Alternative: use member access operator arrow (->) © 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 product or service or otherwise on a password-protected website for classroom
  • 12. 12 Classes, structs, and Pointer Variables (3 of 3) • Syntax to access a class (struct) member using the operator -> : • Thus, (*studentPtr).gpa = 3.9; is equivalent to: studentPtr->gpa = 3.9; © 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 product or service or otherwise on a password-protected website for classroom
  • 13. 13 Initializing Pointer Variables • C++ does not automatically initialize variables • Pointer variables must be initialized if you do not want them to point to anything • Initialized to the value 0 using the null pointer • Or, use the NULL named constant • The number 0 is the only number that can be directly assigned to a pointer variable • C++11 Standard includes a nullptr © 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 product or service or otherwise on a password-protected website for classroom
  • 14. 14 Dynamic Variables • Dynamic variables are created during execution • C++ creates dynamic variables using pointers • new and delete operators: used to create and destroy dynamic variables • new and delete are reserved words in C++ © 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 product or service or otherwise on a password-protected website for classroom
  • 15. 15 Operator new (1 of 2) • new has two forms: • intExp is any expression evaluating to a positive integer • new allocates memory (a variable) of the designated type and returns a pointer to it • The allocated memory is uninitialized © 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 product or service or otherwise on a password-protected website for classroom
  • 16. 16 Operator new (2 of 2) • Example: p = new int; • Creates a variable during program execution somewhere in memory • Stores the address of the allocated memory in p • To access allocated memory, use *p • A dynamic variable cannot be accessed directly • Because it is unnamed © 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 product or service or otherwise on a password-protected website for classroom
  • 17. 17 Operator delete • Memory leak: previously allocated memory that cannot be reallocated • To avoid a memory leak, when a dynamic variable is no longer needed, destroy it to deallocate its memory • delete operator: used to destroy dynamic variables • Syntax: © 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 product or service or otherwise on a password-protected website for classroom
  • 18. 18 Operations on Pointer Variables (1 of 2) • Assignment: value of one pointer variable can be assigned to another pointer of same type • Relational operations: two pointer variables of same type can be compared for equality, etc. • Some limited arithmetic operations • Integer values can be added and subtracted from a pointer variable • Value of one pointer variable can be subtracted from another pointer variable © 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 product or service or otherwise on a password-protected website for classroom
  • 19. 19 Operations on Pointer Variables (2 of 2) • Pointer arithmetic can be very dangerous: • Program can accidentally access memory locations of other variables and change their content without warning - Some systems might terminate the program with an appropriate error message • Always exercise extra care when doing pointer arithmetic © 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 product or service or otherwise on a password-protected website for classroom
  • 20. 20 Dynamic Arrays (1 of 2) • Dynamic array: array created during program execution • Example: int *p; p = new int[10]; *p = 25; //stores 25 in the first memory location p++; //to point to next array component *p = 35; // stores 35 into the second memory location © 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 product or service or otherwise on a password-protected website for classroom
  • 21. 21 Dynamic Arrays (2 of 2) • Can use array notation to access these memory locations • Example: p[0] = 25; p[1] = 35; • Stores 25 and 35 into the first and second array components, respectively • An array name is a constant pointer © 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 product or service or otherwise on a password-protected website for classroom
  • 22. 22 Functions and Pointers • Pointer variable can be passed as a parameter either by value or by reference • As a reference parameter in a function heading, use &: void pointerParameters(int* &p, double *q) { . . . } © 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 product or service or otherwise on a password-protected website for classroom
  • 23. 23 Pointers and Function Return Values • A function can return a value of type pointer: int* testExp(...) { . . . } © 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 product or service or otherwise on a password-protected website for classroom
  • 24. 24 Dynamic Two-Dimensional Arrays • You can create dynamic multidimensional arrays • Examples: int *board[4]; //declares board to be an array of four //pointers wherein each pointer is of type for (int row = 0; row < 4; row++) board[row] = new int[6]; //creates the rows of board int **board; //declares board to be a pointer to a pointer © 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 product or service or otherwise on a password-protected website for classroom
  • 25. 25 Shallow versus Deep Copy and Pointers • Shallow copy: when two or more pointers of the same types point to the same memory • They point to the same data • Danger: deleting one deletes the data pointed to by all of them • Deep copy: when the contents of the memory pointed to by a pointer are copied to the memory location of another pointer • Two copies of 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 product or service or otherwise on a password-protected website for classroom
  • 26. 26 Classes and Pointers: Some Peculiarities (1 of 2) • Example class: class ptrMemberVarType { public: . . . private: int x; int lenP; int *p; }; • Example program statements: ptrMemberVarType objectOne; ptrMemberVarType objectTwo; © 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 product or service or otherwise on a password-protected website for classroom
  • 27. 27 Classes and Pointers: Some Peculiarities (2 of 2) FIGURE 12-13 Objects objectOne and objectTwo © 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 product or service or otherwise on a password-protected website for classroom
  • 28. 28 Destructor • If objectOne goes out of scope, its member variables are destroyed • Memory space of a dynamic array stays marked as allocated, even though it cannot be accessed • Solution: in destructor, ensure that when objectOne goes out of scope, its array memory is deallocated: ptrMemberVarType::~ptrMemberVarType() { delete [] p; } © 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 product or service or otherwise on a password-protected website for classroom
  • 29. 29 Assignment Operator • After a shallow copy: if objectTwo.p deallocates memory space to which it points, objectOne.p becomes invalid FIGURE 12-15 Objects objectOne and objectTwo • Solution: extend definition of the assignment operator to avoid shallow copying of 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 product or service or otherwise on a password-protected website for classroom
  • 30. 30 Copy Constructor (1 of 3) • Default member-wise initialization: • Initializing a class object by using the value of an existing object of the same type • Example: ptrMemberVarType objectThree(objectOne); • Copy constructor: provided by the compiler • Performs this initialization • Leads to a shallow copying of the data if class has pointer member variables © 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 product or service or otherwise on a password-protected website for classroom
  • 31. 31 Copy Constructor (2 of 3) • Similar problem occurs when passing objects by value • Copy constructor automatically executes in three situations: • When an object is declared and initialized by using the value of another object • When an object is passed by value as a parameter • When the return value of a function is an 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 product or service or otherwise on a password-protected website for classroom
  • 32. 32 Copy Constructor (3 of 3) • Solution: override the copy constructor • For classes with pointer member variables, three things are normally done: • Include the destructor in the class • Overload the assignment operator for the class • Include the copy 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 product or service or otherwise on a password-protected website for classroom
  • 33. 33 Inheritance, Pointers, and Virtual Functions (1 of 3) • Can pass an object of a derived class to a formal parameter of the base class type • Compile-time binding: the necessary code to call specific function is generated by compiler • Also known as static binding or early binding • Virtual function: binding occurs at program execution time, not at compile time • Declared with reserved word virtual © 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 product or service or otherwise on a password-protected website for classroom
  • 34. 34 Inheritance, Pointers, and Virtual Functions (2 of 3) • Run-time binding: • Compiler does not generate code to call a specific function: it generates information to enable run-time system to generate specific code for the function call • Also known as late binding or dynamic binding • Note: cannot pass an object of base class type to a formal parameter of the derived class type © 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 product or service or otherwise on a password-protected website for classroom
  • 35. 35 Inheritance, Pointers, and Virtual Functions (3 of 3) • Values of a derived class object can be copied into a base class object • Slicing problem: if derived class has more data members than base class, some data could be lost • Solution: use pointers for both base and derived class objects © 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 product or service or otherwise on a password-protected website for classroom
  • 36. 36 Classes and Virtual Destructors (1 of 2) • Classes with pointer member variables should have the destructor • Destructor should deallocate storage for dynamic objects • If a derived class object is passed to a formal parameter of the base class type, destructor of the base class executes • Regardless of whether object is passed by reference or by value • Solution: use a virtual destructor (base 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 product or service or otherwise on a password-protected website for classroom
  • 37. 37 Classes and Virtual Destructors (2 of 2) • Virtual destructor of a base class automatically makes the destructor of a derived class virtual • After executing the destructor of the derived class, the destructor of the base class executes • If a base class contains virtual functions, make the destructor of the base class virtual © 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 product or service or otherwise on a password-protected website for classroom
  • 38. 38 Abstract Classes and Pure Virtual Functions (1 of 2) • New classes can be derived through inheritance without designing them from scratch • Derived classes: • Inherit existing members of base class • Can add their own members • Can redefine or override public and protected member functions • Base class can contain functions that you would want each derived class to implement • However, base class may contain functions that may not have meaningful definitions in the base 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 product or service or otherwise on a password-protected website for classroom
  • 39. 39 Abstract Classes and Pure Virtual Functions (2 of 2) • Pure virtual functions do not have definitions (bodies have no code) • Example: virtual void draw() = 0; • An abstract class is a class with one or more virtual functions • It can contain instance variables, constructors, and functions that are not pure virtual • It must provide the definitions of the constructor and functions that are not pure virtual © 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 product or service or otherwise on a password-protected website for classroom
  • 40. 40 Address of Operator and Classes (1 of 2) • & operator can create aliases to an object • Example: int x; int &y = x; //x and y refer to the same memory location //y is like a constant pointer variable y = 25; //sets the value of y (and of x) to 25 x = 2 * x + 30; //updates value of x and y © 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 product or service or otherwise on a password-protected website for classroom
  • 41. 41 Address of Operator and Classes (2 of 2) • Address of operator can also be used to return the address of a private member variable of a class • However, if you are not careful, this operation can result in serious errors in the program © 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 product or service or otherwise on a password-protected website for classroom
  • 42. 42 Quick Review (1 of 2) • Pointer variables contain the addresses of other variables as their values • Declare a pointer variable with an asterisk, *, between the data type and the variable • Address of operator (&) returns the address of its operand • Unary operator * is the dereferencing operator • Member access operator (->) accesses the object component pointed to by a pointer © 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 product or service or otherwise on a password-protected website for classroom
  • 43. 43 Quick Review (2 of 2) • Dynamic variable: created during execution • Created using new • Deallocated using delete • Shallow copy: two or more pointers of the same type point to the same memory • Deep copy: two or more pointers of the same type have their own copies of the data • Binding of virtual functions occurs at execution time (dynamic or run-time binding) © 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 product or service or otherwise on a password-protected website for classroom