OPERATOR OVERLOADING IN C++

16
OOP WITH C++ NAME: AABHA.A.TIWARI BRANCH: COMPUTER ENGINEERING TOPIC: OPERATOR OVERLOADING

Transcript of OPERATOR OVERLOADING IN C++

OOP WITH C++NAME: AABHA.A.TIWARI

BRANCH: COMPUTER ENGINEERING

TOPIC: OPERATOR OVERLOADING

CONTENTS:• Introduction• Concept of Operator Overloading• Syntax• Example• Types• Rules for Overloading Operators

INTRODUCTION:

• An overloaded declaration is a declaration that has been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition i.e. implementation.• When you call an overloaded function or operator, the compiler

determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.

• Each C++ operator has a predefined meaning. Operator Overloading is a type of polymorphism in which an operator is overloaded to give user defined meaning to it or say to give an additional meaning to it.

• For example, + operator can be overloaded to perform an operation of string concatenation along with its pre-defined job of adding two numeric values.

• In simple words, overloading an operator means assigning additional operation or job to it; relative to a specific class.

• When an operator is overloaded, none of its original meaning will be lost.

SYNTAX:• Overloaded operators are functions with special names the

keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

CONSIDER FOLLOWING PIECE OF CODE. CONSIDERING THE + OPERATOR IS OVERLOADED IN SUCH A WAY WITH CLASS STRING THAT IT WILL CONCATENATE TWO STRING OBJECTS :int main() {

int n1 = 1, n2 = 2, n3; String s1 = “Hello”, s2=“World”, s3;n3 = n1 + n2; // addition s3 = s1 + s2; // concatenation

}

RULES FOR OVERLOADING OPERATORS:• Only existing operators can be overloaded.New operators cannot be created.• The overloaded operator must have at least one operand that is of user-defined type.• We cannot change the basic meaning of an operator.• You cannot change any operators precedence.• Overloaded operators follow the syntax rules of the original operators.• Binary operators overloaded through a member function take one explicit argument

and those which are overloaded through a friend function take two explicit arguments. • When using binary operators overloaded through a member function, the left hand

operand must be an object of the relevant class.• Binary arithmetic operators such as +, -, * and / must explicitly return a value. They

must not attempt to change their own arguments.

TYPES OF OPERATORS:

• Unary (++, --)• Binary (+, -) • Ternary (?:)

You are free to overload most of the built-in operators, but you cannot create new operators of your own.Therefore, although you can give your class an increment operator (++), you cannot create a squared operator.You can overload these operators to do anything you want, but it is a good programming practice for them to make sense.That means you can have the ++ operator to decrement, but it would make no sense to do so.

OPERATOR FUNCTIONS:• Operators are overloaded by creating operator functions.• An operator function defines the operations that the overloaded operator will

perform relative to the class upon which it will work.• An operator function is created using the keyword operator.• Operator functions can be either I. Member Functions of a class II. Friend Functions of a class• The way operator functions are written differs between member and non-

member functions.• You can overload most of Unary and Binary Operators, but you cannot overload

the ternary operator (?:).Binary as well as Unary operators can be overloaded by both approaches namely Member Functions Approach and Friend Function Approach.

A MEMBER OPERATOR FUNCTION FOR BINARY OPERATORS:

• RetType ClassName::operator#(arg-list) {

// operations }• For example, if you are overloading the + operator, use

operator+.• While overloading binary operators using member function,

thearg-list will contain one parameter.

A MEMBER OPERATOR FUNCTION FOR UNARY OPERATORS:

RetType ClassName::operator#() {

// operations }• While overloading an unary operator, arg-list will be empty.

A FRIEND OPERATOR FUNCTION FOR BINARY OPERATORS:RetType operator#(arg-list){

// operations }• While overloading a binary operator using friend function;

argument list must take 2 arguments, one of them must be of userdefined type.

A FRIEND OPERATOR FUNCTION FOR UNARY OPERATORS:

RetType operator#(arg-list){

// operations }• While overloading an unary operator using friend function;

argument list must have 1 argument as reference to the object.

DIFFERENCE BETWEEN MEMBER FUNCTION AND FRIEND FUNCTION:

• A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators,while a member function has no arguments for unary operators and only one for binary operators.• The friend function takes one argument more than member

function because the invoking appears as an explicit parameter to the friend function whereas in member functions it is passed as an implicit parameter.

Thank

You