OOP - UNIT4 POINTER and Polymorphism
OOP - UNIT4 POINTER and Polymorphism
A Variable Name
5 Value of A
0x7fffa0757dd4 Address of A
Example:- Int A=5 ;
Int*p;
p=&a;
*p=a;
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:-
*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)
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();