Skip to main content

Posts

Showing posts with the label OOP

What is Operator Overloading in c++

  Who doesnot love syntactic sugar ? After all they make our code look more beautiful. So let us discuss how to  overload operators in C++ How to Overload Operators in C++ What if I tell you ,You can use most of the operator in C++ not only with built in type but with the  type you defined yourself (Classes are user defined type) .Yes you got it right , operator overloading lets you define the meaning of  an operator when used to operands of user defined type(classes) Overloaded operator are nothing else than a function (however a special one) with a special keyword operator followed by the symbol for the operator to be overloaded. As I already mentioned that overloaded opeartors are a function so they definetly have a return type,parameter(s) and a body. It should look something like this: returnType operator Symbol(){} As an example:   int   operator  +(){ // body } Let us now overload   plus operator (+) for our class . 1.# include < iostream > 2.using   namespace  std; 3. 4