Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Pointer to an Object

Introduction

A pointer to an object is a variable that stores the memory address of an object. Just like how you can have pointers to primitive data types (like int, float, etc.), you can also have pointers to instances of classes (objects). This allows you to dynamically manage memory, pass objects to functions efficiently, and perform other advanced operations.

Basic Concept

When you create an object in C++, it is stored in memory. A pointer to an object holds the memory address where that object is stored, allowing you to access and manipulate the object through the pointer.

Syntax for Pointer to an Object

  1. Declaration:
ClassName *pointerName;

  • ClassName is the type of the object (i.e., the class name).
  • pointerName is the name of the pointer.

2. Assigning the Address of an Object to a Pointer:

pointerName = &objectName;

  • &objectName gets the memory address of objectName and assigns it to pointerName

3. Accessing Members of an Object via Pointer:

  • Using the arrow operator (->):
pointerName->memberName;

  • Using dereferencing operator (*) with dot operator (.):
(*pointerName).memberName;

Code

#include <iostream>
#include <cmath>  // For mathematical operations like M_PI
using namespace std;

class Circle {
public:
    double radius;

    // Method to calculate the area of the circle
    double area() {
        return M_PI * radius * radius;  // M_PI is a constant for pi (3.14159...)
    }

    // Method to calculate the circumference of the circle
    double circumference() {
        return 2 * M_PI * radius;
    }
};

int main() {
    // Create a stack-allocated Circle object
    Circle circle;

    // Set the radius of the circle
    circle.radius = 5.0;

    // Create a pointer to the Circle object
    Circle* circlePtr = &circle;

    // Calculate and display the area of the circle using the pointer
    cout << "Area of the Circle = " << circlePtr->area() << endl;

    // Calculate and display the circumference of the circle using the pointer
    cout << "Circumference of the Circle = " << circlePtr->circumference() << endl;

    return 0;
}

Output:

Area of the Circle = 78.5398
Circumference of the Circle = 31.4159

Explanation

Step 1: Creating the Circle Class:

  • We define a class named Circle with a single attribute radius.
class Circle {
public:
    double radius;

Step 2: Methods to Calculate Area and Circumference:

  • area(): This method returns the area of the circle using the formula π * r².
  • circumference(): This method returns the circumference using the formula 2 * π * r.
double area() {
    return M_PI * radius * radius;
}

double circumference() {
    return 2 * M_PI * radius;
}

Step 3: Creating a Stack-Allocated Object:

We create an object of the Circle class directly on the stack. This means the object is automatically managed and will be destroyed when it goes out of scope, with no need for manual memory management.

Circle circle;

Step 4: Using a Pointer to the Stack-Allocated Object:

  • Although we are not using dynamic memory allocation, we can still create a pointer that points to the stack-allocated object. This allows us to demonstrate pointer operations.
Circle* circlePtr = &circle;

Step 5: Setting the Radius and Calculating Properties:

  • We set the radius of the circle using the object itself and then use the pointer to calculate the area and circumference.
circle.radius = 5.0;
cout << "Area of the Circle = " << circlePtr->area() << endl;
cout << "Circumference of the Circle = " << circlePtr->circumference() << endl;

Key Points:

  • Stack Allocation: In this example, the Circle object is created on the stack, meaning it is automatically managed, and no manual memory management (e.g., new or delete) is required.
  • Pointer to Object: Even without dynamic allocation, pointers can be used to access and manipulate stack-allocated objects.
  • Efficient Memory Management: Stack allocation is generally faster and simpler than heap allocation, as it avoids the overhead of manual memory management.

Summary:

This code shows how to work with pointers to stack-allocated objects, making it simpler and more efficient for scenarios where dynamic memory allocation is unnecessary. It provides a clear understanding of how pointers can be used with both stack and heap memory, and is a useful concept for students learning C++.

    Leave a Reply

    Your email address will not be published.

    Need Help?