Results 1 to 5 of 5

Thread: What is constructor overloading in c++

  1. #1
    Join Date
    Dec 2010
    Posts
    79

    What is constructor overloading in c++

    I’m a 1st year student of bsc-it and I don’t have any IT background. I want to know about constructor and constructor overloading in c++. I don’t understand the difference between the two, I’m new in c++ programming. Please explain me the why constructor is used and why constructor overloading is used. Please give me the difference between the two I’m very confused, please help me.

  2. #2
    Join Date
    Nov 2009
    Posts
    140

    Re: What is constructor overloading in c++

    constructor:
    Constructor is a special function of class. It is not the same but something similar like function. Constructor is a function which call automatically at the time of execution of program. The name of a constructor is same as the name of the class. Constructor can take parameter also, Constructor containing parameter are called parameterized constructor.
    Constructor overloading:

    overloading a constructor is same like function overloading.
    int cons( int a );
    double cons( double a );
    int cons( int a, int b );
    double cons( int a ); // _NOT_ ALLOWED
    in the above example we have declared the four constructor. The above two is differ in the type and third one is differ in the parameter, fourth one considered as equivalent to a first one therefore it is not allowed.

  3. #3
    Join Date
    May 2009
    Posts
    218

    Re: What is constructor overloading in c++

    A constructor is a special function which is call at the time of object creation. Constructor has same name as the name of a class. If we don’t take any constructor compiler call a default constructor.Overloading constructors is like overloading other function names. It is just the practice of defining more than one constructor for a class. There is also a copy constructor which you can see below in the example:
    class B
    {
    public:
    B(); // Default constructor
    B( B const & other ); // Copy constructor
    // ...
    };

  4. #4
    Join Date
    May 2009
    Posts
    201

    Re: What is constructor overloading in c++

    Constructor overloading is the same as function overloading in c++ and method overloading in java. The name of a constructor is the same as the name of a class. More than one Constructor having same name but different parameter are called and type is called constructor overloading. Here is an example of a constructor overloading:
    class Demo
    {
    private:
    int i;
    public:
    Demo(int i)
    {
    this->i = i;
    }
    Demo()
    {
    this->Demo(2)
    }
    };

  5. #5
    Join Date
    Nov 2009
    Posts
    687

    Re: What is constructor overloading in c++

    Constructor is called or created at the time of object creation. There are different types of constructor like default constructor, parameterized constructor, copy constructor.
    Default constructor:
    It is a constructor which is called when we don’t make any constructor in a program. The compiler by default call this constructor if he do not found any constructor in a program.
    Parameterized constructor:
    The constructor which contains parameter is called parameterized constructor.
    Copy constructor:
    A copy constructor for a class is a constructor that has an object of the class as its only parameter. It makes a new object that is a duplicate of the original object.
    Constructer overloading:
    Constructer overloading is same as the function overloading . you have to pass different argument to the parameter of a constructer at the time of execution i.e.. at the run time. Compiler matches the argument with constructer and call according to that.

Similar Threads

  1. Method overloading c#.net
    By raju_srk in forum Software Development
    Replies: 1
    Last Post: 22-11-2010, 07:00 PM
  2. The way to operator overloading in Java
    By Kohlmann in forum Software Development
    Replies: 8
    Last Post: 18-09-2010, 09:57 PM
  3. copy constructor Vs constructor
    By Truster in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:49 PM
  4. overloading of operators in C++
    By shilong in forum Software Development
    Replies: 3
    Last Post: 28-01-2009, 10:30 PM
  5. Overloading Assignment Operator and Copy constructor in C#
    By Japesh in forum Software Development
    Replies: 0
    Last Post: 14-01-2009, 03:05 PM

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,750,567,016.19677 seconds with 16 queries