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

  • Economics economics experts are offering economics assignment help, economics solutions, economics writing help, project help, assessments help, buy economics assignment.
  • X-Ray Crystallography seeking help in X-Ray Crystallography assignments or questions? finding chemistry tutors who can write your X-Ray Crystallography homework?
  • States of Matter Get States of Matter Assignment Help Online, assessment help and Writing Service from Physical chemistry Assignment Experts.
  • Classification and Division Essay Looking for reliable essay writing service? Seeking help in writing Writing a division and classification essay? Live essay writers support to write essay.
  • Visual Basic Are you searching visual basic programmer online? Want to take assistance in projects or assignments? get visual basic solved projects and assessments.
  • Botany get botany assignment help online, botany assessments help, biology writing service from biology assignment experts.
  • Classes and Objects classes and objects in C++. types of class, objects and concepts behind object, declaration of a class, tutorial on C++, assignment help and C++ tutor service
  • Econometrics get econometrics assignment help online, econometrics assignment writing service from economics assignment experts.
Captcha

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