Array of Objects in C++
Array of Object is similar to creating array or integer, character or any primitive data types.
Example
Array of integer is
int age[5]={1,2,3,4,5};
Similar way array of Students is
Student s[5]={studentObj1,studentObj2,studnetObj3,studentObj4,studentObj5};
The student is class Name
s is an array of objects of 5 elements
To initialize the student object two methods are used here
- Initialize student object from a constructor
- Create a function to get input from the user to initialize the student object.
Example: C++ program for student details using array of objects
- Create a class name student
- Declare necessary fields for student as name,age and rollno as a private member of class.
- In public section create constructor to initialize value from it
- also create a show function to show/ print user data.
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 | #include <iostream> #include <cstring> using namespace std; class Student { private: char name[30]; long rollNo; char branch[30]; public: Student(const char * name, long rollNo, const char * branch) // constructor { strcpy(this -> name, name); this -> rollNo = rollNo; strcpy(this -> branch, branch); } void show() { cout << "Student Details are" << endl; cout << "Name " << name << endl; cout << "Roll No " << rollNo << endl; cout << "Branch " << branch << endl; } }; int main() { Student student[3] = { Student("Ram", 1, "CSE"), Student("Mohan", 2, "ETC"), Student("Sohan", 3, "Mech") }; for (int i = 0; i <= 2; i++) { student[i].show(); } } |
To copy the local array variable name to the Student class name strcpy() is used
Here Student constructor is used to initializing student object.
Result
1 2 3 4 5 6 7 8 9 10 11 12 | Student Details are Name Ram Roll No 1 Branch CSE Student Details are Name Mohan Roll No 2 Branch ETC Student Details are Name Sohan Roll No 3 Branch Mech |
Get User Input for Student Objects
- create a class student
- declare fields of student as private data member
- create a function getStudent() to get input from user and assign it to student data members
- create a function showStudent() to show student object details to user.
- create a main method and define size of student object
- loop student object to take input from user
- loop student objcet to show student object details from array of object.
Example: c++ program to display 5 student details using class and object
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 | #include <iostream> using namespace std; class Student { private: char name[30]; long rollNo; char branch[30]; public: void getStudent() { cout<<"Enter name"; cin>>name; cout<<"Enter rollno"; cin>>rollNo; cout<<"Enter branch"; cin>>branch; } void showStudent() { cout << "Name " << name << endl; cout << "Roll No " << rollNo << endl; cout << "Branch " << branch << endl; } }; int main() { Student student[5]; for (int i = 0; i <= 5; i++) { student[i].getStudent(); } cout << "Student Details are" << endl; for (int i = 0; i <= 5; i++) { student[i].showStudent(); } } |
Output
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 | Enter name Ram Enter rollno 1 Enter branch CSE Enter name Mohan Enter rollno 2 Enter branch Mech Enter name Sohan Enter rollno 3 Enter branch Civil Student Details are Name Ram Roll No 1 Branch CSE Name Mohan Roll No 2 Branch Mech Name Sohan Roll No 3 Branch Civil |
Here we discussed the C++ program for student details using an array of objects if you found helpful then please share.