0% found this document useful (0 votes)
7 views29 pages

OOP - UNIT4 POINTER and Polymorphism

This document covers the concepts of pointers and polymorphism in programming, detailing pointer declaration, arithmetic, and their use with objects. It explains compile-time and run-time polymorphism, including function and operator overloading, as well as the use of the 'this' pointer in class contexts. The document also provides examples and syntax for various pointer operations and their applications in object-oriented programming.

Uploaded by

vrushalikabade36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views29 pages

OOP - UNIT4 POINTER and Polymorphism

This document covers the concepts of pointers and polymorphism in programming, detailing pointer declaration, arithmetic, and their use with objects. It explains compile-time and run-time polymorphism, including function and operator overloading, as well as the use of the 'this' pointer in class contexts. The document also provides examples and syntax for various pointer operations and their applications in object-oriented programming.

Uploaded by

vrushalikabade36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Unit No:-04

Pointers and Polymorphism


In this chapter We are going to
study that following points:-
1. Concepts Of Pointer: Pointer Declaration, Pointer operator, Address
Operator , Pointer Arithmetic.
2. Pointer To Object: Pointer to Object , ’this’ pointer, pointer to derived class.
3. Introduction to Polymorphism , Types of polymorphism
4. Compile Time Polymorphism : Function Overloading ,Revision of
Constructor overloading .
5. Operator Overloading :Rules for Operator overloading , Overloading of
unary and binary operator.
6. Run Time Polymorphism: Virtual Function Rules for Virtual Function, Pure
virtual function.
Concept Of Pointer:-
Q. Define Pointer with their Declaration ?
• Pointer:- Pointer is a variable which used to store the memory address
of another variable.
OR
• Pointer:- Pointer is a variable that holds the memory address of another
variable.
• Syntax:- Pointer Declaration:- Datatype * Pointer Variable.
• Syntax:-Holds Memory Address:- Pointer Variable = &Normal Variable.
• Example:- Int a=5; Variable Declaration
Int *p; Pointer Declaration
P=&a; Assign Address of Variable
Example:-
Int A =5;

A Variable Name

5 Value of A

0x7fffa0757dd4 Address of A
Example:- Int A=5 ;
Int*p;
p=&a;
*p=a;

5 Value of Variable A(*Pointer)

0x7fffa0757dd4 Address of variable A(stored at Pointer)


Pointer Operator & Address
Operator
• Pointer operator(*):- The * Operator Used for declaration of Pointer Variable,
and also used for access the value of normal variable.
• Example Declaration:-Int *ptr; *ptr;.
• Address Of Operator(&):- The Address of operator used for access memory
address of normal variable.
• Example:- Int a=10 ;
int * ptr ;
ptr=&a;
Example:-
• #include<iostream.h>
#include<conio.h>
void main()
{
int a=15;
int *pointer;
clrscr();
pointer=&a;
*pointer=a;
cout<<"VALUE OF A =" <<a<<endl;
cout<<"ADDRESS OF A =" <<&a<<endl;
cout<<"VALUE OF POINTER =" <<*pointer<<endl;
cout<<"ADDRESS OF POINTER =" <<pointer<<endl;
getch();
}
Pointer Arithmetic:-
• Pointer arithmetic that is it’s holds hexadecimal integer value.
• We can perform arithmetic operation on pointer.
• Operations are performs only address not a value.
• Only ptr is used not *ptr .
• Pointer Arithmetic Operations are:-
1. Increment Pointer(++ptr or ptr++)
2. Decrement Pointer (--ptr or ptr --)
3. Add Integer to Integer( ptr+i)
4. Subtract Integer to Integer(ptr-i)
Pointer arithmetic operations are with pointer to array.
Increment Operator (ptr++)(++ptr)
• ++ Operator: - It is referred as increment operator that increments the value
of variable.
• Syntax:- ++ptr OR ptr++;
• Pointer to arithmetic pointer points to next array element.
• If ++ operator is used with pointer variable, then pointer variable points to
next memory address that means pointer increment with respect to size of
the data type used to declare pointer variable.
• Example:- int a[5]={10,20,30,40,50},*ptr; ptr=a[0];
for(i=0;i<5;i++)
{
cout<<*ptr;
ptr++;
}
Description:-

