Is it necessary to use default constructor in this eg of operator overloading? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; } };

6th Aug 2018, 1:03 PM
Neenadk
Neenadk - avatar
4 Answers
+ 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
7th Aug 2018, 1:11 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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
7th Aug 2018, 1:38 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
you might like to check this code : https://code.sololearn.com/c1mokvYx48pF/?ref=app
7th Aug 2018, 1:40 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I am asking why default constructor is called on the above code is it necessary or they have just mentioned it
7th Aug 2018, 1:35 PM
Neenadk
Neenadk - avatar