Monday, 15 April 2019

Copy Constructor in C++

A Copy constructor is an overloaded constructor used to declare and initialize an object from another object. For example, the statement

Student s1(s2);

Would define the object s2 and at the same time initialize it to the values of s1. Another form of this statement is

Student s1=s2;

The above two statement are same. The process of initializing through a copy constructor is known as copy initialization.

A copy constructor takes a reference to an object of the same class as itself as an argument. For Example consider the following program

When Copy Constructor is called

Copy Constructor is called in the following scenarios:
  • When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class.
  • When the object of the same class type is passed by value as an argument.
  • When the function returns the object of the same class type by value.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

python tutorial

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top