SlideShare ist ein Scribd-Unternehmen logo
1 von 43
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

Weitere ähnliche Inhalte

Was ist angesagt?

9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 
Spring AOP
Spring AOPSpring AOP
Spring AOPAnushaNaidu
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop conceptsRushiBShah
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath CommunityRohit 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.pptxRohit 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.pptxRohit 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 JDK8IndicThreads
 
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 RitterJAXLondon2014
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationGregory Solovey
 
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 functionalMarian Wamsiedel
 

Was ist angesagt? (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
 

Ă„hnlich wie 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 Expressionspullaravikumar
 
Python Fundamentals
Python FundamentalsPython Fundamentals
Python Fundamentalspullaravikumar
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry 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 ProjectsAmitaSuri
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdfTanayKashid
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentationdgdotson
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationdgdotson
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk MitigationDr. 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 7Steve Guinan
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementDr. Ahmed Al Zaidy
 
Whitman_Ch06.pptx
Whitman_Ch06.pptxWhitman_Ch06.pptx
Whitman_Ch06.pptxSiphamandla9
 
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 IDr. 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 Processmshellman
 
Chapter 3 Basic Cryptography
Chapter 3 Basic CryptographyChapter 3 Basic Cryptography
Chapter 3 Basic CryptographyDr. Ahmed Al Zaidy
 
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.docxsleeperharwell
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentationdgdotson
 
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 5Steve Guinan
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyDr. Ahmed Al Zaidy
 

Ă„hnlich wie 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
 
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
 
Chapter 3 Basic Cryptography
Chapter 3 Basic CryptographyChapter 3 Basic Cryptography
Chapter 3 Basic Cryptography
 
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
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and Technology
 

Mehr von Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10Terry Yoast
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08Terry Yoast
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07Terry Yoast
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06Terry Yoast
 

Mehr von 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
 

KĂĽrzlich hochgeladen

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 

KĂĽrzlich hochgeladen (20)

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 

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