C++ program for student details using array of objects

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

  1. Initialize student object from a constructor
  2. Create a function to get input from the user to initialize the student object.

Example: C++ program for student details using array of objects

  1. Create a class name student
  2. Declare necessary fields for student as name,age and rollno as a private member of class.
  3. In public section create constructor to initialize value from it
  4. also create a show function to show/ print user data.
#include 
#include 
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

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

  1. create a class student
  2. declare fields of student as private data member
  3. create a function getStudent() to get input from user and assign it to student data members
  4. create a function showStudent() to show student object details to user.
  5. create a main method and define size of student object
  6. loop student object to take input from user
  7. loop student objcet to show student object details from array of object.

Example: c++ program to display 5 student details using class and object

#include 
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

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.

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Command Line Argument
  5. Local vs Global Object in C++
  6. Local and Nested Classes in C++
  7. Arithmetic operations using switch case
  8. Switch Case in C Programming
  9. Constructor in C++
  10. Know more about C++

Categories C++