Q:

What is the difference between a copy constructor and an overloaded assignment operator?

0

What is the difference between a copy constructor and an overloaded assignment operator?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

#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;
}

Output:

Assignment operator called.
Copy constructor called.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now