Results 1 to 5 of 5

Thread: what are constructors in C++?

  1. #1
    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.

  2. #2
    Join Date
    May 2009
    Posts
    527

    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.

  3. #3
    Join Date
    May 2009
    Posts
    513

    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.

  4. #4
    Join Date
    May 2009
    Posts
    542

    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++.

  5. #5
    Join Date
    May 2009
    Posts
    529

    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.

Similar Threads

  1. Constructors in Java help
    By cloud101 in forum Software Development
    Replies: 4
    Last Post: 18-01-2012, 04:02 PM
  2. Constructors and Destructors in PHP
    By garfield1 in forum Software Development
    Replies: 4
    Last Post: 02-03-2010, 10:01 PM
  3. Are constructors in Csharp inherited?
    By Joel5 in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 05:47 PM
  4. Static Constructors in C# (C sharp )
    By NICKAN in forum Software Development
    Replies: 1
    Last Post: 02-03-2009, 12:05 PM
  5. What are the Constructors and Destructors?
    By RupaliP in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 06:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •