C++ Programming From Problem Analysis To Program Design
C++ Programming From Problem Analysis To Program Design
2
Objectives (continued)
3
Objectives (continued)
4
Pointer Variables
identifier
Examples:
int *p;
char *ch;
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;
Address of Operator (&)
7
Dereferencing Operator (*)
It will print 25
x 25 1300 x 55 1300
&p, p, and *p all have
different meanings.
1750
50
38
14
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;
15
Initializing Pointer Variables
17
Dynamic Variables
18
Operator new
19
Operator new (continued)
20
Think about pointer of characters???
Operator new (continued)
23
Operator delete
24
Operator delete (continued)
Memory leak
25
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:
P
1200 NULL
26
Operations on Pointer Variables
28
Operations on Pointer Variables
(continued)
• Examples: p
int *p, *q;
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
29
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
30
Dynamic Arrays
int *p;
p = new int[10];
1036
31
p 1000
1004
1008
1036
Dynamic Arrays (continued)
33
Dynamic Arrays (continued)
37
Pointers and Function Return
Values
• A function can return a value of type pointer:
int* testExp(...)
{
. . .
}
38
5 10 5
5 10 10
5 10 273185792
5 10
8 8
10 10 10
Dynamic Two-Dimensional Arrays
41
• This statement creates an array of 10 pointers of type int
and assign the address of that array to board.
9 ....
Examples
P1 0012FF7C
P2 0012FF78
10
20 X 0012FF74
ptr2
ptr1
p1 p2 p3
x y z
3 10 5
4 20 0
5 0
0
Shallow versus Deep Copy and
Pointers
• If we execute:
45
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
46
Shallow versus Deep Copy and
Pointers (continued)
• Deep copy: two or more pointers have their
own data
47
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
48
Destructor
50
Assignment Operator
51
Assignment Operator (continued)
52
Copy Constructor
(provided by the compiler).
• ClassName newObject (oldObject);
Means newObject = oldObject
Copy Constructor
55
Copy Constructor (continued)
56
obj1
obj2
error
obj1 obj2
x 30
Copy Constructor (continued)
58
Copy Constructor (continued)
59
Inheritance, Pointers, and Virtual
Functions
• You can pass an object of a derived class to
a formal parameter of the base class type
60
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
63
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
64
Inheritance, Pointers, and Virtual
Functions (continued)
65
Classes and Virtual Destructors
71
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
73
Abstract Classes and Pure Virtual
Functions (continued)
74
A
B C
Y Z
Virtual functions
Class A is Abstract class
77
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
78
Address of Operator and Classes
int &fun(int a)
Output: 2 2 Garbage
4 4 4
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
83
Summary