Core c++ concepts of Pointers

Pointer to  Function.

As we use pointer to carry address of variable,also we discussed  pointer to array ,where we assigned pointer to array(pointer pointing to first address of array), similarly pointer can also be used to point to  function by carrying address of the function it points.

Declaration of Function Pointer


data_type(*ptr_name)(arg_list)


data type: data type /return type of member function.

*ptr name: name of the pointer which is pointing to function.

arg_list: List of arguments that function is having.

int(*ptr)(int,int)

Focusing on a brief introduction of using pointer to function:

First before using concept of pointer to function,we will just discuss a small problem of just using concept of member function to add two numbers without using pointers.

#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
int x=5,y=6,z;
int add(int,int);//function prototype
z=add(x,y);
cout<<"result on addition="<<z;
getch();
return 0;
}
int add(int x,int y)
{
    return (x+y);
}

Using same program using pointer to member function.

#include<conio.h>
#include<iostream>
using namespace std;
int add(int,int);//function prototype
int (*ptr)(int,int);//pointer to function
int main()
{
int x=5,y=6,z;
ptr=&add;
z=(*ptr)(x,y);
cout<<"result on addition="<<z;
getch();
return 0;
}
int add(int x,int y)
{
    return (x+y);
}

Why use pointer to Member function!!

1.To select a function dynamically at Runtime.

2.Function to pointers cannot be dereferenced.

3.Here function is passed using pointer.

4.Pointer to function is also called as Call back function and is used in C++ for Dynamic Binding.

Pointer to class data member(Pointer to Class).

Basically we use this method to access data members and member function of class by using pointer.This can be done by making pointer object of class and assigning pointer to Address of  object of class.While using concept of pointer to data member, the data member or member function  should be public.

Following declaration represents pointer as an object of class.Means here we declares pointer in the same manner as we declare object of class.

class Sum
{
private:
...............
...............
public:
...............
...............
};
main()
{
class sum *ptr;
}

Definition of pointer to class

ptr=&object;

There are two ways to use members of class using pointer.

1.First Method :Data member of the class can be accessed as (*) operator i.e accessing class members by using indirection operator.

Always when we use (*) operator with pointer it should be kept in parenthesis as the precedence of (.) operator is highest than(*) indirection operator.

Wrong method when using *pointer_name.member_name;


(*pointer_name).member_name;


2.Second Method:Data members of the class can also be accessed as (->) operator i.e dash sign followed by greater than sign.


Pointer_name->member_name;


//pointer to class variable by assigning object address to pointer
#include<iostream>
using namespace std;

class sum
{public:
    int a=10,b=20;
};
int main()
{
    sum obj;
    sum *ptr;
    ptr=&obj;//pointer to object
    cout<<"calling through object"<<obj.a+obj.b<<endl;
    cout<<"calling through pointer using arrow operator "<<ptr->a+ptr->b<<endl;//calling by pointer using arrow operator
    cout<<"calling by pointer  using .operator" <<(*ptr).a+(*ptr).b<<endl;//calling by pointer  using .operator
    return 0;
}

In above program we declared pointer as we declare object of class.Again this is just is declaration of point.Initialization of a pointer can be done by assigning address of object to pointer.As discussed above calling of member by pointer can be done through (structure)arrow  operator or by indirection operator(*).

Pointer to Data member of class by assigning  pointer,address of class.

We can also access Data member of class by assigning address of class to pointer.Lets discuss the declaration of pointer first

Declaration.


 

 

 

 

Definition

 

 

//pointer to data member of class
#include<iostream>
using namespace std;
class sum
{public:
int a=10,b=20;
};
main()
{sum obj;
int sum::*ptr=&sum::a;//pointer to data member
int sum::*ptr1=&sum::b;
cout<<"sum of a and b is:"<<obj.*ptr+obj.*ptr1; 
return 0;
}

Pointer to member function of class.

Pointer to member function of class is same as we use pointer to array ,pointer to object and so on.

Declaration of pointer to member function:

Definition of pointer to member function:

 

Declaration and definition can be done in same line also.


void (sum::*ptr)()=&sum::display;


 

//ptr to member function of class
#include<iostream>
using namespace std;
class sum
{public:
int a=10,b=20;
void display()
{cout<<a+b;
}
};
main()
{sum obj;
void (sum::*ptr)()=&sum::display;//pointer to member function
(obj.*ptr)();
return 0;
}

 

Compiled program of pointer to class,pointer to object and pointer to data member of class.

#include<iostream>
#include<conio.h>
using namespace std;
class Data
{
 public:
 int a;
 void input() {cout<<"nEnter a ";cin>>a;}
 void print() { cout << "na is "<< a<<endl; }
};
int main()
{
 Data d, *dp;
 dp = &d;     // pointer to object
  cout<<"pointer to object  n";
 d.input();   //enter value in d obj
 (*dp).print();   // access value using pointer sec method dp->print();
 cout<<"pointer to data member :a n";
 int Data::*ptr=&Data::a;   // pointer to data member 'a'
 d.*ptr=10;
 d.print();
 cout<<"pointer to data member :a - Method 1n";
 dp->*ptr=20;
 dp->print();
 cout<<endl;
 cout<<"pointer to data member :a - Method 2n";
  (*dp).*ptr=200;
(*dp).print();
cout<<"npointer to member function : print() n";
 void (Data::*ptr1)()= &Data::print;   // pointer to fnc
 d.input();
 //d->print()
 (d.*ptr1)();


}

this pointer

Just to display address of objects using this pointer

//this pointer  to access the address  of class
//this keyword is local variable available in the body of every non-static member function.
#include<iostream>
using namespace std;

class Add
{public:
    void add()
    {cout<<"objects address"<<this<<endl;
    }
};
int main()
{
    Add obj1,obj2,obj3,obj4;
    obj1.add();
    obj2.add();
    obj3.add();
    obj4.add();
    return 0;
}

this Pointer:  When name of local variable is same as data member of class

//this pointer  when name of local variable is same as data member of class
#include<iostream>
using namespace std;

class Add
{private:
int x=40,y=50;
public:
void set(int x,int y)
{
this->x=x;
this->y=y;
}
void print()
{
cout<<"sum of x and y is"<<x+y<<endl;
cout<<x;
}
};
int main()
{Add obj;
int x=10,y=20;
obj.set(x,y);
obj.print();
return 0;
}

Leave a Comment