开发者

array of objects of a class

#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 c开发者_如何学JAVAalled";
    }
};
int main()
{
    test tarray[5];
    test newobj(31,'z');
};

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


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()};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