1.

Explain a copy constructor with the help of an example.

Answer»

A copy constructor is a member function that uses another object of the same CLASS to initialize an object. Our copy constructor can be defined by us. If no copy constructor is defined, the DEFAULT copy constructor is used.

// Including all the header files#include<bits/stdc++.h>// class SHOWING the usage of a copy constructor class Fun{ long long a,B; Fun(long long _a, long long _b){ this -> a = _a; this -> b = _b; }};// MAIN function of the C++ programint main(){ Fun obj1(5LL,7LL); Fun obj2 = obj1;//In this line, the copy constructor will be called return 0;}

The above-mentioned code snippet shows how the copy constructor is used to define the object "obj2" using the already existing object "obj1".



Discussion

No Comment Found