You must Sign In to post a response.
  • Category: Programming

    Program to implement concept of array of objects in C++.


    Are you in doubt as to how to write a program to implement the concept of an array of objects in C++? Follow this thread to find out the solutions and requisite explanations.

    Following is a question that was asked in the MSBTE (Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Winter 2018 examination.

    This question is asked for 08 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Write a program to declare a class 'staff' having data members as name and department. Accept this data for 10 staffs and display names of staff that is in 'CO' department.
  • Answers

    1 Answers found.
  • It is possible to create array of objects of a class. Though class is a user defined data type it behaves like built in data type.
    Syntax for creating array of objects is as follows

    class name(data type) array name[size];
    e.g. staff arr[10];
    here staff is the name of the class, arr is the name of array and 10 is the size of array.

    Below is the program which creates an array of objects with member variables staff name and department. Program ask user to enter the department name to find out the staff belongs to that particular department.

    Please include iostream.h, conio.h and string.h header files before declaration of class.
    //PROGRAM TO FIND STAFF OF PERTICULAR DEPARTMENT FROM GROUP OF STAFF.
    #include
    #include
    #include
    class staff
    {
    char s_name[20],dept[20],find[20];
    public:
    void get(void)
    {
    cin>>s_name>>dept;
    }
    void display(char * s)
    {
    strcpy(find,s);
    if((strcmp(dept,find)==0)||(strcmp(dept,find)==0))
    {
    cout< }
    }
    };
    void main(void)
    {
    class staff arr[10];
    char ch[20];
    clrscr();
    cout<<"Enter name and department for 10 staff members\n";
    cout<<"\nYou can enter department names as co(computer), ej(electronics), me(mechanical), ee(electrical)\n";
    for(int i=0;i<10;i++)
    {
    arr[i].get();
    cout<<"\n";
    }
    clrscr();
    cout<<"\nEnter the name of department of staff to be found\n";
    cin>>ch;
    cout<<"\nDetails of employees having department as "< for(i=0;i<10;i++)
    {
    arr[i].display(ch);
    cout<<"\n";
    }
    getch();
    }


  • Sign In to post your comments