Dynamic memory allocation for array of objects
In the previous topic memory for array of objects was static as the memory was allocated at compile time. To allocate memory dynamically or at run time, we use an array of pointers which can be created as follows:
ClassName *array-name[size];
Memory allocated for an array of pointers is far less than memory allocated for an array of objects. We can create an object at run time using the newoperator as follows:
array-name[index] = new ClassName;
After dynamically allocating memory for an object we access the members using the -> operator as follows:
array-name[index]->member;
Below example demonstrates how to dynamically allocate memory for an array of objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include<iostream> using namespace std; class Student { private: string name; string regdno; string branch; int age; public: void get_details(); void show_details(); }; void Student::get_details() { cout << "Enter student name: "; cin >> name; cout << "Enter stduent regdno: "; cin >> regdno; cout << "Enter student branch: "; cin >> branch; cout << "Enter student age: "; cin >> age; } void Student::show_details() { cout << "—–Student Details—–" << endl; cout << "Student name: " << name << endl; cout << "Student regdno: " << regdno << endl; cout << "Student branch: " << branch << endl; cout << "Student age: " << age << endl; } int main() { Student * s[3]; for (int i = 0; i < 3; i++) { s[i] = new Student; cout << "Enter stduent " << i + 1 << "" details: "<<endl; s[i] -> get_details(); } for (int i = 0; i < 3; i++) { cout << "Student " << i + 1 << "" details are: "<<endl; s[i] -> show_details(); } return 0; } |
Input and output for the above program is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | Enter student 1 details: Enter student name: ramesh Enter student regdno: 101 Enter student branch: cse Enter student age: 23 Enter student 2 details: Enter student name: suresh Enter student regdno: 102 Enter student branch: cse Enter student age: 23 Enter student 3 details: Enter student name: mahesh Enter student regdno: 103 Enter student branch: cse Enter student age: 24 Student 1 details are: ——–Student Details——– Student name: ramesh Student regdno: 101 Student branch: cse Student age: 23 Student 2 details are: ——–Student Details——– Student name: suresh Student regdno: 102 Student branch: cse Student age: 23 Student 3 details are: ——–Student Details——– Student name: mahesh Student regdno: 103 Student branch: cse Student age: 24 |
This code defines a C++ class called “Student” which has private member variables for storing the student’s name, registration number, branch, and age. The class also has two public member functions: “get_details” and “show_details”.
The “get_details” function prompts the user to input the student’s name, registration number, branch, and age, and stores these values in the private member variables of the class.
The “show_details” function displays the student’s details, including their name, registration number, branch, and age.
In the main function, an array of 3 pointers to Student objects is created. A for loop is used to iterate through the array, and for each iteration, a new Student object is created and allocated memory using the “new” operator. The “get_details” function is then called for each Student object, prompting the user to enter the student’s details.
Another for loop is used to iterate through the array of Student objects and for each iteration, the “show_details” function is called to display the student’s details.
Finally, the program returns 0 to indicate successful execution.
The program can be used to store and display the details of multiple students. The use of pointers and the “new” operator allows for dynamic allocation of memory for an unknown number of Student objects.
Objects As Function Arguments
Like variables, objects can also be passed using pass-by-value, pass-by-reference, and pass-by-address. In pass-by-value we pass the copy of an object to a function. In pass-by-reference we pass a reference to the existing object. In pass-by-address we pass the address of an existing object. Following program demonstrates all three methods of passing objects as function arguments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include<iostream> using namespace std; class Student { private: string name; string regdno; int age; public: string branch; void get_details(); void show_details(); //Pass-by-value void set_branch(Student s) { branch = s.branch; } //Pass-by-reference void set_branch(Student & s, int x) { branch = s.branch; } //Pass-by-address void set_branch(Student * s) { branch = s -> branch; } }; int main() { Student s1, s2, s3, s4; s1.branch = "cse"; s2.set_branch(s1); s3.set_branch(s1, 10); s4.set_branch( & s1); return 0; } |
The code is a C++ program that defines a class “Student” with private member variables name, regdno, age and public member variable branch. The class has two public member functions: get_details and show_details.
The main function starts by creating four objects of the Student class: s1, s2, s3, and s4.
s1.branch is assigned the value “cse”, which sets the branch of s1 to “cse”.
The set_branch function is called on s2, with s1 passed as an argument. This sets the branch of s2 to the same value as s1’s branch, which is “cse”.
The set_branch function is called on s3, with s1 and an integer value 10 passed as arguments. This sets the branch of s3 to the same value as s1’s branch, which is “cse”.
The set_branch function is called on s4, with the address of s1 passed as an argument. This sets the branch of s4 to the same value as s1’s branch, which is “cse”.
The program ends with a return statement in the main function.
Notice that this code will not run as is because the set_branch functions are missing the implementation and have errors in the parameters.