Pointer to Class in C++

In this tutorial chapter we will study and understand the concept and Pointer to Class in C++ also known as Pointer to Object. For this it is first necessary to know the concept of pointers & classes in C++.

In general Pointers are variables that store address of other variables. A class in C++ is and Object oriented programming feature which enables programmer to create user defined complex datatypes (member variables) and functions(member functions) that operate on that data.

C++ Pointer to Class

Pointer to Class in C++
A class pointer is a pointer variable that stores address of an object of a class. As shown in the above diagram we have a class  Rectangle with 2 data members and 1 member function. We have also created an object of that class named var1. Now we create a pointer variable *ptr of type Rectangle and assign the address of the object var1 to this pointer variable. As shown in the diagram the address of the object var1 is stored in the pointer variable ptr. Let us see an example of the same:

Example program of Pointer to Class in C++

Run Online

Output
Area of rectangle is: 20
Explanation
  • As you can see in the program we created an object of type Rectangle and passed length and breadth values in the parameterized constructor.
  • We then created a pointer named ptr of type Rectangle and assigned the address of var1 object to this pointer.
  • Then we calculated the area of the rectangle by calling the getArea() function of the class using this pointer (by using -> operator)
Pointer to Class Array in C++

As we just saw pointer to class, a pointer can also point to an an array of class. That is it can be used to access the elements of an array of type class.

 

pointer to class object array

As you can see in the Above diagram we have created an object array named var of class Rectangle. We have a pointer of class Rectangle named ptr and we have assigned the object array’s base address to this pointer ptr. So now our pointer ptr points to the first element in the object array. We can now iterate through the object array by using the increment operator in c++ like this : ptr++ This make the pointer to point ot the next array element of the object array and we can then access that element’s data members and member functions. Let us see and an example:

Run Online

Output
Area of Rectangle1 : 20
Area of Rectangle2 : 12
Explanation
  • As you can see in the program we created an array object named var of type Rectangle and size 2 and set the values of both array elements using setData(int l, int b) function .
  • Next we created a pointer ptr of type Rectangle and assigned the object array’s address (base address) to this pointer. Pointer ptr now points to the first element of the object array
  • Lastly we access the member function of each element of the object array using pointer ptr & for loop to iterate through the object array.

Leave a Reply

Your email address will not be published. Required fields are marked *