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:-

  • Academic Journal Writing Looking for online help in writing academic journals, looking for assessments writing solutions for writing an academic journals, get help from online experts.
  • Optics-Light Physics Get Optics-Light Physics Assignment Help Online, assessment help and Writing Service from Physics Assignment Experts.
  • Budget and Budgeting Controls Looking for help with assignments in budget and budgeting controls, seeking accounting tutors advice for solving problems of budget and budgeting controls?
  • Biochemical Engineering Finding solutions to biochemical engineering problems, looking for biochemical engineering assignment help - assessments writing service, papers editing service
  • Biochemistry are you looking for biochemistry tutors for writing your biochemistry assignment and assessment? Live biochemistry tutors are helping students in assignments.
  • Thermodynamic Processes Get Thermodynamic Processes Assignment Help Online, assessment help and Writing Service from Physical chemistry Assignment Experts.
  • Accounting Concepts Need Accounting Concepts & homework help? Are you seeking trusted tutor's advice for accounting concepts and assignment help?
  • HRM Assignment Help Are you looking for HRM assignment help and HRM assessments writing service, HRM top tutor service offers HRM homework help around the clock.
Captcha

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