Results 1 to 10 of 10

What is copy constructor?

This is a discussion on What is copy constructor? within the Programming forums, part of the Web Designing & Development category; Constructor which initializes the it's object member variables ( by shallow copying) with another object of the same class. If ...

  1. #1
    Senior Member chinmay.sahoo's Avatar
    Join Date
    Dec 2013
    Location
    Banglore
    Posts
    266

    Thumbs up What is copy constructor?

    Constructor which initializes the it's object member variables ( by shallow copying) with another object of the same class. If you don't implement one in your class then compiler implements one for you. for example:


    (a) Boo Obj1(10); // calling Boo constructor
    (b) Boo Obj2(Obj1); // calling boo copy constructor
    (c) Boo Obj2 = Obj1;// calling boo copy constructor

  2. #2
    Senior Member
    Join Date
    Jan 2016
    Posts
    145

    Default Re: What is copy constructor?

    The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to: Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.

  3. #3
    Senior Member
    Join Date
    Jan 2012
    Posts
    1,894

    Default Re: What is copy constructor?

    The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to: Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.

  4. #4
    Senior Member dennis123's Avatar
    Join Date
    Apr 2013
    Location
    Bangalore
    Posts
    2,568

    Default Re: What is copy constructor?

    The copy constructor is used only for initializations, and does not apply to assignments where the assignment operator is used instead. The implicit copy constructor of a class calls base copy constructors and copies its members by means appropriate to their type. If it is a class type, the copy constructor is called.

  5. #5
    Member
    Join Date
    Jul 2017
    Posts
    63

    Default Re: What is copy constructor?

    A copy constructor initialises a new object from an existing (previously created) object of the same type. Essentially, it is supposed to make a deep copy of an existing object.

  6. #6
    Senior Member
    Join Date
    Oct 2017
    Posts
    119

    Default Re: What is copy constructor?

    A copy constructor is a special constructor for a class/struct that is
    used to make a copy of an existing instance

  7. #7
    Senior Member
    Join Date
    Jun 2017
    Location
    India
    Posts
    307

    Default Re: What is copy constructor?

    The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to − Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.

  8. #8
    Member
    Join Date
    Mar 2018
    Location
    Santa Clara, California, USA
    Posts
    40

    Default Re: What is copy constructor?

    A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values. Copy constructor creates a new object of the same class using an existing object. It creates a copy/replica of the existing object.

  9. #9
    Member
    Join Date
    Aug 2017
    Location
    India
    Posts
    74

    Default Re: What is copy constructor?

    Copy constructor is a constructor which creates an object by initializing it with an object of the same class name.
    It is called when a new object is created from an existing object.


    Syntax :


    ClassName (const ClassName &old_obj);


    eg. class class_name
    {
    public:
    class_name(class_name & obj)
    {
    ....
    }


    Declaration in Main ()

    eg. class_name object1(params);

    Mehod 1 -copy constructor
    class_name object2(object1);

    Method 2-Copy Constructor
    class_name object3 = object1;

  10. #10
    Junior Member
    Join Date
    Apr 2018
    Posts
    15

    Default Re: What is copy constructor?

    Copy constructor creates a copy/replica of the existing object of the same class.It copies value of all member variables from existing object to new object. It copies values member-to-member.It is used when the class has pointer variables and has dynamic allocations,then it is a must to have a copy constructor.

Tags for this Thread