Home | | Programming and Data Structure II | Copy Constructor

Chapter: Programming and Data structures : Object Oriented Programming Concepts

Copy Constructor

This constructor takes one argument, also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.

COPY CONSTRUCTOR:

 

This constructor takes one argument, also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.

 

For example to invoke a copy constructor the programmer writes:

 

Exforsys e3(e2);

 

or

 

Exforsys e3=e2;

 

Both the above formats can be sued to invoke a copy constructor.

 

 

For Example:

 

 

#include <iostream.h>

 

using namespace std;

 

class Exforsys

 

{

 

private:

 

int a; public:

 

Exforsys() { }

 

Exforsys(int w)

 

{

 

a=w;

 

}

 

Exforsys(Exforsys& e)

 

{

 

a=e.a;

 

cout << " Example of Copy Constructor";

 

}

 

void result()

 

{

 

cout<< a;

 

}

 

};

 

void main()

 

{

 

Exforsys e1(50); Exforsys e3(e1); cout<< "ne3="; e3.result();

}

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : Object Oriented Programming Concepts : Copy Constructor |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.