• In the above example, ptr points to memory


location of a[0].
• Increment statement ptr++ increments ptr
by memory size of int i.e 2 bytes and ptr
points to a[1].
Increment Pointer:-
• Example:- Int a[8]={10,20,30,40,50,60,70,80};
int *ptr;
ptr=&a[0]; \\ Pointer To Array

PTR a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]


*PTR
10 20 30 40 50 60 70 80

PTR[0];
*PTR =10;
++PTR , *PTR=20;
PTR++, *PTR=30
Decrement Pointer: (--ptr)(ptr--)
• - - Operator: - It is referred as decrement operator that decrements the value of
variable.
• Syntax:- --ptr; OR ptr--;
• Pointer to arithmetic pointer points to previous array element.
• If - - operator is used with pointer variable, then pointer variable points to
previous memory address that means pointer decrement with respect to size of
the data type used to declare pointer variable.
• Example:- int a[5]={10,20,30,40,50},*ptr; ptr=a[4];
for(i=0;i<5;i--)
{
cout<<*ptr;
ptr--;
}
Description:-

• In the above example, ptr points to memory


location of a[4].
• Decrement statement ptr- - decrements ptr
by memory size of int i.e 2 bytes and ptr
points to a[3].
Decrement Pointer:-
• Example:- Int a[8]={10,20,30,40,50,60,70,80};
int *ptr; PTR[7];
*PTR =80;
ptr=&a[7]; \\ Pointer To Array --PTR , *PTR=70;
PTR--, *PTR=60

PTR a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

