tags:

views:

67

answers:

1
#include<iostream.h>
class test{
    int a;
    char b;
public:
    test()
    {
        cout<<"\n\nDefault constructor being called";
    }
    test(int i,char j)
    {
        a=i;
        b=j;
        cout<<"\n\nConstructor with arguments called";
    }
};
int main()
{
    test tarray[5];
    test newobj(31,'z');
};

In the above code snippet can we initialize values to tarray[5]?

+7  A: 
test  tarray[5] = {test(1, 2), test(), test(5, 6), test()};

The fifth element will be initialized with default constructor.

//here length of array will be inferred from number of initializers,
//   so it's going to be 4
test  tarray[] = {test(1, 'a'), test(), test(5, 'b'), test()};
vava
tarray is an object of class test and it has its own int a and char b.how can we intializa the character
anurag18294
@anurag18294: tarray is an array of `test` s, not an instance of `test`. However, its members are instances of `test`. In other words, there is no `tarray.b`, only `tarray[0].b`, `tarray[1].b`, etc.
Joey Adams
can we intialize it in this mannertest tarray[5]={test(1,'a'),test(3,'b'),test(5,6),test(),test(4,'s')};
anurag18294
i want that syntax
anurag18294
@anurag, sure, whatever `test` constructor can handle.
vava