Is it necessary to use default constructor in this eg of operator overloading? | SoloLearn: Learn to code for FREE!

0

Is it necessary to use default constructor in this eg of operator overloading?

class MyClass { public: int var; MyClass() {} MyClass(int a) : var(a) { } MyClass operator+(MyClass &obj) { MyClass res; res.var= this->var+obj.var; return res; } };

8/6/2018 1:03:04 PM

Neenadk

4 Answers

New Answer

+1

coder , I am sorry but could not get your question... Do you mean to say that default constructor is to be defined in class or not.Plz correct me if i misunderstood your question.. Let me answer this : If your main function is going to call only parameterized constructor, then default is not required... for example; Myclass obj1(5); // this type of call in main function don't ask for default constructor if along with above , if you call something like this (Myclass obj2;), default constructor is must in class

+1

it's simply class definition.. default constructor is required or not is dependent on how you create object of class...do share full code and then can comment whether it is required or not

+1

you might like to check this code : https://code.sololearn.com/c1mokvYx48pF/?ref=app

0

I am asking why default constructor is called on the above code is it necessary or they have just mentioned it