How to initialize an array of user defined objects?


The following program shows how to initialize an array of objects.

#include <iostream.h>

class emp

{

private :

int i ;

public:

emp( int ii = 5 )

{

i = ii ;

}

void print( )

{

cout << i << endl ;

}

} ;

main( )

{

emp e1 ( 10 ) ;

emp e2[4] = { e1, emp ( 15 ), 17 } ;

}

In the above program e2[0] will get initialized with the contents of e1. e2[1] will get initialized with a object whose i equals 15 and e2[2] with an object whose i value would be 17. The last element, e2[3] will be initialized with an object whose i takes the default value of 5.

This entry was posted in C++ and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>