Object Oriented Programming
26th may 2008
What is operator overloading?
Changing the definition of an operator so it can be applied on the objects of a class is called operator overloading. To overload an operator, we need to write a function for the operator we are overloading.
What is operator overloading?
Is done by declaring a function with a name consisting of the keyword operator followed by one of the built-in operators. New operators cannot be defined. Makes no assumptions about similar operators. For example, the fact that you overloaded + does not mean that you have also defined += for your class type. Cannot use default arguments.
Special cases
The assignment operator (=) may be used with every class without explicit overloading. The default behavior of the assignment operator is a member-wise assignment of the data members of the class. The address operator (&) may also be used with objects of any class without explicit overloading.It returns the address of the object in memory.
General Format-Operator Overloading
return_type classname :: operator operator_symbol(argument)
{ .. statements; } class class name { private: . public: void operator operator_symbol(argument); };
Operators in C++
C++ has a rich collection of operators most of which are common to other programming languages It is the standard arithmetic and logical operators
+ - * / % & ! >> << || && == etc
It has the array indexing and function evaluation operators
[] ()
It has the assignment operators
= += -= *= /= &= |= etc
It has the auto increment and decrement operators
++ --
It has the pointer de-referencing and address of operators
* &
It has the memory management operators
new delete new[] delete[]
We can divide up the set of operators into unary and binary operators
A unary operator has one operand Examples
if (!x) {..} x++; --x; // unary operator !, operand x // post-fix operator ++, operand x / / pre-fix operator --, operand x
int y=a[5]; // operator [], operand a
Unary Operator Overloading Example
#include <iostream.h> class Integer { private: int x; public: Integer( ) { x=0; } //Constructor void display(); void operator ++( ); }; void Integer :: display() { cout<<\nValue of x is: << x; } void Integer :: operator ++( ) //Operator Overloading for operator ++ defined { ++x; }
Unary Operator Overloading Example
void main( ) { Integer e1,e2; //Object e1 and e2 created cout<<Before Increment cout <<\nObject e1: <<e1.display(); cout <<\nObject e2: <<e2.display(); ++e1; //Operator overloading applied ++e2; cout<<\n After Increment cout <<\nObject e1: <<e1.display(); cout <<\nObject e2: <<e2.display(); }
Unary Operator Overloading Example
Before Increment Object e1: Value of x is: 0 Object e2: Value of x is: 0 Before Increment Object e1: Value of x is: 1 Object e2: Value of x is: 1
A binary operator has two operands
Examples
int z=x+y; bool z=x&&y; x+=y; // binary operator +, operands x and y // binary operator && operands x and y // binary operator +=, operands x and y
Binary Operator Overloading Example
Class add{
Private:
int x;
Public: add(int y) {x = y;}
void display() {cout<<x<<endl;} add operator +(add a) { add temp;
temp.x = x+a.x; return temp;}
}; Void main() { add a1(10),a2(20),a3(0); a3 = a1+a2; a3.display(); }
Class dist{ private:
Arithmetic Assignment Operator
int feet; float inches;
public: dist(int a,float b) { feet = a; inches = b; } void display() {cout<<feet<< <<inches<<endl;} void operator +=(dist d) { feet += d.feet; inches += d.inches; if(inches>12.0) { inches -=12.0; feet++; }};
Void main() { dist d1(6,3),d2(10,5.0); d1+=d2; d1.display(); }
Comparison Operator-Example
Class str{ private: char s[10]; public: str(char s1[]) { strcpy(s,s1);} void display() {cout<<s;} bool operator ==(string ss) const { return (strcmp(s,ss.s)==0)? True:false;} }; Void main() { str t = yes; str t1 = no; if(t == t1) t.display(); Else t1.display(); }
Class cat{ Private:
Concatenating Strings
string str;
public: string operator+(string s) { strcat(str,s) return str;}}; Void main() { string s1 = hello; String s2 = world; String s3 = s1+s2; Cout<<s3; }
Restrictions on operator overloading The precedence of an operator cannot be changed The arity of an operator cannot be changed We cannot redefine a binary operator to be unary or vice verse The associativity of an operator cannot change Whether it applies left to right or right to left