CSIT Object Oriented Programming with C++(Rules for Operator Overloading ) Part II

I am assuming that, You have already studied Part I of Operator Overloading Series. So in this post, I am going to teach you about Rules That should be followed while performing operator Overloading.

So, let's get started,

The following Rules must be followed while doing operator overloading in C++.

  1. Operators that are already defined in the C++ compiler can be only overloaded. Operator can't change operator templates that is for example, the decrement operator '--' is used only as unary operator. It can not be used as binary operator.
  2. Overloading an operator does not change its basic meaning. for example assume the + operator can be overloaded to subtract two objects. But, still when we use it with two numbers, it finds sum of two numbers.
  3. Unary operators, overloaded by means of a member function, takes no explicit argument and return no explicit values. But, those overloaded by means of a friend function takes one reference argument(the object of relevant class).
  4. Binary operator overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments.
  5. Overloaded operators must either be a non-static class member function or a global function.
  6. A global function that needs access to private or protected class members must be declared as a friend function of that class. A global function must take at least one argument that is of class or enumerated type or that is a reference to a class or enumerated type.
These rules must be followed while doing Operator Overloading.


In next post, we will learn How to overload Unary operators. So refer next  post for that topic.

If This post Helped you, Please leave a comment, and share with your Coder Friends.
Thank You.

Keep Learning, Keep Growing



Comments

Popular posts from this blog

CSIT Object Oriented Programming with C++(Unary Operator Overloading using Friend function) Part IV

CSIT Object Oriented Programming with C++(Insertion and Extraction Operator Overloading ) Part VI

CSIT Object Oriented Programming with C++(Unary Operator Overloading) Part III