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

Sponsored Links



what are constructors in C++?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 29-12-2010
Member
 
Join Date: Mar 2010
Posts: 456
what are constructors in C++?
  

Hey Guys, I wanted know what are constructors in C++? Well, I have heard about this but I have no idea of its working. So, If anyone having any information or knowledge about this term than please let me know as soon as possible. I very keen to know this term as I want to know and execute a program on this term. Well, can anyone tell me what is the connection of object initialized and the constructor? If anybody who knows athe solution for this query then please let me know as soon as possible.

Reply With Quote
  #2  
Old 29-12-2010
Member
 
Join Date: May 2009
Posts: 523
Re: what are constructors in C++?

When a object is created, the variable of the objects must be initialized. Initialization of variable of object is done automatically when the object is created .The object is initialized at the time of creation of it by means of a special function. This special function is known as constructor. It is called as constructor because it constructs the value of data member of class. You must know that constructor is created when an object is initialized.
Reply With Quote
  #3  
Old 29-12-2010
Member
 
Join Date: May 2009
Posts: 504
Re: what are constructors in C++?

Characteristics of constructor
• The name of the constructor should be always on the name of class.
• Constructor will be called automatically whenever the object is created.
• They do not have any return value.
• They should be always declared in public section.
• They will be called only once for each object
• They cannot be inherited.
This are characteristics of a constructor where it follows all of the above points. When you define a class, you should know that an object should be initialized and it should be created. So, it is necessary to follow this points. You should also be aware that a constructor does not returns the value. This is what many of them are still not aware of it. One more important thing is that it should be always be created in public section not in private or protected section.
Reply With Quote
  #4  
Old 29-12-2010
Member
 
Join Date: May 2009
Posts: 532
Re: what are constructors in C++?

Code:
For example
class Box
    {
         int l,w,h;

public:
          Box()            //  constructor
           { 
             l=10;
 h=20;
 w=30;
}
          }

     main()
        {
          Box b1;
         }

This is an simple example of constructor which is shown as above. In this an object is initialized and how it is created is shown. You will find that the box named constructor is created in the program. The name of the constructor and the class should be similar which is also shown. After initialization the value of objects are also assigned in the program. This is the most simplest and easiest program of constructor in C++.
Reply With Quote
  #5  
Old 29-12-2010
Member
 
Join Date: May 2009
Posts: 525
Re: what are constructors in C++?

When a object b1 of class Box is created the control will automatically transfer to the constructor and execute the statements written in side the constructor, which initializes the variable l, h, w of object b1;
The constructor mentioned above does not have arguments so such type of constructor is know as constructor without argument. If a class does not define any constructor then c++ creates a constructor when the object of class is created and initializes all variable of object to value 0.such kind of constructor is known as default constructor.
Reply With Quote
Reply

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



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "what are constructors in C++?"
Thread Thread Starter Forum Replies Last Post
Constructors in Java help cloud101 Software Development 4 18-01-2012 05:02 PM
Constructors and Destructors in PHP garfield1 Software Development 4 02-03-2010 11:01 PM
Are constructors in Csharp inherited? Joel5 Software Development 3 14-11-2009 06:47 PM
Static Constructors in C# (C sharp ) NICKAN Software Development 1 02-03-2009 01:05 PM
What are the Constructors and Destructors? RupaliP Software Development 4 27-02-2009 07:03 PM


All times are GMT +5.5. The time now is 04:54 PM.