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

  • Electrochemistry Finding expert in electrochemistry who may solve your electrochemistry assignment or homework? Get electrochemistry assignment help and assessments writing help
  • Narrative Essay Looking for essay writing service? want to buy a perfect narrative essay online? We help students in writing narrative essay, a perfect essay writing service.
  • Dissertation Looking for live dissertation writers for taking help online in writing your college dissertation, Want to prepare A++ high scoring dissertation in cheap price?
  • Aerospace Engineering Looking for Aerospace Engineering Assignment Help, assessments writing service online, Finding Aerospace Engineering tutors to solve problems online?
  • Ray optics and optical instruments Get Ray optics and optical instruments Assignment Help Online, assessment help and Writing Service from General Physics Assignment Experts.
  • Cash flow and Fund flow statements seeking help in problems of cash flow, fund flow statements? looking for cash flow, fund flow statements assignment help-assessments help? live finance tutors
  • Quantum Gravity Get Quantum Gravity Assignment Help Online, assessment help and Writing Service from Physics Assignment Experts.
  • Treasury and Working Capital Management Looking for tutor/expert service to write finance assignment? seeking help in writing treasury and working capital management assignments? get assignment help.
Captcha

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