Array Of Objects In C++ | Array Of Objects Explained With Real Life Example

 Array Of Objects In C++







What is an array of objects?

As you know about the array that it is a set of anything like if there is an array of characters then there will be a set of characters.

So the same concepts will be applied to objects - A array of objects will be a set of objects.

To understand array of objects more clearly suppose you have a form for a job application and you want to send same job application to 100 people. Then rather then creating 100 job application from scratch you can photocopy the same job application form 100 times.Right?

So here how you can create array of object in c++ - 

#include <iostream>
using namespace std;

class School
{
    public: 
    int std_id;
    int std_class;
    float percentage;

    void add_details()
    {
        
        cout<<"Enter The Student ID: "<<endl;
        cin>>std_id;
        cout<<"Enter The Class Of The Student: "<<endl;
        cin>>std_class;
        cout<<"Enter The Percentage Of Previous Class Of The Stduent: "<<endl;
        cin>>percentage;
    }
    void show_details()
    {
        
        cout<<"Student ID: "<<std_id<<endl;
        cout<<"Student Class: "<<std_class<<endl;
        cout<<"Previous Class Percentage: "<<percentage<<endl;
    }
};

int main()
{
    //Creating Array Of Objects
    School s1[3]; //= s1 , s2 , s3


    for (int i = 1; i < 4; i++)
    {
        cout<<"Enter Details Of The Student No. "<<i<<endl;
        s1[i].add_details();
    }


    for (int i = 1; i < 4; i++)
    {
        cout<<"Here are the details about student No. "<<i<<endl;
        s1[i].show_details();
    }
    

    return 0;
}

Output - 

Enter Details Of The Student No. 1
Enter The Student ID:
666
Enter The Class Of The Student: 
12
Enter The Percentage Of Previous Class Of The Stduent: 
96.36
Enter Details Of The Student No. 2
Enter The Student ID:
785
Enter The Class Of The Student: 
6
Enter The Percentage Of Previous Class Of The Stduent: 
87.66
Enter Details Of The Student No. 3
Enter The Student ID:
698
Enter The Class Of The Student: 
8
Enter The Percentage Of Previous Class Of The Stduent: 
90
Here are the details about student No. 1
Student ID: 666
Student Class: 12
Previous Class Percentage: 96.36
Here are the details about student No. 2
Student ID: 785
Student Class: 6
Previous Class Percentage: 87.66
Here are the details about student No. 3
Student ID: 3
Student Class: 9
Previous Class Percentage: 90



Comments

Popular posts from this blog

Function Overloading In C++ | Function Overloading Explained With Real Life Examples | Function Overloading In OOPs

Pointers In C++ | Pointer Explained With Real Life Examples | Pointers In OOPs C++