Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

Chameli Devi Group of Institutions

UNIT-4

BY
MS. HARLEEN KAUR

ASST.PROFFESSOR(CSE)
Unit-4 OOPM
Runtime polymorphism 3

• A virtual function is a member function in the base class


that we expect to redefine in derived classes.

• Basically, a virtual function is used in the base class in


order to ensure that the function is overridden. This
especially applies to cases where a pointer of base
class points to an object of a derived class.
• Now on the basis of object assigned, the respective
class function is called.
4
5
Output 6
Friend Function 7
8
9
10
11
12
13
14
15
16
17
18
19
20
Pointer and its types

 Pointers are the symbolic representations of addresses. They enable


programs to simulate call-by-reference as well as to create and
manipulate dynamic data structures.
 Syntax:
 datatype *var_name;
 int *ptr;
22
output 23
Address of arrays

 An array is a collection of similar data items. All array elements


stored at contiguous memory locations and these elements can be
accessed randomly using indices of an array.
 When the program is compiled, the compiler does not save the
addresses of all of the elements, but only saves the address of the
first element.
 The name of an array usually evaluates to the address of the first
element of the array, so array and &array have the same value .
Basically, “array” is a “pointer to the first element of array” but
“&array” is a “pointer to all elements of the array ”.
25
output 26
27
output 28
29
output 30
31
output 32
This pointer 33

 Every object in C++ has access to its own address through an


important pointer called this pointer. The this pointer is an implicit
parameter to all member functions. Therefore, inside a member
function, this may be used to refer to the invoking object.
This pointer 34
output 35
The address-of operator

 An address-of-operator is a mechanism in C++ that returns the


memory address of a variable. These addresses returned by the
address-of-operator are known as pointers because they "point" to
the variable in memory.
 The address-of-operator is a unary operator represented by an
ampersand (&). It is also known as an address operator
37
output 38
Debugging Pointers

 Uninitialized pointer
 Null pointer
I. Uninitialized pointers
The pointer p is uninitialized and points
to a random location in memory when
declare it. It could be pointing into the
system stack, or the global variables, or
into the program's code space
Output
II Null pointer

 It is always a good practice to assign a NULL value to a pointer variable in


case you do not have an exact address to be assigned.

 This is done at the time of variable declaration. A pointer that is assigned


NULL is called a null pointer.
Output
A pointer to an object

 A pointer to an object acts the same as Pointer to a variable. But


here, in place of the address of the variable, address of the object is
stored.
 For creating a pointer to an object, we should not use data type for
the Pointer. Instead, we need to use the class name for the object
pointer.
Pointer to objects
Output
Function pointer
New and delete operator

 C++ allows us to allocate the memory of a variable or an array in


run time. This is known as dynamic memory allocation.
New Operator
Delete operator:Free the memory
allocated by the new operator.
Output(Garbage value)
Copy constructor & Assignment 60

initialization
61
Dynamic cast(run time information)
Disinheritance(By using virtual
keyword ambiguity is erased
(Hybrid Inheritance)
Output

You might also like