HOW TO CREATE A ARRAY OF OBJECTS IN C ?

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
 750  View(s)
Ratings:
Rate this:
Hi,

How to create a array of objects in c++?

Thanks in advance. 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to create a array of objects in c ?

    Hi,


    try this:

    class sample{
    int a;
    public:
    void set_a(int n) {a=n;}
    int get_a( ) {return a}
    };
    int main()
    {
    sample ob[4];
    int i;
    for(i=0;  i<4;  i++) ob[i].set_a(i);
    for(i=0; i<4; i++) cout<< ob[i].get_a( );
    cout<<" \n";
    return 0;
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!