The document discusses operator overloading and polymorphism in C++, explaining that polymorphism allows one interface to perform multiple actions and can be static or dynamic. It details how to overload functions and operators, specifying that most C++ operators can be overloaded except for a few exceptions, and emphasizes that while the semantics can be extended, the syntax cannot be changed. The document also outlines the syntax for defining operator overloading for both unary and binary operators, indicating that they can be implemented as member or friend functions.
The document discusses operator overloading and polymorphism in C++, explaining that polymorphism allows one interface to perform multiple actions and can be static or dynamic. It details how to overload functions and operators, specifying that most C++ operators can be overloaded except for a few exceptions, and emphasizes that while the semantics can be extended, the syntax cannot be changed. The document also outlines the syntax for defining operator overloading for both unary and binary operators, indicating that they can be implemented as member or friend functions.
Polymorphism • Refers to ‘one name having many forms’, ‘one interface doing multiple actions’. • In C++, polymorphism can be either – static polymorphism or – dynamic polymorphism.
• C++ implements static polymorphism through
– overloaded functions – overloaded operators Overloading • Overloading – A name having two or more distinct meanings
• Overloaded function - a function having
more than one distinct meanings
• Overloaded operator - When two or more
distinct meanings are defined for an operator Operator overloading • Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators. We can overload all the C++ operators except the following: • Class members access operator (. , .*) • Scope resolution operator (: :) • Size operator(sizeof) • Condition operator (? :) • Although the semantics of an operator can be extended, we can't change its syntax, the grammatical rules that govern its use such as the no of operands, precedence and associativity. For example the multiplication operator will enjoy higher precedence than the addition operator. • When an operator is overloaded, its original meaning is not lost. An operator is overloaded by writing a non-static member function definition or non-member function definition as you normally would, except that the function name starts with the keyword operator followed by the symbol for the operator being overloaded. ◦ For example, the function name operator+ would be used to overload the addition operator (+) for use with objects of a particular class. DEFINING OPERATOR OVERLOADING: • To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied . • Syntax:- return-type class-name :: operator op( arg-list) { function body } • operator functions must be either member function, or friend function. Unary – operator overloading • A unary operator for a class can be overloaded as a non-static member function with no arguments or as a non-member function with one argument that must be an object (or a reference to an object) of the class. Unary – operator overloading(using member function): Unary – - operator overloading(using friend function): Overloading Binary Operators • A binary operator can be overloaded as a non- static member function with one parameter or as a non-member function with two parameters (one of those parameters must be either a class object or a reference to a class object). Binary – operator overloading(using member function): Binary – operator overloading(using friend function):