Program to illustrates Array of Objects


#include <iostream.h>
#include <string.h>
/*----------------------Class definition-----------------------*/
class student
{
        int roll_no;
        char name[20];
    public :                               // prototypes declared
        void set_data(int, char *);
        void print_data();
};

void student::set_data(int c, char *s)
{
    roll_no = c;
    strcpy(name, s);
}
void student::print_data()
{
    cout << "Roll No. : " << roll_no << "\t";
    cout << "Name     : " << name << "\n\n";
}
/*--------------------Definition ends here-------------------*/
void main(void)
{
    student array[10];        // Array of Objects declared
    int i;
    char names[10][10] = {"kush", "stavan", "poonam", "sachi", "samarth", "hetal", "trupti",     "simone", "shalaka", "megha"};
    // accessing member of an object where the object is an array element

    for (i=0; i<10; i++)
        array[i].set_data(i, names[i]);                                               
    cout << "The data output is :\n\n";       
    for (i=0; i<10; i++)
        array[i].print_data();
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|