Array of object and Pointers to Objects
In the world of object-oriented programming (OOP) with C++, managing multiple objects efficiently is crucial. Two powerful concepts that help achieve this are Arrays of Objects and Pointers to Objects. This documentation will provide a clear understanding of both concepts and how they can be employed in C++.
Example 1:
#include <iostream> using namespace std; int main() { int arr[5] = {10, 50, 40, 77, 33}; // Printing the memory address of the array (address of the first element) cout << arr << endl; // Printing the value at the first element of the array cout << *arr << endl; // Printing the memory address of the second element of the array cout << arr + 1 << endl; // Printing the memory address of the last element of the array cout << arr + 4 << endl; // Printing the value at the last element of the array cout << *(arr + 4) << endl; // Using a loop to print all elements of the array using pointer arithmetic for (size_t i = 0; i < 5; i++) { cout<< *(arr + i) << endl; } return 0; }
This program demonstrates the use of pointers with arrays.
The output will display the memory addresses, values, and elements of the array based on the pointer arithmetic and loop iterations.
Example 2:
#include <iostream> using namespace std; int main() { int arr[5] = {10, 50, 40, 77, 33}; int *p; p = arr; for (size_t i = 0; i < 5; i++) { cout << *p << endl; p++; } return 0; }
This C++ program demonstrates the use of a pointer to iterate through an array.
The output will display the values of each element in the array, printed one at a time, as the pointer p is incremented within the loop.
10 50 40 77 33
Note:
- The pointer p is initially set to the address of the first element of the array.
- Incrementing the pointer (p++) makes it point to the next element in the array.
- This approach provides an alternative way to iterate through array elements using pointers.
Example 3:
#include <iostream> #include <cstdlib> #include <cstring> using namespace std; class Student { char name[20]; int ID; public: Student() //empty constructor { cout << "empty\n"; strcpy(name, "No name"); ID = 0; } Student(char n[], int id) //constructor overloading { cout << "parameterize\n"; strcpy(name, n); ID = id; } void print(void) { cout << name << "\t" << ID << endl; } }; int main() { Student S1("Ali", 123), S2("Ahmad", 456); Student arr[3]; for (int i = 0; i < 3; i++) arr[i].print(); return 0; }
This program demonstrates the use of constructor overloading and default constructors in a class Student. Let’s break down the code:
- Class Student:
Defines a class named Student with private data members name and ID.
Contains two constructors: an empty constructor and a parameterized constructor.
Provides a member function print() to display student details. - Empty Constructor:
Student() { cout << "empty\n"; strcpy(name, "No name"); ID = 0; }
Initializes name with “No name” and ID with 0.
Prints “empty” when an object is created using this constructor. - Constructor Overloading:
Student(char n[], int id) { cout << "parameterize\n"; strcpy(name, n); ID = id; }
Initializes name and ID with the provided arguments.
Prints “parameterize” when an object is created using this constructor. - print() Function:
void print(void) { cout << name << "\t" << ID << endl; }
Prints the student’s name and ID.
- Creating Objects:
Objects S1 and S2 are created using the parameterized constructor, displaying “parameterize” for each object.
An array arr of Student objects is created using the default constructor, displaying “empty” for each object. - Printing Student Details:
The print() function is called for each object in the array to display their details.
Output:
The output will display the constructor messages and the details of each student in the array.