Using Pointers with Objects in C++

GitHub code

Step 1: Define a Class

We’ll define a simple Rectangle class that has private member variables for length and width, a constructor, and a member function to calculate the area.

#include <iostream>

class Rectangle {
private:
int length;
int width;

public:
// Constructor
Rectangle(int l, int w) : length(l), width(w) {}

// Function to calculate area
int area() const {
return length * width;
}

// Function to display the dimensions
void display() const {
std::cout << "Rectangle with length " << length << " and width " << width << std::endl;
}

// Destructor (optional for demonstration)
~Rectangle() {
std::cout << "Rectangle destroyed!" << std::endl;
}
};

Step 2: Create and Use Objects with Pointers

Now, we’ll create a main program where we dynamically allocate memory for Rectangle objects using pointers, access their member functions through the pointers, and then properly clean up the memory.

#include <iostream>
using namespace std;

class Rectangle {
private:
int length;
int width;

public:
// Constructor
Rectangle(int l, int w) : length(l), width(w) {}

// Function to calculate the area of the rectangle
int area() const {
return length * width;
}

// Function to display the dimensions
void display() const {
cout << "Rectangle with length " << length << " and width " << width << endl;
}

// Destructor
~Rectangle() {
cout << "Rectangle destroyed!" << endl;
}
};

int main() {
// Dynamically allocate memory for a Rectangle object using a pointer
Rectangle* rectPtr = new Rectangle(10, 5);

// Access members using the pointer (-> operator)
rectPtr->display();
cout << "Area of the rectangle: " << rectPtr->area() << endl;

// Dynamically allocate memory for another Rectangle
Rectangle* anotherRectPtr = new Rectangle(15, 7);

// Access the new Rectangle's data
anotherRectPtr->display();
cout << "Area of the second rectangle: " << anotherRectPtr->area() << endl;

// Free the dynamically allocated memory
delete rectPtr;
delete anotherRectPtr;

return 0;
}

Explanation of the Program

  1. Rectangle Class:
    • This class has two private member variables, length and width, and a constructor to initialize these values.
    • The area function calculates and returns the area of the rectangle.
    • The display function prints the dimensions of the rectangle.
    • A destructor (~Rectangle) is defined, which is called when the object is destroyed, and it prints a message indicating the destruction of the object.
  2. Pointers and Dynamic Memory Allocation:
    • In the main function, we dynamically allocate memory for the Rectangle objects using the new keyword:
      Rectangle* rectPtr = new Rectangle(10, 5);
    • This creates a Rectangle object with length 10 and width 5, and the pointer rectPtr holds the memory address of that object.
    • To access the class members through the pointer, we use the arrow operator (->):
      rectPtr->display(); cout << "Area of the rectangle: " << rectPtr->area() << endl;
  3. Freeing Memory:
    • After we are done using the objects, we free the dynamically allocated memory using the delete keyword:
      delete rectPtr; delete anotherRectPtr;
    • This ensures that the memory is properly deallocated and prevents memory leaks.

Program Output

Rectangle with length 10 and width 5
Area of the rectangle: 50
Rectangle with length 15 and width 7
Area of the second rectangle: 105
Rectangle destroyed!
Rectangle destroyed!

Key Points

  • Dynamic Allocation: The new keyword allocates memory on the heap, and the delete keyword frees that memory.
  • Pointer to Objects: Pointers can hold the addresses of dynamically allocated objects, and you can access the object’s members via the pointer using the arrow (->) operator.
  • Destructor: When you delete an object, its destructor is automatically called, allowing you to perform clean-up tasks if necessary.

This example demonstrates how pointers work with objects in C++ and how dynamic memory allocation is handled for objects.

Scroll to Top