Unit 3 C++ Operator Overloading (PDF)
Document Details
Uploaded by WittyCommonsense
Shree Swaminarayan College of Computer Science
Gaurang Bhatt
Tags
Summary
This document includes notes on operator overloading in C++. The document covers basic concepts and examples of operator overloading in C++. It is part of a larger course on Object-Oriented Programming (OOP) using C++.
Full Transcript
Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-3 Unit-3: Operator Overloading and Type Conversion 1. Basic concept of operator overloading 2. Types of operator overloading – Unary and Binary...
Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-3 Unit-3: Operator Overloading and Type Conversion 1. Basic concept of operator overloading 2. Types of operator overloading – Unary and Binary 3. Operator overloading using member function & friend function 4. Type conversion 5. Categories of type conversion – Basic to Class, Class to Basic, Class to Class Prepared By: Gaurang Bhatt B.SC IT-Sem-3 Page 1 of 19 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-3 Q.1 Explain Basic concept of operator overloading. Introduction: - ❖ Operator overloading is one of the many exciting features of C++ language. It is an important technique that has enhanced the power of extensibility of c++. ❖ We have stated more than once that C++ tries to make the user-defined data types behave inmuch the same way as the built-in types. ❖ C++ permits us to add two variables of user-defined types with the same syntax that is applied to the basic types. ❖ This means that C++ has the ability to provide the operators with a special meaning for a data type. The mechanism of giving such special meanings to an operator is known as operator overloading. ❖ Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators. ❖ We can almost create a new language of our own by the creative use of the function and operator overloading technique. ❖ We can overload (give additional meaning to) all the C++ operator except the following: 1. Class member access operators (. or. * ) 2. Scope resolution operator ( :: ) 3. Size operator ( sizeof ) 4. Conditional Operator (?:) ❖ The excluded operators are very few when compared to the large number of operators which qualify for the operator overloading definition. ❖ Although the semantics of an operator can be extended, we cannot change its syntax, the grammatical rules that govern its use such as the number of operands, precedence and associativity. For e.g., the multiplication operator will enjoy higher precedence than the addition operator. ❖ When operator is overloaded, its original meaning is not lost. For instance, the operator +, which has been overloaded to add two arrays, can still be used to add two integers. Prepared By: Gaurang Bhatt B.SC IT-Sem-3 Page 2 of 19 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-3 Define Operator Overloading: - ❖ To define an additional task to an operator, we must specify what it means in relation tothe class to which the operator is applied. ❖ This is done with the help of a special function, called operator function, which describes the task. The general form of an operator function is: return-type classname :: operator op(arglist) { function body //task defined } ❖ Where return type is the type of value returned by the specified operation and op is the operator being overloaded. ❖ The op is preceded by the keyword operator. Operator op is the function name. ❖ Operator function must be either member function (non-static) or friend functions. ❖ A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operator, while a member function has no arguments for unary operators and only one for binary operators. ❖ This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. This is not the case with friend function. ❖ Arguments may be passed either by value or by reference. Operator functions are declared in the class using prototypes as flows: vector operator +(vector); //vector addition, Binary Operator Overloading vector operator– (); //unary minus without friend function friend vector operator + (vector, vector); //vector addition, binary Operator friend vector operator–(vector);//unary minus with friend function vector operator– (vector &a); //subtraction, Binary Operator int operator = =(vector); //comparison Binary Operator friend vector operator = = (vector, vector); //comparison, Binary operator Prepared By: Gaurang Bhatt B.SC IT-Sem-3 Page 3 of 19 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-3 Q. What do you mean by unary and binary operator in C++? 1. Unary Operator: An operator that operates on single operand or variable and give the result or output. 2. Binary Operator: An operator that operates on two operand or variable and give the result or output. Overloading unary operator: - ❖ Let us consider the unary minus operator. A minus operator when used as a unary, takes just one operand. ❖ We know that operator changes the sign of an operand when applied to a basic data item. ❖ We will see here how to overload this operator so that it can be applied to an object in much the same way as is applied to an int or float variable. ❖ The unary minus when applied to an object should change the each of its data items. The following program shows how unary minus operator is overloaded. class space { int x; int y; int z; public: void getdata(int a, int b, int c); void display () void operator - (); //overload unary minus operator declaration }; void space::getdata(int a, int b, int c) { x=a; y=b; z=c; } void space :: display() { cout