C++ Programming: From Problem Analysis to Program Design, Fourth Edition
-
Upload
hyacinth-patton -
Category
Documents
-
view
34 -
download
5
description
Transcript of C++ Programming: From Problem Analysis to Program Design, Fourth Edition
![Page 1: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/1.jpg)
C++ Programming: From Problem Analysisto Program Design, Fourth Edition
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
![Page 2: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/2.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2
Objectives
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 operator and the
dereferencing operator• Discover dynamic variables
![Page 3: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/3.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 3
Objectives (continued)
• Explore how to use the new and delete operators to manipulate dynamic variables
• Learn about pointer arithmetic• Discover dynamic arrays• Become aware of the shallow and deep
copies of data• Discover the peculiarities of classes with
pointer member variables
![Page 4: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/4.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4
Objectives (continued)
• Learn about virtual functions• Examine the relationship between the
address of operator and classes• Become aware of abstract classes
![Page 5: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/5.jpg)
Pointer Variables
• Pointer variable: content is a memory address
• Declaring Pointer Variables: Syntax
Examples:
int *p;
char *ch;
identifier
![Page 6: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/6.jpg)
Pointer Variables (continued)
• These statements are equivalent
int *p; int* p; int * p;• In the statement
int* p, q;
only p is the pointer variable, not q; here q is an int variable
• To avoid confusion, attach the character * to the variable name
int *p, q;• The following statement declares both p and q to be pointer
variables of the type int.
int *p, *q;
![Page 7: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/7.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 7
Address of Operator (&)
• The ampersand, &, is called the address of operator
• The address of operator is a unary operator that returns the address of its operand
![Page 8: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/8.jpg)
Dereferencing Operator (*)
• C++ uses * as the binary multiplication operator and as a unary operator
• When used as a unary operator, *
− Called dereferencing operator or indirection operator
− Refers to object to which its operand (that is, a pointer) points
![Page 9: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/9.jpg)
The following statement prints the value stored in the memory space pointed to by p, which is the value of x.
The following statement stores 55 in the memory location pointed to by p—that is, in x.
P
x
1300 1200
1300
P
x
1300
55
1200
1300
It will print 25
25
![Page 10: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/10.jpg)
&p, p, and *p all have different meanings.
• &p means the address of p—that is, 1200
• p means the content of p (1800).
• *p means the content (24) of the memory location (1800) pointed to by p (that is, pointed to by the content of memory location 1200).
![Page 11: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/11.jpg)
Example 14-1
&p= 1400
P= ???
*p= Undefined
&x= 1750
X= ???
&p= 1400
P= ???
*p= Undefined
&x= 1750
X= 50
&p= 1400
P= 1750
*p= 50
&x= 1750
X= 50
&p= 1400
P= 1750
*p= 38
&x= 1750
X= 38
50
1750
38
![Page 12: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/12.jpg)
1. A declaration such as int *p; allocates memory for p only, not for *p.
2. Assume the following:int *p; int x; Then,a. p is a pointer variable. b. The content of p points only to a memory location of
type int.c. Memory location x exists and is of type int. Therefore,
the assignment statement p = &x;
is legal. After this assignment statement executes, *p is valid and meaningful.
More About Pointers
![Page 13: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/13.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 13
Classes, Structs, and Pointer Variables
• You can declare pointers to other data types:
− student is an object of type studentType; studentPtr is a pointer variable of type studentType
![Page 14: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/14.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 14
Classes, Structs, and Pointer Variables (continued)
• 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 (->)
![Page 15: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/15.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 15
Classes, Structs, and Pointer Variables (continued)
• The syntax for accessing a class (struct) member using the operator -> is:
• Thus,(*studentPtr).gpa = 3.9;
is equivalent to:
studentPtr->gpa = 3.9;
![Page 16: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/16.jpg)
![Page 17: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/17.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17
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 using the constant value 0
• Called the null pointer• Example: p = 0;
− Or, use NULL named constant: p = NULL;− The number 0 is the only number that can be
directly assigned to a pointer variable
![Page 18: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/18.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 18
Dynamic Variables
• Dynamic variables: created during execution• C++ creates dynamic variables using pointers• Two operators, new and delete, to create
and destroy dynamic variables− new and delete are reserved words
![Page 19: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/19.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 19
Operator new
• new has two forms:
− where 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 address of the allocated memory
• The allocated memory is uninitialized
![Page 20: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/20.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 20
Operator new (continued)
• The statement: p = &x; − Stores address of x in p
• However, no new memory is allocated
• The statement: p = new int;− Creates a variable during program execution
somewhere in memory, and stores the address of the allocated memory in p
• To access allocated memory: *p
![Page 21: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/21.jpg)
![Page 22: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/22.jpg)
Think about pointer of characters???
![Page 23: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/23.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 23
Operator new (continued)
• new allocates memory space of a specific type and returns the (starting) address of the allocated memory space
• If new is unable to allocate the required memory space, it throws bad_alloc exception− If this exception is not handled, it terminates
the program with an error message
![Page 24: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/24.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 24
Operator delete
![Page 25: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/25.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 25
Operator delete (continued)
Memory leak
![Page 26: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/26.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition26
Operator delete (continued)• To avoid memory leak, when a dynamic variable is no
longer needed, destroy it-----Deallocate its memory• delete is used to destroy dynamic variables• Syntax:
• Tip: to avoid dangling pointers, set variable to NULL afterwards
1200 1300 130012
P
1200 1300 1300
P
Now 1300 is available to be used later on…
1200 NULL
P
![Page 27: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/27.jpg)
![Page 28: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/28.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 28
Operations on Pointer Variables
• 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
![Page 29: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/29.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 29
Operations on Pointer Variables (continued)
• Examples:int *p, *q;
p = q;
− In this case, p == q will evaluate to true, and p != q will evaluate to false
int *p
double *q;
− In this case, q++; increments value of q by 8, and p = p + 2; increments value of p by 8
p
q
![Page 30: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/30.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 30
Operations on Pointer Variables (continued)
• Pointer arithmetic can be very dangerous− The program can accidentally access the
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
![Page 31: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/31.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 31
Dynamic Arrays
• Dynamic array: array created during the execution of a program
• Example:int *p;
p = new int[10];
*p = 25;
p++; //to point to next array component
*p = 35;
stores 25 into the first memory location
stores 35 into the first memory location
p 10001004
1036
![Page 32: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/32.jpg)
p 1000
1004
1036
1008
![Page 33: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/33.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 33
Dynamic Arrays (continued)
• C++ allows us to use array notation to access these memory locations
• The statements:p[0] = 25;
p[1] = 35;
store 25 and 35 into the first and second array components, respectively
![Page 34: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/34.jpg)
![Page 35: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/35.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 35
Dynamic Arrays (continued)
• The value of list (1000) is constant− Cannot be altered during program execution− The increment and decrement operations
cannot be applied to list
• If p is a pointer variable of type int, then:p = list;copies the value of list, the base address of the array, into p− We can perform ++ and -- operations on p
• An array name is a constant pointer
![Page 36: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/36.jpg)
![Page 37: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/37.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 37
Functions and Pointers
• A pointer variable can be passed as a parameter either by value or by reference
• To make a pointer a reference parameter in a function heading, use &:void example(int* &p, double *q){
. . .}
![Page 38: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/38.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 38
Pointers and Function Return Values
• A function can return a value of type pointer:
int* testExp(...){
. . .}
![Page 39: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/39.jpg)
5 10 5
5 10 10
5 10 273185792
![Page 40: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/40.jpg)
5 10
8 8
10 10 10
![Page 41: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/41.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 41
Dynamic Two-Dimensional Arrays
• You can create dynamic multidimensional arrays
• Examples:
declares board to be an array of four pointers wherein each pointer is of type int
creates the rows of board
declares board to be a pointer to a pointer
![Page 42: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/42.jpg)
• This statement creates an array of 10 pointers of type int and assign the address of that array to board.
• This for loop creates the columns of board. • To access the components of board you can use the
array subscripting notation.board
.
.
.
. . . .
. . . .
. . . .
. . . .
0 1 14
0
1
9
![Page 43: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/43.jpg)
Examples
P1 0012FF7C
P2 0012FF78
X 0012FF742010
![Page 44: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/44.jpg)
3
4
5
10
20
5
0
0
0
x y z
ptr2
p1 p2 p3
ptr1
![Page 45: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/45.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 45
Shallow versus Deep Copy and Pointers
• Assume some data is stored in the array:
• If we execute:
![Page 46: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/46.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 46
Shallow versus Deep Copy and Pointers (continued)
• Shallow copy: two or more pointers of the same type point to the same memory− They point to the same data
![Page 47: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/47.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 47
Shallow versus Deep Copy and Pointers (continued)
• Deep copy: two or more pointers have their own data
![Page 48: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/48.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 48
Classes and Pointers: Some Peculiarities
There are three things to take care when the class uses pointer members:
1. Destructor to delete the dynamic array
2. Overload the assignment operator
3. Override the Copy constructor
![Page 49: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/49.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 49
Destructor
• If objectOne goes out of scope, the member variables of objectOne are destroyed− The memory space of the dynamic array
would stay marked as allocated, even though it cannot be accessed
![Page 50: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/50.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 50
Destructor (continued)
• Solution:− Put the necessary code in the destructor to
ensure that when objectOne goes out of scope, the memory of the array is deallocated
![Page 51: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/51.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 51
Assignment Operator
![Page 52: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/52.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 52
Assignment Operator (continued)
• If objectTwo.p deallocates memory space to which it points, objectOne.p becomes invalid
• Solution: extend definition of the assignment operator to avoid shallow copying of data (Ch15)
![Page 53: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/53.jpg)
Copy Constructor (provided by the compiler).
• ClassName newObject (oldObject);
Means newObject = oldObject
![Page 54: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/54.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 54
Copy Constructor
• This initialization is called the default member-wise initialization− Initialization due to the constructor, called the
copy constructor (provided by the compiler)
![Page 55: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/55.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 55
Copy Constructor (continued)
• Default initialization leads to shallow copying of data
• Similar problem occurs when passing objects by value:
![Page 56: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/56.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 56
Copy Constructor (continued)
• Copy constructor automatically executes in three situations:− When an object is declared and initialized by
using the value of another object
− When, as a parameter, an object is passed by value
− When the return value of a function is an object
![Page 57: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/57.jpg)
30
errorobj1 obj2
x
x
obj2
obj1
![Page 58: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/58.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 58
Copy Constructor (continued)
• Solution: properly define copy constructor
![Page 59: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/59.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 59
Copy Constructor (continued)
• 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
![Page 60: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/60.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 60
Inheritance, Pointers, and Virtual Functions
• You can pass an object of a derived class to a formal parameter of the base class type
![Page 61: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/61.jpg)
![Page 62: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/62.jpg)
![Page 63: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/63.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 63
Inheritance, Pointers, and Virtual Functions (continued)
• For both statements (Lines 6 and 7), member function print of baseClass was executed− Because the binding of print, in the body
of callPrint, occurred at compile time• Compile-time binding: the necessary code to
call a specific function is generated by the compiler− Also known as static binding
![Page 64: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/64.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 64
Inheritance, Pointers, and Virtual Functions (continued)
• How can we avoid this problem? − Virtual functions (reserved word virtual)
• Virtual function: binding occurs at program execution time, not at compile time− This kind of binding is called run-time binding
• 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 dynamic binding
![Page 65: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/65.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 65
Inheritance, Pointers, and Virtual Functions (continued)
![Page 66: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/66.jpg)
![Page 67: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/67.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 67
Classes and Virtual Destructors
• Classes with pointer member variables should have the destructor− Destructor can be designed to 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)
![Page 68: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/68.jpg)
![Page 69: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/69.jpg)
![Page 70: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/70.jpg)
Virtual functions for class B:
fun1 and fun2
Virtual functions for class A:
fun2
Virtual functions for class C:
fun1 and fun2 and fun3
![Page 71: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/71.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 71
Classes and Virtual Destructors (continued)
• The 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
![Page 72: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/72.jpg)
![Page 73: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/73.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 73
Abstract Classes and Pure Virtual Functions
• Through inheritance, we can derive new classes without designing them from scratch− Derived classes inherit existing members of
base class, can add their own members, and also redefine or override public and protected member functions
− Base class can contain functions that you would want each derived class to implement
• Base class may contain functions that may not have meaningful definitions in the base class
![Page 74: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/74.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 74
Abstract Classes and Pure Virtual Functions (continued)
• To make them pure virtual functions:
![Page 75: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/75.jpg)
Y
Z
B C
A
![Page 76: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/76.jpg)
Virtual functions Class A is Abstract class
Class B is non abstract class
![Page 77: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/77.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 77
Abstract Classes and Pure Virtual Functions (continued)
• Abstract class: contains one or more pure virtual functions
You cannot create objects of an abstract class
![Page 78: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/78.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 78
Abstract Classes and Pure Virtual Functions (continued)
• If we derive rectangle from shape, and want to make it a nonabstract class:− We must provide the definitions of the pure
virtual functions of its base class
• Note that an abstract class can contain instance variables, constructors, and functions that are not pure virtual− The class must provide the definitions of
constructor/functions that are not pure virtual
![Page 79: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/79.jpg)
![Page 80: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/80.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 80
Address of Operator and Classes
• & operator can create aliases to an object• Consider the following statements:
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 the value of x
and hence of y
![Page 81: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/81.jpg)
8 8 212 2 5
![Page 82: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/82.jpg)
4 4 4
int fun(int &a) //error
int &fun(int a)
Output: 2 2 Garbage
![Page 83: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/83.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 83
Address of Operator and Classes (continued)
• The 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
11
![Page 84: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/84.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 84
Summary
• 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
• & is called the 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
![Page 85: C++ Programming: From Problem Analysis to Program Design, Fourth Edition](https://reader031.fdocuments.us/reader031/viewer/2022032206/56813137550346895d97a958/html5/thumbnails/85.jpg)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 85
Summary (continued)
• 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
• Can pass an object of a derived class to a formal parameter of the base class type
• Binding of virtual functions occurs at execution time (dynamic or run-time binding)