Home > Net >  Operator Overloading C for operator
Operator Overloading C for operator

Time:10-19

I am currently learning how to do operator overloading in C , i found some codes online that works when it is run below.

class Complex {
 private:
  int real, imag;

 public:
  Complex(int r = 0, int i = 0) {
    real = r;
    imag = i;
  }

  // This is automatically called when ' ' is used with
  // between two Complex objects
  Complex operator (Complex const &obj) {
    Complex res;
    res.real = real   obj.real;
    res.imag = imag   obj.imag;
    return res;
  }
  void print() { cout << real << "   i" << imag << endl; }
};

int main() {
  Complex c1(10, 5), c2(2, 4);
  Complex c3 = c1   c2;  // An example call to "operator "
  c3.print();
}

However when i tried a similar structure, i received the following error : no default constructor found and << error which i am not familiar with.

class Chicken {
 private:
  int i;
  int k;
  int s;
  int total = 0;

 public:
  Chicken(int index, int kentucky, int sign) {
    i = index;
    k = kentucky;
    s = sign;
  }
  Chicken operator (Chicken obj) {
    Chicken Chicky;
    Chicky.total = i   obj.i;
    return Chicky;
  }
};

int main() {
  Chicken P(1, 2, 3);
  Chicken U(2, 2, 2);
  Chicken W = P   U;

  cout << W;
}

enter image description here

CodePudding user response:

Below is the corrected example. First, you were getting the error because inside operator you were default constructing an object of Chicken type but your class doesn't have any default constructor. The solution is to add the default constructor.

#include <iostream>
class Chicken{
    //needed for cout<<W; to work
    friend std::ostream& operator<<(std::ostream& os, const Chicken &rhs);
    private:
      int i = 0;
      int k = 0;
      int s = 0;
      int total = 0; 

    public:

    

    //default constructor 
    Chicken() = default;
    
    
    //use constructor initializer list instead of assigning inside the body
    Chicken(int index, int kentucky, int sign): i(index), k(kentucky), s(sign){

        //i = index;
        //k = kentucky;
        //s = sign;
    }
    Chicken operator   (Chicken obj){
        Chicken Chicky;//this needs/uses default constructor
        Chicky.total = i   obj.i;
        return Chicky;
    }
   
};
std::ostream& operator<<(std::ostream& os, const Chicken &rhs)
{
    os << rhs.i << rhs.k << rhs.s << rhs.total ;
    return os;
}
int main(){
    
   
    
    Chicken P(1,2,3); //this uses parameterized constructor
    Chicken U(2,2,2); //this uses parameterized constructor
    Chicken W = P U;  //this uses overloaded operator 
    
    std::cout << W;   //this uses oveloaded operator<<
    
}
 

Second, you were using the statement cout<<W; but without overloading operator<<. For std::cout<< W; to work you will need to overload operator<< as i have shown in the above code.

You can check out the working example here.

Third, you can/should use constructor initializer list instead of assigning values inside the body of the constructor as shown above.

  • Related