Pointers:
A pointer in C++ is a variable that stores the address (or memory location) of another
variable. In other words, a pointer points to the address of another variable.
Reference Operator (&):
The reference operator (&) returns the address of any variable (including pointers). For
example:
float a = 23.4;
// using the reference operator
cout << &a;
Output:
0x7ffe0734e9b4
Dereference Operator (*):
The Asterisk symbol (*) is called dereference operator when it is used with pointers.
For example:
int var = 45;
int* ptr;
ptr = &var;
// using the dereference operator
cout << "The value returned by ptr is: " << ptr << endl;
cout << "The value returned by *ptr is: " << *ptr;
Output:
The value returned by ptr is: 0x7fff40bf6674
The value returned by *ptr is: 45
pointer Declaration:
Pointers must be declared before they can be used, just like a normal variable. The syntax of
declaring a pointer is to place a * in front of the name. A pointer is associated with a type
(such as int and double) too.
Pointers in C++ are declared using the following syntax:
datatype *pointer_name;
// or
datatype* pointer_name;
// or
datatype * pointer_name;
Initializing Pointers:
this normally done via the address-of operator (&).
The address-of operator (&) operates on a variable, and returns the address of the variable.
You can use the address-of operator to get the address of a variable, and assign the address to
a pointer variable.
Example for pointers:
#include <iostream.h>
int main()
{
int x = 27;
int *ip;
ip = &x;
cout << "Value of x is : ";
cout << x << endl;
cout << "Value of ip is : ";
cout << ip<< endl;
cout << "Value of *ip is : ";
cout << *ip << endl;
return 0;
}
Output:
Pointer To Object:
A pointer is a variable that stores the memory address of another variable (or object) as its
value.
Pointers to objects aim to make a pointer that can access the object, not the variables. Pointer
to object in C++ refers to accessing an object.
For creating a pointer to an object in C++, we use the following syntax:
classname*pointer to object;
For storing the address of an object into a pointer in c++, we use the following syntax:
Pointer to object=&objectname;
Example:
#include <iostream.h>
class My_Class
{
int num;
public:
void set_number(int value) {num = value;}
void show_number();
};
void My_Class::show_number()
{
cout << num << "\n";
}
int main()
{
My_Class object, *p; // an object is declared and a pointer to it
object.set_number(1); // object is accessed directly
object.show_number();
p = &object; // the address of the object is assigned to p
p->show_number(); // object is accessed using the pointer
Output:
1
1
This pointer:
The this pointer holds the address of current object, in simple words you can say that
this pointer points to the current object of the class.
Here you can see that we have two data members num and ch.
In member function setMyValues() we have two local variables having same name as
data members name.
In such case if you want to assign the local variable value to the data members then
you won’t be able to do until unless you use this pointer, because the compiler won’t
know that you are referring to object’s data members unless you use this pointer.
This is one of the example where you must use this pointer.
Example:
#include <iostream.h>
class Demo
{
private:
int num;
char ch;
public:
void setMyValues(int num, char ch){
this->num =num;
this->ch=ch;
}
void displayMyValues(){
cout<<num<<endl;
cout<<ch;
}
};
int main(){
Demo obj;
[Link](100, 'A');
[Link]();
return 0;
}
Output:
100
A
Pointers to Derived Classes:-
Polymorphism is also accomplished using pointers in C++.
It allows a pointer in a base class to point to either a base class object or to any
derived class object.
We can have the following Program segment show how we can assign a pointer to
point to the object of the derived class.
Example :
#include<iostream.h>
class base
{
public:
int n1;
void show()
{
cout<<”\nn1 = “<<n1;
}
};
class derive: public base
{
public:
int n2;
void show()
{
cout<<”\nn1 = “<<n1;
cout<<”\nn2 = “<<n2;
}
};
int main()
base b;
base *bptr; //base pointer
cout<<”Pointer of base class points to it”;
bptr=&b; //address of base class
bptr->n1=23; //access base class via base pointer
bptr->show();
derive d;
cout<<”\n”;
bptr=&d; //address of derive class
bptr->n1=63; //access derive class via base pointer
bptr->show();
Output:
The pointer of the base class points to it
n1 = 23
The pointer of base class points to derive a class
n1=63