开发者

array of objects of a class

开发者 https://www.devze.com 2023-01-01 08:58 出处:网络
#include<iostream.h> class test{ int a; char b; public: test() { cout<<\"\\n\\nDefault constructor being called\";
#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

精彩评论

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