Introduction
Pointers to objects in C++ allow you to manage dynamic memory allocation for objects and manipulate objects through their memory addresses. Using pointers to objects can provide greater flexibility and efficiency, especially when dealing with large objects or collections of objects.
Defining and Using Pointers to Objects
Basic Example
You can define a pointer to an object and use it to access the object’s members using the arrow operator (->
).
#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
// Create an object dynamically using a pointer
Person* personPtr = new Person;
// Access members using the pointer
personPtr->name = "Alice";
personPtr->age = 30;
// Call member function using the pointer
personPtr->display();
// Deallocate memory
delete personPtr;
return 0;
}
Output
Name: Alice, Age: 30
Explanation
Person* personPtr = new Person;
creates a newPerson
object dynamically and assigns its address to the pointerpersonPtr
.personPtr->name
andpersonPtr->age
access the object’s members using the arrow operator.personPtr->display();
calls the member function using the pointer.delete personPtr;
deallocates the memory allocated for the object.
Array of Objects Using Pointers
You can also create an array of objects dynamically using pointers.
Example: Array of Objects
#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
// Create an array of objects dynamically using a pointer
int numPersons = 3;
Person* persons = new Person[numPersons];
// Access and modify members of the array of objects
for (int i = 0; i < numPersons; i++) {
cout << "Enter name for person " << i + 1 << ": ";
cin >> persons[i].name;
cout << "Enter age for person " << i + 1 << ": ";
cin >> persons[i].age;
}
// Call member function for each object in the array
for (int i = 0; i < numPersons; i++) {
persons[i].display();
}
// Deallocate memory
delete[] persons;
return 0;
}
Output
Enter name for person 1: Alice
Enter age for person 1: 30
Enter name for person 2: Bob
Enter age for person 2: 25
Enter name for person 3: Charlie
Enter age for person 3: 35
Name: Alice, Age: 30
Name: Bob, Age: 25
Name: Charlie, Age: 35
Explanation
Person* persons = new Person[numPersons];
creates an array ofPerson
objects dynamically.persons[i].name
andpersons[i].age
access the members of each object in the array.persons[i].display();
calls the member function for each object in the array.delete[] persons;
deallocates the memory allocated for the array of objects.
Pointer to a Pointer (Double Pointer)
A pointer to a pointer (double pointer) can be used to dynamically allocate a 2D array of objects.
Example: Pointer to a Pointer
#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
int rows = 2;
int cols = 2;
// Create a 2D array of pointers to objects
Person** persons = new Person*[rows];
for (int i = 0; i < rows; i++) {
persons[i] = new Person[cols];
}
// Access and modify members of the 2D array of objects
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Enter name for person at (" << i << ", " << j << "): ";
cin >> persons[i][j].name;
cout << "Enter age for person at (" << i << ", " << j << "): ";
cin >> persons[i][j].age;
}
}
// Call member function for each object in the 2D array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
persons[i][j].display();
}
}
// Deallocate memory
for (int i = 0; i < rows; i++) {
delete[] persons[i];
}
delete[] persons;
return 0;
}
Output
Enter name for person at (0, 0): Alice
Enter age for person at (0, 0): 30
Enter name for person at (0, 1): Bob
Enter age for person at (0, 1): 25
Enter name for person at (1, 0): Charlie
Enter age for person at (1, 0): 35
Enter name for person at (1, 1): Dave
Enter age for person at (1, 1): 40
Name: Alice, Age: 30
Name: Bob, Age: 25
Name: Charlie, Age: 35
Name: Dave, Age: 40
Explanation
Person** persons = new Person*[rows];
creates an array of pointers toPerson
objects.persons[i] = new Person[cols];
allocates an array ofPerson
objects for each row.- The nested loops access and modify members of the 2D array of objects and call the member function for each object.
- The nested loops also deallocate the memory allocated for the 2D array of objects.
Example Programs
Example 1: Dynamic Object Creation and Member Function Calls
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int year;
public:
Car(string b, int y) : brand(b), year(y) {}
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// Create an object dynamically using a pointer
Car* carPtr = new Car("Toyota", 2020);
// Call member function using the pointer
carPtr->display();
// Deallocate memory
delete carPtr;
return 0;
}
Output
Brand: Toyota, Year: 2020
Explanation
Car* carPtr = new Car("Toyota", 2020);
creates a newCar
object dynamically and assigns its address to the pointercarPtr
.carPtr->display();
calls the member function using the pointer.delete carPtr;
deallocates the memory allocated for the object.
Example 2: Managing a Collection of Objects Using Pointers
#include <iostream>
#include <string>
using namespace std;
class Book {
public:
string title;
string author;
Book() {}
Book(string t, string a) : title(t), author(a) {}
void display() {
cout << "Title: " << title << ", Author: " << author << endl;
}
};
int main() {
int numBooks;
cout << "Enter the number of books: ";
cin >> numBooks;
// Dynamically allocate an array of Book objects
Book* books = new Book[numBooks];
// Input book details
for (int i = 0; i < numBooks; i++) {
cout << "Enter title for book " << i + 1 << ": ";
cin.ignore(); // To ignore newline character left in the buffer
getline(cin, books[i].title);
cout << "Enter author for book " << i + 1 << ": ";
getline(cin, books[i].author);
}
// Display book details
for (int i = 0; i < numBooks; i++) {
books[i].display();
}
// Deallocate memory
delete[] books;
return 0;
}
Output
Enter the number of books: 2
Enter title for book 1: C++ Programming
Enter author for book 1: Bjarne Stroustrup
Enter title for book 2: Effective C++
Enter author for book 2: Scott Meyers
Title: C++ Programming, Author: Bjarne Stroustrup
Title: Effective C++, Author: Scott Meyers
Explanation
Book* books = new Book[numBooks];
dynamically allocates an array ofBook
objects.- The user inputs the details of each book, which are stored in the array.
- The details of each book are displayed using the
display
member function. - delete[] books; deallocates the memory allocated for the array of objects.
Conclusion
Pointers to objects in C++ allow for dynamic memory allocation and manipulation of objects through their memory addresses. This chapter covered the basics of defining and using pointers to objects, creating arrays of objects dynamically, and using double pointers for 2D arrays of objects. Example programs demonstrated practical applications of pointers to objects, including dynamic object creation and managing collections of objects. Understanding pointers to objects is essential for writing efficient and flexible C++ programs.