Download presentation
Presentation is loading. Please wait.
1
5th Chapter Pointers in C++
2
What is pointer Every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory. A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is: datatype *var-name;
3
Example: int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character
4
There are few important operations, which we will do with the pointers.
(a) we define a pointer variables (b) assign the address of a variable to a pointer (c) finally access the value at the address available in the pointer variable by using unary operator * that returns the value of the variable
5
Pointer variable consist of two parts
1. the pointer operator (*) 2. The Address operator (&) Syntax to create pointer variable: datatype *ptr_name; How to assign address to pointer: ptr_name=&variable_name
6
Example: int a=10; int *p; p=&a; Suppose address location of a is 1000 then p=1000 *p=10 Note:if we use * sign along with pointer then it refers to value stored at that address location
7
*p 10 1000 address location p
8
Pointer Arithmetic As we understood pointer is an address which is a numeric value; therefore, we can perform arithmetic operations on a pointer just as we can a numeric value. There are four arithmetic operators that can be used on pointers: ++ //increment -- //decrement + //addition - //subtraction
9
Pointer to Array Array is the collection of elements of a same data type. Syntax to create Pointer to array int a[5]; int *ptr; ptr=&a[0] or ptr=a;
10
Following array operations can be performed using pointer
1.Insertion 2.deletion 3.searching
11
Pointer to object Earlier we have seen pointer can be created for the varilaes types of variables of inbuild type.(Eg.int,char) Same way we can declare pointer for user defined data type that is class. For Eg: class A is defined we have to create pointer of this classtype Synatx: class_name *pointervariable; Eg: A *ptr;
12
Another way Syntax: class_name *pointervarname=new classname; Eg: A *ptr=new A;
13
How to invoke/call class method using pointer
class item { public: int a; void getdata() cout<<”\n Enter A”; cin>>a; } void putdata() { cout<<”\n A=”<<a; } };
14
Using OBJECT Using POINTER void main() void main() { { item i;
i.getdata(); i.putdata(); getch(); } Using POINTER void main() { item *p; p->getdata(); p->putdata(); gecth(); } EQUAL
15
this pointer C++ uses unique keyword called this to represent an object that invokes/calls a member function. this is a pointer that points to the object for which this function was called. Eg: if we call max function using object A then A.max() will set the pointer this to the address of the object A.
16
Class ABC { int a; void set(); }; Private variable can be used directly inside a member function like void set a=123; }
17
C++ allows us to use short hand operator i. e
C++ allows us to use short hand operator i.e. A=123 which is equvalent to this->a=123 One important application of the pointer is to return the object it points to means we can retuen object along with all its properties. Eg. person & person::greater(person &x) { if x.age>age; return x; else return *this; }
18
Pointer To Derived Class
We can use pointer not olny to the base class object but also to the object of derived class. A single pointer variable can be mase to point to objects belonging to different class Suppose B is base class and D is derived class form B,then pointer declared as a pointer to B can also be pointer to D.
19
Example B *p1; //pointer object B b1 //base class object D d1 //derived class object p1=&b1; We can make p1 top point to the objectd1 as: p1=&d1;
20
However ,there is a problem in using p1 to access the public memebers of the derived class D.
Using p1(pointer of base class) we can access olny those members which are inherited from B and not the members that originally belong to D In case a member of D has the same name as one of the member of B. Then any reference to that member by p1 will always access the base class member.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.