Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 04-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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.
Reply With Quote
  #3  
Old 04-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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.
Reply With Quote
  #4  
Old 04-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 09:12 PM.