Results 1 to 4 of 4

Thread: Array of Objects to call constructor

  1. #1
    Join Date
    Sep 2009
    Posts
    135

    Array of Objects to call constructor

    I am a relatively a new user to C++. I am not that good a software programming. Recently while reading I read a question which stated "If we create Arrays of Object then which Constructor will be called?" I have no idea about which constructor will be called in this question. First of all , can we call constructor by creating an array of objects?
    Last edited by KALLIYAN; 04-11-2009 at 09:23 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Array of Objects to call constructor

    Yes, a constructor will be called. In C++ a constructor is called whenever we create an object of the class. In this case the Default Constructor if the class will be called.
    Here is an example to clearify the above statement:
    eg.

    Code:
    class temp
    {
     public:
       temp();
       ...
     };
     
     void main()
     {
       temp t[6];            
       temp* x = new temp[5];  
       ...
     }
    The first Array of objects t[6] calls the default constructor 6 times and the second will also call the default constructor but 5 times.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Array of Objects to call constructor

    Its by default that the C++ compiler will always call the default constructor first. As the name suggests its the default constructor and the compiler has no other options before the default constructor. And Yes, we can call default constructor by creating array of objects. Lets assume your class does not have a default constructor.

    Code:
    class def
    {
     int x;
    
     public:
    
       def(int a);      
       {
        x=a;
       }
    
     };
     
     int main()
     {
       def d[3];              
       ..
       ..
     }
    The code line def d[3] will generate an error as there is no default constructor in the class.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Array of Objects to call constructor

    Even I was faced with a similar problem when I started learning C++. I never knew anything about array of objects, but was asked a question that can we call the default constructor by creating an array of objects. But now I know, yes we can. Infact when we create an array of objects in C++ the comipler will first call the default constructor. Thus we need a default constructor when we create an array of objects. If the default constructor is not present the compiler genarates an error.

Similar Threads

  1. What is constructor overloading in c++
    By Mast Maula in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:34 AM
  2. Array of objects in java
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 02:05 AM
  3. copy constructor Vs constructor
    By Truster in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:49 PM
  4. One constructor to call another in C Sharp
    By Ivann in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 02:32 AM
  5. use of methods and constructor
    By beelow in forum Software Development
    Replies: 4
    Last Post: 14-11-2009, 01:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,828,306.78175 seconds with 16 queries