Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why do you seldom need to overload the assignment operator? In what situation is

ID: 3572170 • Letter: W

Question

Why do you seldom need to overload the assignment operator? In what situation is it necessary to define an assignment operator? What is the purpose of the explicit keyword? What potential error does the use of this keyword eliminate? Which operators does the string class overload? What unique characteristic makes the function call operator different from all other operators? What is a function object? How is a function object different from a function? What capabilities does a function object possess that an ordinary function does not?

Explanation / Answer

Hi, I have answered first two questions in details. Please repost other questions in separate post.


6)
   so, assignment operator only works for primitive data type. like- int , float, double, char

   It does not work for User defined data tupes, like- class, structure

   So, to use assignemnet operator with user defined data type(class object), you need to overload '=' operator

   Ex:
   class Complex
   {
   private:
   double real;
   double imag;
     
   public:
  
   Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
     
   // A method to compare two Complex numbers
   bool operator== (Complex rhs) {
   return (real == rhs.real && imag == rhs.imag)? true : false;
   }
   };

   Now we can compare two complex number
  

7)
   This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object.

   if a class has a constructor which can be called with a single argument, then this constructor becomes conversion constructor because such a constructor allows conversion of the single argument to the class being constructed.
   We can avoid such implicit conversions as these may lead to unexpected results. We can make the constructor explicit with the help of explicit keyword

   For example, if we try the following program that uses explicit keyword with constructor, we get compilation error.

  
   class Complex
   {
   private:
   double real;
   double imag;
     
   public:
   // Default constructor
   explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
     
   // A method to compare two Complex numbers
   bool operator== (Complex rhs) {
   return (real == rhs.real && imag == rhs.imag)? true : false;
   }
   };
     
   int main()
   {
   // a Complex object
   Complex com1(3.0, 0.0);
     
   if (com1 == 3.0)
   cout << "Same";
   else
   cout << "Not Same";
   return 0;
   }
  
   Output: Compiler Error

   no match for 'operator==' in 'com1 == 3.0e+0'


   We can still typecast the double values to Complex, but now we have to explicitly typecast it. For example, the following program works fine.
     
   class Complex
   {
   private:
   double real;
   double imag;
     
   public:
   // Default constructor
   explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
     
   // A method to compare two Complex numbers
   bool operator== (Complex rhs) {
   return (real == rhs.real && imag == rhs.imag)? true : false;
   }
   };
     
   int main()
   {
   // a Complex object
   Complex com1(3.0, 0.0);
     
   if (com1 == (Complex)3.0)
   cout << "Same";
   else
   cout << "Not Same";
   return 0;
   }
  
   Output: The program compiles fine and produces following output.

   Same

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote