Object Oriented Programming: Chapter 2: Classes and Objects

Array of Objects

C++ Programming | Object Oriented Programming

Question: Explain array of objects with example.

Array of Objects

•  In some database like applications we need to maintain the set of record for each entity.

•  For example if we want to store the roll numbers and names of 50 students in an array then instead of creating 50 objects, an array of objects can be created.

•  Following is a simple C++ program in which we have created an array of objects in which the student record is stored and then displayed.

 

#include<iostream>

using namespace std;

 #define MAX 50

class ArrObjects

{

private:

    int roll;

    char name[20];

public:

void get_data()

{

    cout<<"\n Enter roll number: ";

    cin>>roll;

    cout<<"\n Enter name: ";

    cin>>name;

}

void display()

{

    cout<<"\n Roll: "<<roll;

    cout<<"\n Name: "<<name;

}

};

void main()

{

ArrObjects obj[MAX]; //array of objects is created here

char ans='y';

 int n=0;

do

{

    cout<<"\n Enter Record ";

     obj[n++].get_data();

    cout<<"\n Do You Want to enter more record?";

    cin>>ans;

} while(ans= ='y' || ans = ='Y');

    cout<<"\n\n You have entered following data...\n";

    for(int i=0;i<n;i++)

        obj[i].display();

}

Output

Enter Record

Enter roll number: 10

Enter name: AAA

Do You Want to enter more record? y

Enter Record

Enter roll number: 20

Enter name: BBB

Do You Want to enter more record? y

Enter Record

Enter roll number: 30

Enter name: CCC

Do You Want to enter more record?

You have entered following data...

Roll: 10

Name: AAA

Roll: 20

Name: BBB

Roll: 30

Name: CCC

 

Review Question

1. Explain array of objects with example.

 

Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Array of Objects


Object Oriented Programming: Chapter 2: Classes and Objects



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation