Explain the default action of the copy constructor. Write a suitable program that demonstrates the technique of overloading the copy constructor.

This answer is restricted. Please login to view the answer of this question.

Login Now

A constructor is a special member function used to initialize objects of its class. It will be called automatically when the object is created.

A class with two or more construct functions with the same name but with different parameters or arguments and other data types is called  Constructor overloading.

A copy constructor is a method or member function which initialize an object using another object within the same class.

Example:

#include <iostream>
using namespace std;

class ABC
{
    public:
        int x;

        // this is parameterized constructor
        ABC(int a){
            x = a;
        }

        // this is copy constructor
        ABC(ABC &i){
            x = i.x;
        }
};

int main()
{
    // Calling the parameterized constructor.
    ABC a1(40);

    // Calling the copy constructor.
    ABC a2(a1);

    cout << a2.x;
    return 0;
}
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .