Pointer to Object in C++
What is a pointer?
A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we use & operator. So to store the address of the variable in the Pointer, we need to create a pointer variable using proper syntax.
Syntax: The syntax for pointer variable is given as:
Data_type * variable=&variable
In above syntax, * indicates that the variable is a Pointer and will store the variable's address. We can use the pointer variable in the code to change the value of the main variable by depointing it.
Programs to Illustrate Pointers in C++
- Example for Pointer:
#include "bits/stdc++.h"
using namespace std;
int main(){
int a=20;
int * b=&a;//Pointer to store the address of the variable a
cout<<a<<endl;
cout<<&a<<endl;//print the address of a
cout<<b<<endl;//prints the address of a
cout<<*b<<endl;//depointing the pointer
return 0;
}
Output

Explanation:
In the above code, firstly, we have declared a variable of int data type, and we have also declared a pointer to store the address of the variable. Then we have printed the value of variable a, and then address of the variable using Pointer and & operator, after than we have printed the value at pointer variable b using depointing it.
What is Pointer to object?
A pointer to an object acts the same as Pointer to a variable. But here, in place of the address of the variable, address of the object is stored. In the main function, when an object is created to a class, a pointer variable is decalred in the same manner as we declared for the variable, and it will store the object's address. For creating a pointer to an object, we should not use data type for the Pointer. Instead, we need to use the class name for the object pointer. If we want to use a member function in the class using the Pointer in the main function, then we need to use the -> symbol, as shown in the below example.
Example:1
#include "bits/stdc++.h"
using namespace std;
class example{
public:
string hi(){
}
};
int main(){
//object is created for the example class
example obj;
//Pointer for obj object is created using class name
example *a=&obj;
}
Explanation:
In the above code, we created a class with an example name and an empty member function. We have created an object for the example class in the main function. We have created a pointer variable using class name as data type and assigned address of the object to the Pointer.
Example: 2
#include <bits/stdc++.h>
using namespace std;
class Rectangle
{
private:
int length;
int breadth;
public:
Rectangle(int l, int b)
{
length=l;
breadth=b;
}
int getArea()
{
return 2*length*breadth;
}
};
int main()
{
// creating an object of Rectangle
Rectangle var1(10,30);
//creating a pointer for the object using class name as data type
Rectangle* ptr = &var1;
//calling the member function using -> symbol
int area = ptr->getArea();
cout<<"Area of rectangle is: "<<area;
return 0;
}
Output:

Explanation:
In the above example, we have created a class that will return the output as the area of the rectangle. We have created an object for the class rectangle in the main function. Then we created Pointer using the class name as the datatype for the Pointer. Using & operator, we assigned the object's address to the Pointer. Using->, we have sent the data to the member function Rectangle. Using the getarea() member function, which returns the area of the rectangle, we have printed the value.
Example:3
#include <bits/stdc++.h>
using namespace std;
class Square
{
private:
int side;
public:
Square(int l)
{
side=l;
}
int getArea()
{
return side*side;
}
};
int main()
{
// creating an object of Rectangle
Square var1(10);
//creating a pointer for the object using class name as data type
Square* ptr = &var1;
//calling the member function using -> symbol
int area = ptr->getArea();
cout<<"Area of rectangle is: "<<area;
return 0;
}
Output:

Explanation:
In the above code, we have created a class Square that returns the area of the square. Inside the class, we have created a constructor that takes the data and a member function that returns the area of the square. We created an object for the square class in the main class. Using the name of the class as a datatype, we created a pointer for the object. Using the -> symbol, we called the getarea() member function, which returns the area of the square.