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.

Subscribe / Share

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

Powered by Yahoo! Answers