Presentation is loading. Please wait.

Presentation is loading. Please wait.

OPERATOR OVERLOADING Customised behaviour of operators.

Similar presentations


Presentation on theme: "OPERATOR OVERLOADING Customised behaviour of operators."— Presentation transcript:

1 OPERATOR OVERLOADING Customised behaviour of operators

2 Introduction  Operator overloading  Use traditional operators with user-defined objects  Straightforward and natural way to extend C++  Requires great care When overloading misused, program difficult to understand

3 Fundamentals of Operator Overloading  Use operator overloading to improve readability  Avoid excessive or inconsistent usage  Format  Write function definition as normal  Function name is keyword operator followed by the symbol for the operator being overloaded.  operator+ would be used to overload the addition operator ( + )

4 Restrictions on Operator Overloading  Most of C++’s operators can be overloaded

5 Restrictions on Operator Overloading (II)  Arity (number of operands) cannot be changed  Urnary operators remain urnary, and binary operators remain binary  Operators *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately

6 Restrictions on Operator Overloading (III)  No new operators can be created  Use only existing operators  Built-in types  Cannot overload operators  You cannot change how two integers are added

7 Coding Practices

8 Example: Operator Overloading class OverloadingExample { private: int m_LocalInt; public: OverloadingExample(int j) // default constructor { m_LocalInt = j; } int operator+ (int j) // overloaded + operator { return (m_LocalInt + j); } };

9 Example: Operator Overloading (contd.) void main() { OverloadingExample object1(10); cout << object1 + 10; }

10 Types of Operator  Unary operator  Binary operator

11 Unary Operators  Operators attached to a single operand (-a, +a, -- a, a--, ++a, a++)

12 Example: Unary Operators class UnaryExample { private: int m_LocalInt; public: UnaryExample(int j) { m_LocalInt = j; } int operator++ () { return (m_LocalInt++); } };

13 Example: Unary Operators (contd.) void main() { UnaryExample object1(10); cout << object1++; }

14 Prefix and Postfix Notation int operator++ () // Prefix { return (++m_LocalInt); } int operator++ (int) // postfix { return (m_LocalInt++); } Only difference is the int in the parenthesis

15 Binary Operators  Operators attached to two operands a+b, a-b, a*b etc.)

16 Example: Unary Operators class UnaryExample { private: int m_LocalInt; public: UnaryExample(int j) { m_LocalInt = j; } int operator++ () { return (m_LocalInt++); } };

17 Binary Operators  Operators attached to two operands (a-b, a+b, a*b, a/b, a%b, a>b, a>=b, a<b, a<=b, a==b)

18 Example: Binary Operators class BinaryExample { private: int m_LocalInt; public: BinaryExample(int j) { m_LocalInt = j; } int operator+ (BinaryExample rhsObj) { return (m_LocalInt + rhsObj.m_LocalInt); } };

19 Example: Binary Operators (contd.) void main() { BinaryExample object1(10), object2(20); cout << object1 + object2; }

20 Another Way of Doing it! How to solve object1 + object2 +object3+……?

21 class BinaryExampleU { private: int m_LocalInt; public: BinaryExampleU(int j) { m_LocalInt = j; } BinaryExampleU operator+ (BinaryExampleU rhsObj) { return BinaryExampleU(m_LocalInt + rhsObj.m_LocalInt); } void print() { cout<<m_LocalInt; } };

22 void main() { BinaryExampleU object1(10), object2(20), object3 (30), object4(0); object4 = object1 + object2 +object3 ; object4.print(); }

23 Case Study: An Array class  Implement an Array class with  Range checking  Array assignment  Arrays that know their size  Outputting/inputting entire arrays with >  Array comparisons with == and !=

24 Thankyou


Download ppt "OPERATOR OVERLOADING Customised behaviour of operators."

Similar presentations


Ads by Google