Download presentation
Presentation is loading. Please wait.
Published byAmberlynn Edwards Modified over 9 years ago
1
Operator overloading Object Oriented Programming
2
Introduction Operator overloading – Enabling C++’s operators to work with class objects – Using traditional operators with user-defined objects – Requires great care; when overloading is misused, program difficult to understand – Examples of already overloaded operators Operator << is both the stream-insertion operator and the bitwise left-shift operator + and -, perform arithmetic on multiple types – Compiler generates the appropriate code based on the manner in which the operator is used
3
Introduction Overloading an operator – Write function definition as normal – Function name is keyword operator followed by the symbol for the operator being overloaded – operator+ used to overload the addition operator ( + ) Using operators – To use an operator on a class object it must be overloaded unless the assignment operator (=) or the address operator (&) Assignment operator by default performs memberwise assignment Address operator (&) by default returns the address of an object
4
Restrictions on Operator Overloading C++ operators that can be overloaded C++ Operators that cannot be overloaded
5
Restrictions on Operator Overloading Overloading restrictions – Precedence of an operator cannot be changed – Associativity of an operator cannot be changed – Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately No new operators can be created – Use only existing operators No overloading operators for built-in types – Cannot change how two integers are added – Produces a syntax error
6
Operator Functions as Class Members vs. as friend Functions Member vs non-member – Operator functions can be member or non-member functions – When overloading ( ), [ ], -> or any of the assignment operators, must use a member function Operator functions as member functions – Leftmost operand must be an object (or reference to an object) of the class If left operand of a different type, operator function must be a non- member function Operator functions as non-member functions – Must be friend s if needs to access private or protected members – Enable the operator to be commutative
7
Overloading Stream-Insertion and Stream- Extraction Operators Overloaded > operators – Overloaded to perform input/output for user- defined types – Left operand of types ostream & and istream & – Must be a non-member function because left operand is not an object of the class – Must be a friend function to access private data members
8
DEFINING OPERATOR OVERLOADING return type classname :: operator op(arglist) { Function body // task defined } Operator functions can be member functions or friend functions Member functions : – No arguments for unary – One argument for binary For friend functions – Unary operator one argument – Binary operator two arguments
9
Invoking overloaded operator op x for unary x op operator For binary operator y op x x op y For friend function operator op (x)
10
Class counter { private : Unsigned int count; Public: Counter() : count(0) { } Void operator ++ () { ++Count; } Void operator++ (int)//postfix operation { Count++ } Counter C1,C2; c1++; ++c2; Example
11
Operator overloading we can overload all the c++ operators except Class member access operator(.,.*) Scope resolution operator (::) Size operator (sizeof) Conditional operator (?:)
12
Operator overloading Return type classname :: operator op(arglist) { //function body } Operator functions must be either member functions or friend functions It should be non-static.
13
Overloading unary operators Class space { int x,y,z; public: void getdata(int a, int b, int c); void display(); void operator – (); }; Void space :: getdata(int a, int b, int c) { x = a; y = b; z = c; } Void space :: display() { cout << “X Y Z” << x << y << z; }
14
Overloading unary operators Void space :: operator – () { x = -x; y = -y; z = -z; } int main() { space S; S.getdata(10, -20, 30); S.display(); -S; S.display(); return 0; }
15
Overloading Binary operators Class complex { float real, imag; public: complex () {} complex (float x, float y) { real = x; imag = y; } complex operator +(complex); void display(); } Void complex :: display () { cout << real << “+ i” << imag ; }
16
Example complex complex :: operator + (complex c) { complex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return (temp); } int main() { complex c1,c2,c3; c1 = complex (2.5, 3.5); c2 = complex (1.6, 2.7); c3 = c1 + c2 ; c1.display(); c2.display(); c3.display(); return 0; }
17
Overloading binary operators using friend function Declaration in class friend complex operator+(complex,complex); Definition complex operator+(complex,complex) { } Calling friend function C3=C1+C2;
18
Need of friend function One is object & another is built in data type A =B+2; A= 2*B; //wont work Left hand operand is responsible for invoking the function.
19
Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.