Copy Constructor, C++ tutorial

Copy Constructor

The copy constructor is used to copy the content of one object to another object. A copy constructor always takes a reference object as an argument. A copy constructor can be defined in two ways:

i) Inside the class definition

ii) Outside the class definition

i)  Inside the class definition

The general form of defining a copy constructor holds the following syntax.

class classname

{

        private:

                //Data members

        public:

                classname(classname &object_name)

                {

                        //Copy Constructor Statements

                }

};

ii) Outside the class definition

The general form of defining a copy constructor outside the class holds the following syntax.

class classname

{

        private:

                //Data members

        public:

                classname(classname &object_name); // Copy Constructor                       prototype

};


classname::classname(classname &object_name) < --------------------- Outside the class

{

        // Copy  Constructor Statements

}

Remember

When the copy constructor is defined outside the class, its prototype must be declared in the public category of the class. 

Invoking a copy constructor

We must pass the initial values as arguments to copy constructor after initializing the source object

Method1

class_name destination_object=source_object;

Example

          demo d1(4500);

        demo d2=d1;

Method2

class_name destination_object(source_object);

Example

          demo d1(4500);

        demo d2(d1);

The programmer can use either of the methods.

Program 4.3

Write a program to provide same salary to three employees using copy constructor.

#include<iostream.h>

#include<constream.h>

class employee

{

        private:

                int salary;

        public:

                employee(int);

                employee(employee&);

                void display();

};

employee::employee(int val)

{

        salary=val;

}

employee::employee(employee &e)

{

        salary=e.salary;

}

void employee::display()

{

        cout<<salary<<endl;

}

main()

{

        clrscr();

        employee e1(4500);

        employee e2(e1);

        employee e3=e2;

        cout<<"Salary of  employee1 :";e1.display();

        cout<<"Salary of  employee2 :";e2.display();

        cout<<"Salary of  employee3 :";e3.display();

        return(0);

}

Output

Salary of  employee1 :4500

Salary of  employee2 :4500

Salary of  employee3 :4500

Explanation

In the above program, the source object e1 is initialized first, then its content is assigned to destination object e2 and e3 as declared in the main( ) function.

How we help you? - C++ Assignment Help 24x7

We offer C++ assignment help, C++ assignment writing help, programming assessments writing service, C++ tutors support, step by step solutions to Polymorphism problems, Constructor programming answers, C++ assignment experts help online. Our assignment help service is most popular and browsed all over the world for each grade level.

Why choose us - The first thing come in your mind that why choose us why not others what is special and different about us in comparison to other site. As we told you our team of expert, they are best in their field and we are always live to help you in your assignment which makes it special.

Key features of services are listed below:

  • Confidentiality of student private information
  • 100% unique and original solutions
  • Step by step explanations of problems
  • Minimum 4 minutes turnaround time - fast and reliable service
  • Secure payment options
  • On time delivery
  • Unlimited clarification till you are done
  • Guaranteed satisfaction
  • Affordable price to cover maximum number of students in service
  • Easy and powerful interface to track your order

assignment help

Popular Writing Services:-

  • Stoichiometry Get Stoichiometry Assignment Help Online, assessment help and Writing Service from Physical chemistry Assignment Experts.
  • R Programming Statistics Get r programming statistics assignment help online, r-programming assignment writing service from statistics assignment experts.
  • Reaction Engineering Get Reaction Engineering Assignment Help Online, Assignment Writing Service from Engineering Assignment Experts.
  • Astronomy Ask a astronomy expert and get answer, astronomy assignment help and assessments writing service online from qualified physics experts.
  • Set Theory get set theory assignment help online, math assignment writing service, mathematics solutions in set theory from maths assignment experts.
  • Moments of inertia Get Moments of inertia Assignment Help Online, assessment help and Writing Service from Motions and forces Assignment Experts.
  • Training and Development What is benefit of HRM training and development? HRM Training and Development tutorial and homework help - assignment help assistance at low cost.
  • Capital Budgeting Are you Getting stock with Capital Budgeting problems? Save time and money acquire Capital Budgeting assignment help from qualified accounting tutors.
Captcha

Get Academic Excellence with Best Skilled Tutor! Order Assignment Now! Submit Assignment