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

  • Engineering get engineering assignment help, engineering assessments writing service, engineering papers writing service, engineering problems solutions, assessments help.
  • Satirical Essay How to draft perfect Satirical Essay? Satirical Essay Example and Structure - Acquire trusted writers and Buy Satirical Essay at cost effective price.
  • Speed Get Speed Assignment Help Online, assessment help and Writing Service from Motions and forces Assignment Experts.
  • Leadership Theory Acquire Leadership assignment help and assessment writing services from qualified tutors. get solved homework in leadership theory.
  • Game Theory get game theory assignment help online, solutions, assessments writing service from game theory economics assignment experts.
  • Solutions Get Solutions Assignment Help Online, assessment help and Writing Service from Physical chemistry Assignment Experts.
  • Business Plan prepare your business plan online, get business plan writing service, business plan assignment help online, buy business plan from assignment experts.
Captcha

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