A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.
#include<iostream>
using namespace std;
class Demo
{
public:
Demo() {}
Demo(const Demo &obj)
{
cout<<"Copy constructor called "<<endl;
}
Demo& operator = (const Demo &obj)
{
cout<<"Assignment operator called "<<endl;
return *this;
}
};
int main()
{
Demo a, b;
//calls assignment operator
b = a;
// calls copy constructor
Demo c = a;
return 0;
}
Answer:
A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.
Output:
Assignment operator called.
need an explanation for this answer? contact us directly to get an explanation for this answerCopy constructor called.