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
- Rectangle Class:
- This class has two private member variables,
lengthandwidth, and a constructor to initialize these values. - The
areafunction calculates and returns the area of the rectangle. - The
displayfunction 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.
- This class has two private member variables,
- Pointers and Dynamic Memory Allocation:
- In the
mainfunction, we dynamically allocate memory for theRectangleobjects using thenewkeyword:Rectangle* rectPtr = new Rectangle(10, 5); - This creates a
Rectangleobject with length10and width5, and the pointerrectPtrholds 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;
- In the
- Freeing Memory:
- After we are done using the objects, we free the dynamically allocated memory using the
deletekeyword:delete rectPtr; delete anotherRectPtr; - This ensures that the memory is properly deallocated and prevents memory leaks.
- After we are done using the objects, we free the dynamically allocated memory using the
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
newkeyword allocates memory on the heap, and thedeletekeyword 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
deletean 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.
