0% found this document useful (0 votes)
32 views

Pointer to object

Uploaded by

patbate71
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Pointer to object

Uploaded by

patbate71
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

A pointer is a variable that stores the memory address of another variable (or object) as its

value. A pointer aims to point to a data type which may be int, character, double, etc.

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.

There are two approaches by which you can access an object. One is directly and the other
is by using a pointer to an object in C++.

A pointer to an object in C++ is used to store the address of an object. For creating a pointer
to an object in C++, we use the following syntax:
classname*pointertoobject;

For storing the address of an object into a pointer in c++, we use the following syntax:

pointertoobject=&objectname;

The above syntax can be used to store the address in the pointer to the object. After storing
the address in the pointer to the object, the member function can be called using the pointer
to the object with the help of an arrow operator.

Examples

Now, let us see some examples and understand the pointer to the object in c++ in a better
way.

Example 1. In the below example, a simple class named My_Class is created. An object of
the class is defined as named object. Here a pointer is also defined named p. In the program
given below program, it is shown how can we access the object directly and how can we use
the pointer to access the object directly.

// Example using an object pointer.

#include <iostream>
using namespace std;

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

return 0;
}

Try it yourself
Output:

1
1

Explanation: The address of the object named object is accessed by using the address of
(&) operator. Whenever there is any increment or decrement in the pointer, it changes in
such a way that the pointer will aim at the next element of that particular base class. Also,
the same thing happens whenever there is any change in a pointer to an object. The change
here refers to any increment or decrement in the pointer.

For showing this, there are some modifications done in the above program so that the object
will be a two-dimensional array of type My_Class. Now, the pointer p will be once
incremented and then decremented for accessing the two elements in the array.

You might also like