×

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++

  1. 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

Pointer to Object in C++

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:

Pointer to Object in C++

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:

Pointer to Object in C++

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.


Related Topics

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the...

3 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.