*PTR 10 20 30 40 50 60 70 80
Increment Pointer Example:
#include<iostream.h>
#include<conio.h>
void main()
{
int arithmetic[8]={10,20,30,40,50,60,70,80};
int *ptr;
int i;
clrscr();
cout<<" ****The array values are**** " <<endl;
for(i=0;i<8;i++)
{
cout<<arithmetic[i]<<endl;
ptr=&arithmetic[0];
cout<<" value of pointer1:= "<<*ptr<<endl;
ptr++;
cout<<" value of pointer2:= "<<*ptr<<endl;
ptr++;
cout<<" value of pointer3:= "<<*ptr<<endl;
ptr++;
cout<<" value of pointer4:= "<<*ptr<<endl;
ptr++;
cout<<" value of pointer5:= "<<*ptr<<endl;
ptr++;
cout<<" value of pointer6:= "<<*ptr<<endl;
ptr++;
cout<<" value of pointer7:= "<<*ptr<<endl;
ptr++;
cout<<" value of pointer8:= "<<*ptr<<endl;
getch();
}
Decrement Pointer Example:
• #include<iostream.h>
#include<conio.h>
void main()
{
int arithmetic[8]={10,20,30,40,50,60,70,80};
int *ptr;
int i;
clrscr();
cout<<" ****The array values are**** " <<endl;
for(i=0;i<8;i++)
{
cout<<arithmetic[i]<<endl;
}
ptr=&arithmetic[7];
cout<<" value of pointer1:= "<<*ptr<<endl;
ptr--;
cout<<" value of pointer2:= "<<*ptr<<endl;
ptr--;
cout<<" value of pointer3:= "<<*ptr<<endl;
ptr--;
cout<<" value of pointer4:= "<<*ptr<<endl;
ptr--;
cout<<" value of pointer5:= "<<*ptr<<endl;
ptr--;
cout<<" value of pointer6:= "<<*ptr<<endl;
ptr--;
cout<<" value of pointer7:= "<<*ptr<<endl;
ptr--;
cout<<" value of pointer8:= "<<*ptr<<endl;
getch();
}
Add Integer to Integer( ptr+i)

• Pointer Subtraction pointer points to ith element of an array from


current position onwards .
• It is jump to pointer from one element to another element in forward
position.
• Syntax:- ptr= ptr +i;
• Where I is an integer
• Example:- ptr = &list [0];
• ptr=ptr+2;
• ptr= ptr+3;
Add Integer to Integer( ptr+i)
• Example:- Int a[8]={10,20,30,40,50,60,70,80};
int *ptr;
ptr=&a[0]; \\ Pointer To Array
PTR a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
*ptr 10 20 30 40 50 60 70 80
Ptr[0]
*ptr=10
Ptr+1; *ptr=20
Ptr+3;*ptr=50
#include<iostream.h>
#include<conio.h>
void main()
{
int a[8]={10,20,30,40,50,60,70,80};
int *ptr;
ptr=&a[0];
int i;
clrscr();
cout<<"array elements are:-"<<endl;
for (i=0;i<8;i++)
{
cout<<a[i]<<endl;
}
cout<<"value of ptr:="<<*ptr<<endl;
ptr= ptr+2;
cout<<"value of pointer 1:=" <<*ptr <<endl;
ptr= ptr+5;
cout<<"value of pointer 1:=" <<*ptr <<endl;
getch();
}
Subtract Integer to Integer( ptr-
i)
• Pointer addition pointer points to ith element of an array from current position
backward .
• It is jump to pointer from one element to another element in backward
direction .
• Syntax:- ptr= ptr -i;
• Where I is an integer.
• Example:- ptr = &list [7];
• ptr=ptr-1;
• ptr= ptr-3;
Subtract Integer to Integer( ptr-i)
• Example:- Int a[8]={10,20,30,40,50,60,70,80};
int *ptr;
ptr=&a[7]; \\ Pointer To Array
PTR a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
*ptr 10 20 30 40 50 60 70 80

Ptr[7]
*ptr=80
Ptr-2; *ptr=60
Ptr-2;*ptr=40
#include<iostream.h>
#include<conio.h>
void main()
{
int a[8]={10,20,30,40,50,60,70,80};
int *ptr;
ptr=&a[7];
int i;
clrscr();
cout<<"array elements are:-"<<endl;
for (i=0;i<8;i++)
{
cout<<a[i]<<endl;
}
cout<<"value of ptr:="<<*ptr<<endl;
ptr= ptr-2;
cout<<"value of pointer 1:=" <<*ptr <<endl;
ptr= ptr-5;
cout<<"value of pointer 1:=" <<*ptr <<endl;
getch();
}
Pointer to Object:-
• Object to pointer are useful in creating object at runtimes.
• We can also use the pointer to object to access the public member
function of class.
• Syntax:-
• class name Object name;
• class name *pointer name;
• pointer name =&object name;
• pointer name-> member function name ();
Programmatic flow using pointer to
object:-
class Base class name
{ Data Member();
Member Function();
};
class Derived class name: Visibility Mode base class name
{ Data member();
member function();
};
return type main function(){
class name Object name;
class name *pointer name;
pointer name =&object name;
pointer name-> member function name ();
}
This Pointer:-
• C++ uses a unique keyword called ‘this’ to represent the object that invoke or
called the member function .
• The unique operator automatically passed the member function when it is called
or invoked.
• It is always points the member function of class .
• We use arrow operator(->) to show the this keyword.
• When we use the this keyword so compiler should understand properly about
local and instant variable have same name.
• Example:- int a, b;
• A(int a, int b) Instant Variable
• { Local variable
• this a->a; this b->b;}
Pointer to derived class
• The pointer to derived class are used to pointing the object of derived
class.
• This pointer of base class will be able to access the function variables
of own class and can still points to derived class object.
• By making the base class pointers and passing the derived class
objects address to the base class pointer so we access the member
function of base class and points the function of derived class.
Programmatic flow
• Class Base class name{
data member();
member function();
};
Class Derived class name : visibility modes base class name
{ data member()
member function(); };
return type main function() {
base class name * pointer name;
derived class name object name;
pointer name=& object name;
pointer->base class member function();

You might also like