0% found this document useful (0 votes)
11 views36 pages

Chapter-7 OOPC Old

Uploaded by

alex.moon268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views36 pages

Chapter-7 OOPC Old

Uploaded by

alex.moon268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Chapter-7

Operator Overloading

Prepared By: Aayushi Chaudhari


Assistant Professor, CE,CSPIT.

3/14/2021 | U & P U. Patel Department of Computer Engineering 1


Contents
• Introduction
• Defining Operator overloading
• overloading unary
• binary operators
• overloading binary operator using friend function
• rules for overloading operators
• Type Conversion

Weightage: 11%
Hours: 6 Hours needed

3/14/2021 | U & P U. Patel Department of Computer Engineering


Introduction
Operator Overloading is an important branch of polymorphism in C++.

Polymorphism

Compile Time Runtime

Function Operator Virtual


Overloading Overloading Functions

3/14/2021 | U & P U. Patel Department of Computer Engineering


What is operator overloading?
Operator Overloading is a specific case of polymorphism in which some or all operators like +, = or = =
are treated as polymorphic functions and as such have different implementations depending on the types of
their arguments.
Operator overloading gives us the opportunity to redefine the C++ language.

C++ permits us to add two variables of user defined types with the same syntax that is applied to the
basic data types.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Restriction on operator overloading
Overloading restrictions
Precedence of an operator cannot be changed
Associatively 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
3/14/2021 | U & P U. Patel Department of Computer Engineering
Restrictions on Operator Overloading
• Following operators can’t be overloaded:
• Class member access operators (., .*)
• Scope resolution operator (::)
• Size operator (sizeof )
• Conditional operator (?:)
• All other operators can be overload

3/14/2021 | U & P U. Patel Department of Computer Engineering


Defining Operator Overloading
• General syntax for operator overloading is: return type classname :: operator op(arglist)
• {
• Function body
• }
• For e.g.:
• vector operator +(vector);
• vector is a data type of class.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Operator Overloading Process
• The process of overloading involves the following steps:

• Create a class that defines the data type that is to be used in the

overloading operation.
• Declare the operator function operator op() in the public part of

the class.
• Define the operator function to implement the required operations.
3/14/2021 | U & P U. Patel Department of Computer Engineering
Overloading Unary Operators
• Consider unary minus operator (changes the sign of the operand)

• The unary minus when applied to an object should change the sign of each of its data items.

• Lets see an example:

3/14/2021 | U & P U. Patel Department of Computer Engineering


Example for unary – operator overloading

3/14/2021 | U & P U. Patel Department of Computer Engineering


Overloading Binary Operators
• Asa unary operator is overloaded we can also overload a binary operator.

• For e.g: A binary operator + can be overloaded to add two objects rather than adding two
variables.
• Using operator overloading a functional notation,

• C = sum(A, B);
• Can be replaced by,
• C = A + B;

3/14/2021 | U & P U. Patel Department of Computer Engineering


Overloading Binary Operators
• As a rule, in overloading binary operators,

• the left-hand operand is used to invoke the operator function and

• the right-hand operand is passed as an argument.

return complex((x+c.x), (y+c.y));

• The compiler invokes an appropriate constructor, initializes an object with no name and returns the
contents for copying into an object.

• Such an object is called a temporary object and goes out of space as soon as the contents are assigned to
another object.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Example for binary operator

3/14/2021 | U & P U. Patel Department of Computer Engineering


Overloading binary operators using Friends
• Friend functions maybe used in place of member functions for overloading a binary operator.

• A friend function requires two arguments to be explicitly passed to it, while a member
function requires only one.
• For e.g:

complex operator + (complex);

friend complex operator + (complex, complex);

3/14/2021 | U & P U. Patel Department of Computer Engineering


Overloading binary operators using Friends
• The operator function definition would also be modified as:
complex complex :: operator+(complex c)
{
complex temp; temp.x = x + c.x; temp.y = y + c.y; return(temp);
}

complex operator + (complex a, complex b)


{
return complex((a.x + b.x), (a.y + b.y));
}

3/14/2021 | U & P U. Patel Department of Computer Engineering


Overloading binary operators using Friends
• The statement,

C3 = C1 + C2;

Is equivalent to

C3 = operator+(C1, C2);

3/14/2021 | U & P U. Patel Department of Computer Engineering


Example for overloading binary operator using friend

3/14/2021 | U & P U. Patel Department of Computer Engineering


Manipulation of Strings using Operators
• There are lot of limitations in string manipulation in C as well as in C++.

• Implementation of strings require character arrays, pointers and string functions.

• C++ permits us to create our own definitions of operators that can be used to
manipulate the strings very much similar to other built-in data types.

• ANSI C++ committee has added a new class called string to the C++ class library that
supports all kinds of string manipulations.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Manipulation of Strings using Operators
• Strings can be defined as class data member which can be then manipulated like the
built-in types.

