0% found this document useful (0 votes)
34 views25 pages

Pointer

The document discusses pointers and polymorphism in C++. It covers concepts like pointers, pointer arithmetic, pointers to objects, and this pointer. Example code is provided to demonstrate arithmetic operations on pointers and using this pointer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views25 pages

Pointer

The document discusses pointers and polymorphism in C++. It covers concepts like pointers, pointer arithmetic, pointers to objects, and this pointer. Example code is provided to demonstrate arithmetic operations on pointers and using this pointer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Unit 04 :

Pointers and Polymorphism


in c++

Written by
Unit Outcome 1 : Create C++
programs to perform the given
arithmetic operations using
pointers

Written by
Learning Outcome 1 : student will be
able to use pointers.

Written by
What we will learn today

1. Concept of Pointers Key takeaways


2. Arithmetic operation on pointers Concept of Pointers

Page 4 Maharashtra State Board of Technical Education 4 July 2020


Concept Map

Pointer
Declaration

Working Of
Pointer Pointer
Initialization

Pointer

Memory
Allocation Operation
on Pointer

Page 5 Maharashtra State Board of Technical Education 4 July 2020


Learning Objective/ Key learning

► To use pointer and perform operations on pointers.

► Write program to perform arithmetic operation on pointers.

Page 6 Maharashtra State Board of Technical Education 4 July 2020


Concept Explanation: Pointers

► Every variable is a memory location and every memory location has its address defined
► Address can be accessed using ampersand (&) operator which denotes an address
► C++ tasks are performed more easily with pointers
► C++ tasks, such as dynamic memory allocation, cannot be performed without them
► Pointer is a variable in C++ that holds the address of another variable.
► Pointers have data type just like variables.
► Pointers are variables which display value at the memory address.
► Like any variable or constant, you must declare a pointer before you can work with it
7

Page 7 Maharashtra State Board of Technical Education 4 July 2020


Declaring and initializing pointer
► The variable which store address of integer variable it is called as an integer pointer.

► Similarly if we stores address of character variable it is called as character pointer

► Declaring Pointers
► Syntax:

data_type *pointer_name;
int *ptr; //declaration
8
ptr=&a; //initialization

Page 8 Maharashtra State Board of Technical Education 4 July 2020


C++ Pointer Operators : Reference and Dereference Operators

► Pointer operators used in C++ are :


► ‘value at’ operator
► * is the dereference operator and can be read as “value pointed by”.
► ‘address’ operator
► & is the reference operator and can be read as “address of”.

Pointer Operator

Address Operator Indirection


(&) Operator (*)

Page 9 Maharashtra State Board of Technical Education 4 July 2020


Example By Using Pointer

#include<iostream.h>
void main()
{ ptr a
int a=10;
int *ptr; Value of ptr 1024 10
ptr=&a; 1000 1024
cout<<"\n Value of a:"<<a; //a=10 Value of a

cout<<"\n Value at ptr:"<<*ptr; //*ptr=10


cout<<"\n Address of a:"<<&a; //&a=1024
cout<<"\n Address of a:"<<ptr; //ptr=1024
cout<<"\n Address of ptr:"<<&ptr; //&ptr=1000 Address of Address of ‘a’ (&a)
} ptr

Page 10 Maharashtra State Board of Technical Education 4 July 2020


Pointer Arithmetic

► Operation
► Increment operator (++)
► Decrement operator (–)
► Addition (+)
► Subtraction (-)

► Valid statements
► p2-p1;
► ptr1+1;
► ptr=ptr+2;

► Invalid statements
► p1+p2;
► p2 * p1;
► p2/p1;
Page 11 Maharashtra State Board of Technical Education 4 July 2020
Pointer Arithmetic

► C++ allows pointers to perform some arithmetic operations like :


⮚ A pointer can be incremented (++) or decremented(--).

⮚ Any integer can be added to or subtracted from a pointer.

⮚ One pointer can be subtracted from another.

Page 12 Maharashtra State Board of Technical Education 4 July 2020


Example

► int a=10, b=5;


0000 a
► Assign p1=&a and p2=&b
P1 0001 Value
► So value of p1 will become 0000 (starting address of a) 0002 is 10
► Value of p2 will become 0004 (starting address of b) 0003
0004 b
P2 0005 Value
0006 is 5
► When we perform p1 - p2 0007
► It will not calculate difference of how many bytes fall between the two addresses, but rather how
many elements are there.
► P1+P2 Its not valid as we cannot perform addition of addresses

