Search This Blog

Thursday 28 April 2016

Create a class called Person, that has member data name. Include member functions getdata() to get data from the user, and putdata() function to display its data. Write a main( ) program that creates an array of pointers, persPtr[100] to Person. In a loop, ask the user for data, and use new to create an object of type Person to hold the data. Put the pointer to the object in the array. When the user has finished entering the data for all the persons, use a for loop and a single statement such as persPtr[I]->putdata( ) to display the data from each object in the array

Practical 27


 #include <iostream>
using namespace std;

class Person
{
    string name;
public:
    void getdata()
    {
        cout<<"\nEnter Name:";
        cin>>name;
    }
    void putdata()
    {
        cout<<"\nYour name :"<<name;
    }
};

int main()
{
    int n;
    Person *persptr[10];
    cout<<"\nHow Many Names:";
    cin>>n;

    for(int i=0;i<n;i++)
    {
        persptr[i] = new Person;
        persptr[i]->getdata();
    }
    for(int i=0;i<n;i++)
    {
        persptr[i]->putdata();
    }

    return 0;
}

No comments:

Post a Comment