• Since the strings vary in size, we use new to allocate memory for each string and a
pointer variable to point to the string array.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Operators used for String Operations
1. =: assignment
2. +: concatenation
3. ==: Equality
4. !=: Inequality
5. <: Less than
6. <=: Less than or equal
7. >: Greater than
8. >=: Greater than or equal
9. []: Subscription
10. <<: Output
11. >>: Input
3/14/2021 | U & P U. Patel Department of Computer Engineering
Manipulation of Strings using Operators
• We must create string objects that can hold two pieces of information:
• Length
• Location
class string
{
char *p; // pointer to string
int len; // length of string
public :
------
------
};
3/14/2021 | U & P U. Patel Department of Computer Engineering
Example for manipulating strings

3/14/2021 | U & P U. Patel Department of Computer Engineering


Type Conversions
• The type conversions are automatic only when the data types involved are built-in types.

• int m;

• float x = 3.14159;

• m = x; // convert x to integer before its value is assigned

• // to m.
• For user defined data types, the compiler does not support automatic type conversions.

• We must design the conversion routines by ourselves.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Type Conversions
• Different situations of data conversion between incompatible types.

• Conversion from basic type to class type.

• Conversion from class type to basic type.

• Conversion from one class type to another class type.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Basic to Class Type
• In this type of conversion the source type is basic type and the destination type is
class type. Means basic data type is converted into the class type.
• For example we have class employee and one object of employee ‘emp’ and
suppose we want to assign the employee code of employee ‘emp’ by any integer
variable say ‘Ecode’ then the statement below is the example of the conversion
from basic to class type.
emp = Ecode;
• Here the assignment will be done by converting “Ecode” which is of basic or
primary data type into the class type.
3/14/2021 | U & P U. Patel Department of Computer Engineering
Basic to Class Type
• The conversion from basic type to the class type can be performed by two ways:
1. Using constructor
2. Using operator overloading
Using Constructor
• We can use constructor to perform type conversion during the object creation.
Using Operator Overloading
• We can also achieve type conversion by operator overloading.
• We can overload assignment operator for this purpose.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Example for BtoC using constructor

3/14/2021 | U & P U. Patel Department of Computer Engineering


Example for BtoC using operator overloading

3/14/2021 | U & P U. Patel Department of Computer Engineering


Class type to basic type
• In this type of conversion the source type is class type and the destination type is
basic type.
• Means class data type is converted into the basic type.
• For example we have class Time and one object of Time class ‘t’ and suppose we
want to assign the total time of object ‘t’ to any integer variable say ‘duration’ then
the statement below is the example of the conversion from class to basic type.
duration = t
Here the assignment will be done by converting “t” object which is of class type into
the basic or primary data type. It requires special casting operator function for class
type to basic type conversion. This is known as the conversion function.
3/14/2021 | U & P U. Patel Department of Computer Engineering
Note:
• To overload the function that casts our class to an int, we
write a new function in our class called operator int(). Note
that there is a space between the word operator and the type
we are casting to.
• It must not specify the return value even though it returns the
value.
• It must not have any argument.
Eg. operator int()
3/14/2021 | U & P U. Patel Department of Computer Engineering
Example for CtoB using OO and constructor

3/14/2021 | U & P U. Patel Department of Computer Engineering


Class Type to Another Class Type
• In this type of conversion both the type that is source type and the
destination type are of class type. Means the source type is of class type
and the destination type is also of the class type. In other words, one class
data type is converted into the another class type.
• For example we have two classes one for “computer” and another for
“mobile”. Suppose if we wish to assign “price” of computer to mobile then
it can be achieved by the statement below which is the example of the
conversion from one class to another class type.
mob = comp

3/14/2021 | U & P U. Patel Department of Computer Engineering


One Class To Another Class Type
objX = objY ; // objects of different types

• objX is an object of class X and objY is an object of class Y.

• The class Y type data is converted to the class X type data and the converted value is
assigned to the objX.

• Conversion is takes place from class Y to class X.

• Y is known as source class.

• X is known as destination class.


3/14/2021 | U & P U. Patel Department of Computer Engineering
One Class To Another Class Type
operator typename( )
• Converts the class object of which it is a member to typename.
• The typename may be a built-in type or a user-defined one.
• In the case of conversions between objects, typename refers to the destination class.
• When a class needs to be converted, a casting operator function can be used at the source
class.
• The conversion takes place in the source class and the result is given to the destination class
object.

3/14/2021 | U & P U. Patel Department of Computer Engineering


Example for One Class To Another Class Type

3/14/2021 | U & P U. Patel Department of Computer Engineering


Thank You.

3/14/2021 | U & P U. Patel Department of Computer Engineering

You might also like