Object Oriented Programming with C++
UNIT-III
Mr.V.Yuvaraj
Assistant Professor – Department of Computer Applications
Dr. N.G.P. ARTS AND SCIENCE COLLEGE
Dr. N.G.P.-KALAPATTI ROAD
COIMBATORE-641 048
Tamil Nadu, India
Mobile: +917502919891,
E-mail: [email protected]
Dr. NGPASC
COIMBATORE | INDIA
Pointer to Pointer
• A pointer variable is a location in memory.
• Can we store the address of this memory
location in another memory location?
• The answer is yes.
• We can have new types: pointer to pointer
to int, pointer to pointer to double, and so on.
Dr. NGPASC
COIMBATORE | INDIA
Pointer to Pointer
Dr. NGPASC
COIMBATORE | INDIA
Arrays and Pointers
• When we declare an array named arr of type t and of
capacity N, the system creates N memory locations
of type t. The system then creates a constant pointer
of type t that is pointed to the first element the
address of all the elements. The address of the
element at index 0 is arr (or arr + 0); the address of
the element at index 1 is arr + 1; and so on.
Dr. NGPASC
COIMBATORE | INDIA
Arrays and Pointers
Dr. NGPASC
COIMBATORE | INDIA
Arrays and Pointers
Dr. NGPASC
COIMBATORE | INDIA
Arrays and Pointers
Dr. NGPASC
COIMBATORE | INDIA
Arrays and Pointers
• This means that arr[0] is the same as *(arr + 0), but
we must be careful to use parentheses because the
asterisk operator has priority over the addition
operator, and using parentheses will allow us to
change this.
• It is also interesting that each address increases by 4
bytes, which demonstrates that the size of an integer
is 4.
Dr. NGPASC
COIMBATORE | INDIA
Arrays and Pointers
Dr. NGPASC
COIMBATORE | INDIA
Arrays and Pointers
Dr. NGPASC
COIMBATORE | INDIA
UNIT-IV
• Polymorphism gives us the ability to write several
versions of a function, each in a separate class. Then,
when we call the function, the version appropriate for
the object being referenced is executed.
Dr. NGPASC
COIMBATORE | INDIA
• Condition for Polymorphism:
For polymorphism, we need pointers (or references),
we need exchangeable objects, and we need virtual
functions.
• Pointers or References:
– We can define a pointer (or a reference) that can
point to the base class; we can then let the pointer
point to any object in the hierarchy. For this
reason, the pointer and reference variables are
sometimes referred to as polymorphic variables.
Dr. NGPASC
COIMBATORE | INDIA
• Exchangeable Objects
– We need plug-compatible objects that play the role of the
devices. An object in an inheritance hierarchy plays this
role.
• Virtual Functions
– We need something to play the role of power given to
different devices that perform different tasks. This is done
in C++ using virtual functions, which are modified by the
keyword virtual. For example, we can have a print
function in all classes, all named the same but each printing
differently.
Dr. NGPASC
COIMBATORE | INDIA
Dr. NGPASC
COIMBATORE | INDIA
Dr. NGPASC
COIMBATORE | INDIA
Dr. NGPASC
COIMBATORE | INDIA
• Binding:
• Binding is an issue related to polymorphism. While there is no
extra code required to assure that binding is correct, we should
understand what happens.
• A function is split into two entities: function call and function
definition.
• Binding here means the association between the function call
—for example, print()—and the function body—for example,
void print {…}.
Dr. NGPASC
COIMBATORE | INDIA
Static Binding
• The term static binding (sometimes called compile-time binding or early
binding) occurs when we have more than one definition for a function, but
the compiler knows which version of the definition is to be used when the
program is compiled.
• We know that each definition of the function is stored somewhere in
memory, and the compiler knows where it is when it encounters the
function call.
• This may happen, for example, when the function is called by its
corresponding object, as shown below
• person.print ();
• student.print ();
Dr. NGPASC
COIMBATORE | INDIA
• Dynamic Binding
• Static binding is simple, but it is not always possible. We use
polymorphism for dynamic binding (also called late binding
or run-time binding), which means that we must bind a call to
the corresponding definition during run time.
• This is needed when the object is not known during the
compilation.
Dr. NGPASC
COIMBATORE | INDIA
• Run-Time Type Information (RTTI):
• When working with hierarchy of classes, sometimes we need
to know the type of the object we are dealing with or
sometimes we want to change the type of the object.
• Using typeid Operator
• If we want to find the type of the object at run time, we can
use the <typeinfo> header to access an object of the class
type_info (note that the class name has an underscore, but the
header name does not).
Dr. NGPASC
COIMBATORE | INDIA
• The only way to create an object of type_info is by using an
overloaded operator named typeid.
• We can create an object of type_info by passing an expression
to the operator typeid that can be evaluated as a type; for
example, typeid (5), typeid (object_name), typeid (6 + 2), and
so on.
Dr. NGPASC
COIMBATORE | INDIA
Dr. NGPASC
COIMBATORE | INDIA
• Using a Dynamic-Cast Operator:
• We have seen that in a polymorphic relationship we can upcast
a pointer, which means that we make a pointer in the derived
class so that it points to the base class as shown below:
• Person* ptr1 = new Student
• The pointer returned from the new operator is a pointer to a
Student object, but we assign it to a pointer that points to a
Person object (the pointer is upcast).
Dr. NGPASC
COIMBATORE | INDIA
• C++ also allows us to downcast a pointer to make it point to
an object in the lower order of hierarchy. This can be done
using a dynamic_cast operator.
• Student* ptr2 = dynamic_cast <Student*) (ptr1)
• This casting proves that the Student class is a class derived
from the Person class because ptr1 can be downcast to ptr2
Dr. NGPASC
COIMBATORE | INDIA
Dr. NGPASC
COIMBATORE | INDIA 25