Page 13 Maharashtra State Board of Technical Education 4 July 2020


Example Of Arithmetic Operation On Pointer

Program code

Output

Page 14 Maharashtra State Board of Technical Education 4 July 2020


What we will learn today

1. Pointer to Object Key takeaways


2. This Pointer Concept Of Pointer To Object

3. Pointer To Derived Class

Page 15 Maharashtra State Board of Technical Education 4 July 2020


Concept Map

Pointer to
Object Pointee derived
class

Pointer This pointer

Page 16 Maharashtra State Board of Technical Education 4 July 2020


Learning Objective/ Key learning

► To use pointer to object.

► To use This pointer.

► Write program using This pointer.

Page 17 Maharashtra State Board of Technical Education 4 July 2020


Concept Explanation: Pointers To Objects

⮚ A pointer can point to an object created by class. ⮚ Syntax:

⮚ Object pointers are useful in creating objects at Class_name * variable name;

run time. Variable_name=&Object_name;

⮚ Object pointers can be used to access public ⮚ Example:

members of an object. Item obj;


Item *ptr;
⮚ We can refer to public member of object using
ptr=&obj;
two ways
obj->getdata();
⮚ dot operator ( . )
obj->putdata();
⮚ arrow operator (->) 18

Page 18 Maharashtra State Board of Technical Education 4 July 2020


Concept Explanation: Pointers To Objects

⮚ As object pointer is an alias of object ⮚ ptr can be used to refer to member


variable, we can also use ptr->getdata();
(*object_pointer).member_function() ⮚ We can also create an array of objects
► We can also create object using pointers using pointers
and new operatora ⮚ item *ptr=new item[10];
► Example: item *ptr=new item; ⮚ This statement creates memory space for
⮚ This statement allocates enough memory an array of 10 objects.
for the data members in the object and
assign address of the memory space to 19

Page 19 ptr. Maharashtra State Board of Technical Education 4 July 2020


This Pointer

► C++ uses a unique keyword called ‘this’ to represent an object that invokes a
member function.
► This unique pointer is automatically passed to a member function when it is invoked.
► ‘this’ is a pointer that always point to the object for which the member function was
called.
► The ‘this’ pointer can be treated like any other pointer to an object.
► For example, the function call
20
A.max () will set the pointer ‘this’ to the address of the object A.
Next time suppose we call B.max(), the pointer ‘this’ will store address of object B.

Page 20 Maharashtra State Board of Technical Education 4 July 2020


Program using This pointer:

If we try to write
x=x;
It will give garbage Value

Output :

Page 21 Maharashtra State Board of Technical Education 4 July 2020


Pointer To Derived Classes

► Pointers can be used to the base class objects ► Example:


as well as objects of derived class. B *cptr; // pointer to B class
B b; //Base object
► Pointers to objects of base class are
D d; // Derived object
compatible with pointers to objects of a cptr=&b; //ptr points to object b
derived class.
► We can make cptr to point to the
► Single pointer variable can be made to point
object d as follows:
objects belonging to different classes.
cptr=&d; //ptr points to object d
► If B is base and D is derived class, then pointer
declared as a pointer to B can also be a pointer

Page 22
to D. Maharashtra State Board of Technical Education 4 July 2020
Pointers to derived class

► Example:
B *cptr; // pointer to B class
B b; //Base object
D d; // Derived object
cptr=&b; //ptr points to object b

► We can make cptr to point to the object d as follows:


► cptr=&d; //ptr points to object d
► Problem is by using cptr to access the public members of the derived class D.
► The pointer cptr can point to both b and d.
► Using cptr, we can access only those members which are inherited from B and not the members that
originally belongs to D.
► If a member of D has the same name as one of the members of B, then any reference to that member
by cptr will always access the base class member.
Page 23 Maharashtra State Board of Technical Education 4 July 2020
Pointer to Derived class

Output :

Page 24 Maharashtra State Board of Technical Education 4 July 2020


Advantages of pointer

► Pointers save the memory.

► Pointers reduce the length and complexity of a program.


► Pointers allow passing of arrays and strings to functions more efficiently.
► Pointers make possible to return more than one value from the function.

► Pointers increase the processing speed.

Page 25 Maharashtra State Board of Technical Education 4 July 2020

You might also like