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

Pointer to an Object in a Heap

Let’s explore the concept of a pointer to an object in a heap by using an interesting example of a Book class. We will create an object dynamically (in the heap) and access it through a pointer.

Example: Book Class

We’ll create a Book class that has attributes like title, author, and pages. We’ll dynamically allocate memory for a Book object on the heap using a pointer, set its attributes, and display its details.

Code

#include <iostream>
#include <string>
using namespace std;

class Book {
public:
    string title;
    string author;
    int pages;

    // Method to display book details
    void displayDetails() {
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
        cout << "Pages: " << pages << endl;
    }
};

int main() {
    // Dynamically allocate memory for a Book object on the heap
    Book* bookPtr = new Book();

    // Set the attributes of the Book object
    bookPtr->title = "1984";
    bookPtr->author = "George Orwell";
    bookPtr->pages = 328;

    // Display the details of the book using the pointer
    cout << "Book Details:" << endl;
    bookPtr->displayDetails();

    // Deallocate the memory (freeing the heap memory)
    delete bookPtr;

    return 0;
}

Output:

Book Details:
Title: 1984
Author: George Orwell
Pages: 328

Explanation:

Step 1: Create a Book Class:

  • The Book class has three public attributes: title, author, and pages.
  • It also has a method displayDetails() that prints out the details of the book.
class Book {
public:
    string title;
    string author;
    int pages;

    void displayDetails() {
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
        cout << "Pages: " << pages << endl;
    }
};

Step 2: Heap Allocation:

  • In the main() function, we dynamically allocate memory for a Book object using the new keyword. This creates the object on the heap and returns a pointer to it.
Book* bookPtr = new Book();

Step 3: Setting Attributes:

  • Using the pointer bookPtr, we set the title, author, and pages attributes of the Book object.
bookPtr->title = "1984";
bookPtr->author = "George Orwell";
bookPtr->pages = 328;

Step 4: Accessing Members Using Pointer:

  • We call the displayDetails() method using the pointer to display the book’s details.
bookPtr->displayDetails();

Step 5: Memory Deallocation:

  • After we are done with the object, we free the allocated memory using the delete keyword to avoid memory leaks.
delete bookPtr;

Summary:

  • Heap Allocation: The object is created on the heap, meaning its memory needs to be managed manually (using new and delete).
  • Pointer to Object: A pointer is used to access and manipulate the object in the heap.
  • Memory Management: It’s important to release the memory when done using the delete keyword to avoid memory leaks.

    Leave a Reply

    Your email address will not be published.

    Need Help?