Copy Constructor in C++ | Copy Constructor Example | C++ Programming Language | C++ Programming Tutorial | C++ Programming Examples | C++ Programming Books

Copy Constructor in C++ | Copy Constructor Example | C++ Programming Language | C++ Programming Tutorial | C++ Programming Examples | C++ Programming Books

Copy Constructor in C++ | Copy Constructor Example | C++ Programming Language | C++ Programming Tutorial | C++ Programming Examples | C++ Programming Books |

Copy Constructors

The duplicate constructor is a constructor which makes a protest by instating it with a question of a similar class, which has been made beforehand. The duplicate constructor is utilized to −



Introduce one question from one more of a similar sort.

Duplicate a protest pass it as a contention to a capacity.

Duplicate a protest return it from a capacity.




In the event that a duplicate constructor is not characterized in a class, the compiler itself characterizes one.If the class has pointer factors and has some unique memory assignments, at that point it is an unquestionable requirement to have a duplicate constructor. The most widely recognized type of duplicate constructor is appeared here −

classname (const classname &obj) {

/collection of constructor

}



Here, obj is a reference to a protest that is being utilized to introduce another question.

#include <iostream>

utilizing namespace sexually transmitted disease;

class Line {

open:

int getLength( void );

Line( int len );/basic constructor

Line( const Line &obj);/duplicate constructor

~Line();/destructor

private:

int *ptr;

};

/Member capacities definitions including constructor

Line::Line(int len) {

cout << “Typical constructor designating ptr” << endl;

/designate memory for the pointer;

ptr = new int;

*ptr = len;

}

Line::Line(const Line &obj) {

cout << “Duplicate constructor designating ptr.” << endl;

ptr = new int;

*ptr = *obj.ptr;/duplicate the esteem

}

Line::~Line(void) {

cout << “Liberating memory!” << endl;

erase ptr;

}

int Line::getLength( void ) {

return *ptr;

}

void display(Line obj) {

cout << “Length of line : ” << obj.getLength() <<endl;

}

/Main capacity for the program

int fundamental() {

Line line(10);

display(line);

return 0;

}

At the point when the above code is ordered and executed, it creates the accompanying outcome −

Ordinary constructor designating ptr

Duplicate constructor assigning ptr.

Length of line : 10

Liberating memory!

Liberating memory!

Give us a chance to see a similar illustration yet with a little change to make another protest utilizing existing object of a similar sort −

#include <iostream>

utilizing namespace sexually transmitted disease;

class Line {

open:

int getLength( void );

Line( int len );/straightforward constructor

Line( const Line &obj);/duplicate constructor

~Line();/destructor

private:

int *ptr;

};

/Member capacities definitions including constructor

Line::Line(int len) {

cout << “Ordinary constructor designating ptr” << endl;

/assign memory for the pointer;

ptr = new int;

*ptr = len;

}

Line::Line(const Line &obj) {

cout << “Duplicate constructor designating ptr.” << endl;

ptr = new int;

*ptr = *obj.ptr;/duplicate the esteem

}

Line::~Line(void) {

cout << “Liberating memory!” << endl;

erase ptr;

}

int Line::getLength( void ) {

return *ptr;

}

void display(Line obj) {

cout << “Length of line : ” << obj.getLength() <<endl;

}

/Main capacity for the program

int primary() {

Line line1(10);

Line line2 = line1;/This additionally calls duplicate constructor

display(line1);

display(line2);

return 0;

}

At the point when the above code is incorporated and executed, it delivers the accompanying outcome −

Typical constructor allotting ptr

Duplicate constructor designating ptr.

Duplicate constructor designating ptr.

Length of line : 10

Liberating memory!

Duplicate constructor designating ptr.

Length of line : 10

Liberating memory!

Liberating memory!

Liberating memory!

 

Thanks for reading this post and don’t forget to share it and keep visits for more updates



About Ashok.cse

Hi, I'm currently studying for the Bachelor of Technology In Computer Science from Rajasthan Technical University. I am the web developer with 3+ years of experience. my range of developer services including creating the design for new apps and web platforms or building UI design for mobile, tablets or desktops. making UX designs wireframes and layouts concepts and redesigns websites are adapting to mobile and responsive design. I have worked with many clients. I am working professionally with WordPress And Adobe tools.

View all posts by Ashok.cse →

Leave a Reply

Your email address will not be published. Required fields are marked *