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

    Program to accept and display data for five object using pointer to array of object.


    Have a computer engineering exam question for which you need a quick solution? With the help of experts you can get free tutorials to know how to give the correct solution to this question.

    Following is the question asked in the MSBTE (Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2017 examination.
    This question is for 4 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program to declare class Account having data member as acc_no and balance. Accept and display data for five object using pointer to array of object.
  • Answers

    1 Answers found.
  • The real appeal of Object Oriented Programming Language is that it allows the user defined data type that is class to behave like built in data type. So we can easily declare array of objects as simply as we declare array of integers. Also we can declare a pointer variable of a class to hold the address of object of that class.
    Using this theme following program is developed. In this program pointer to object holds the base address of array of objects. Just by incrementing the pointer we can access every object of the array.

    Below is the program which is having an array of objects and one pointer variable to hold the address of object.

    Please add iostream.h and conio.h header files before declaration of class.

    //PROGRAM TO HANDLE ARRAY OF OBJECT USING POINTER TO ARRAY OF OBJECTS
    #include "iostream.h"
    #include "conio.h"
    class Account
    {
    private:
    float bal;
    int accno;
    public:
    void get(void)
    {
    cout<<"\nEnter account number and amount you want to deposite\n";
    cin>>accno>>bal;
    }
    void display(void)
    {
    cout<<"\nAccount number is "< cout<<"\nAvailable balance is "< }
    };
    void main(void)
    {
    class Account a[5];
    class Account *p;
    clrscr();
    p=a;
    cout<<"\nPlease add the data for five users one by one\n";
    for(p=a;p {
    p->get();
    }
    clrscr();
    cout<<"\nDetails of five users is as follows\n";
    for(p=a;p {
    p->display();
    }
    getch();
    }

    In above program an array of objects a[5] of type Account is declared and a pointer variable p of type class Account type is also declared. When we use the statement p=a; it means p is holding the base address of array that is address of zeroth object. In for loop when we increment the pointer p as p++, it points to next object of the array. Mean while when pointer reaches out of the boundary of array, it must be rewind to hold base address of array. So in second loop we use statement p=a again.
    When pointer holds the address of object and we want to call a specific function call using that object, in such case we use statement like pointer variable->function name(); So inside the first and second loop we use statement like p->get() and p->display(). Arrow operator is used when member function is called using object pointer instead of object itself.


  • Sign In to post your comments