Chapter 5 · perform special operations relative to classes that you ... Suppose we want to find...

31
www.sahajsolns.com Chapter 5 Chapter 5 Sahaj Computer Solutions 1 OOPS WITH C++

Transcript of Chapter 5 · perform special operations relative to classes that you ... Suppose we want to find...

www.sahajsolns.com

Chapter 5Chapter 5

Sahaj Computer Solutions 1OOPS WITH C++

www.sahajsolns.com

Contents� Overloading

� Function Overloading

� Operator overloading

Rules for Operator Overloading� Rules for Operator Overloading

� Overloading of Unary Operators

� Overloading of Binary Operators

� Manipulation on strings

� Type Conversion

Sahaj Computer Solutions 2OOPS WITH C++

www.sahajsolns.com

Function Overloading� Function overloading is the process of using the same

name for two or more functions.

� The secret to overloading is that each redefinition of the function must use either different types of the function must use either different types of parameters or a different number of parameters.

� It is only through these differences that the compiler knows which function to call in any given situation.

� For example, CH5PG1.CPP program overloads myfunc() by using different types of parameters.

� Next example: CH5PG2.CPP

Sahaj Computer Solutions 3OOPS WITH C++

www.sahajsolns.com

Operator Overloading� Closely related to function overloading is operator

overloading.

� In C++, you can overload most operators so that they perform special operations relative to classes that you perform special operations relative to classes that you create.

� For example, a class that maintains a stack might overload + to perform a push operation and – – to perform a pop.

Sahaj Computer Solutions 4OOPS WITH C++

www.sahajsolns.com

Operator Overloading� When an operator is overloaded, none of its original

meanings are lost.

� Instead, the type of objects it can be applied to is expanded.expanded.

� DEFINITION: The mechanism of giving special meanings to an operator is known as operator overloading.

Sahaj Computer Solutions 5OOPS WITH C++

www.sahajsolns.com

Do all Operators overload?� We can overload all the operators except the following:

� Class member access Operator (. ,.*)

� Scope resolution Operator(::)

� Size of Operator (sizeof)� Size of Operator (sizeof)

� Conditional Operator(?:)

Sahaj Computer Solutions 6OOPS WITH C++

www.sahajsolns.com

Rules for Operator Overloading� 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.operand that is of user-defined type.

� We cannot change the meaning of an operator. That is say, we cannot define + plus operator to subtract one value from the other.

� Overloaded operators follow the syntax rules of the original operators. They cannot be overridden.

Sahaj Computer Solutions 7OOPS WITH C++

www.sahajsolns.com

Rules for Operator Overloading� There are some operators that cannot be overloaded.

� We can make use of friend function to overload certain operators shown in below table:

Operators Description

= Assignment Operator

( ) Function Operator

[ ] Subscripting Operator

-> Class member access operators

Sahaj Computer Solutions 8OOPS WITH C++

www.sahajsolns.com

Defining Operator Overloading� To define an additional operator, we must specify what

it means in relation to the class to which the operator is applied. This is done with a special function , called operator function, which describes the task.operator function, which describes the task.

� The general form of an operator function is:

return-type operator op(arg-list){

function body //task defined

}

Sahaj Computer Solutions 9OOPS WITH C++

www.sahajsolns.com

Defining Operator Overloading� 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.used in the overloading operation.

� Declare the operator function operator op() in the public part of the class. It may be either member function or friend function.

� Define the operator function to implement the required operations.

Sahaj Computer Solutions 10OOPS WITH C++

www.sahajsolns.com

Overloading Unary Operators� Let us consider the unary minus operator.

� A minus operator is used as a unary operator and takes only one operand.

� This operator changes the sign of an operand.This operator changes the sign of an operand.

� However we can apply it to user-defined types by overloading it as follows:

void operator –()

{

x=-x;

y=-y;

z=-z;

}

Sahaj Computer Solutions 11OOPS WITH C++

www.sahajsolns.com

Overloading Unary Operators� Here void is the return type of the function.

� The keyword operator tells the compiler that an operator is getting overloaded.

� - tells the compiler that the operator – (unary minus is � - tells the compiler that the operator – (unary minus is getting overloaded.

� ()does not take any arguments. Since the function is the member of the class , it can directly access the members of the object which activated it.

Sahaj Computer Solutions 12OOPS WITH C++

www.sahajsolns.com

Overloading Unary Operators� If friend function is used then the following code had

to be written.

friend void operator –(test &t)

� Note that the argument is passed by reference. � Note that the argument is passed by reference.

� It will not work if we pass argument by value because only copy of the object that activated the call is passed to operator-().

� Therefore changes made are not reflected in the called object.

Sahaj Computer Solutions 13OOPS WITH C++

www.sahajsolns.com

Creating Prefix and Postfix Forms of the

Increment and Decrement Operators� C++ allows you to explicitly create separate prefix and

postfix versions of increment or decrement operators.

� To accomplish this, you must define two versions of theoperator++() function.

One is defined as shown as shown above and the other is� One is defined as shown as shown above and the other isdeclared like this:

loc operator++(int x);

� If the ++ precedes its operand, the operator++() function iscalled.

� If the ++ follows its operand, the operator++(int x) iscalled and x has the value zero.

Sahaj Computer Solutions 14OOPS WITH C++

www.sahajsolns.com

Overloading Binary Operators� Like unary operators binary operators can be

overloaded with same mechanism.

� Suppose we want to find sum of two complex numbers then a statement like C= sum(A,B);then a statement like C= sum(A,B);

� Would be replaced by C=A+B; by overloading the operator +.

� The following program shows overloading of binary addition operator (+).

� Example: CH5PG5.CPP

Sahaj Computer Solutions 15OOPS WITH C++

www.sahajsolns.com

Overloading Binary Operators� Let us take a close look at the function operator+()

and see how the operator overloading is implemented.

complex operator+(complex c)

{{

complex temp;

temp.real=real+c.real;

temp.img=img+c.img;

return(temp);

}

Sahaj Computer Solutions 16OOPS WITH C++

www.sahajsolns.com

Overloading Binary Operators� Note the following:

� It receives only one complex type argument explicitly

� It returns a complex type argument.

� It is a member function of complex.� It is a member function of complex.

� The statement C3= C1+C2; that invokes the operator+() function.

� Here the object C1 invokes the operator+() function and passes c2 as its parameter.

� The above invocation is equivalent to c3=c1.operator+(c2);

Sahaj Computer Solutions 17OOPS WITH C++

www.sahajsolns.com

Overloading Binary Operators� Therefore the data members of C1 are accessed directly

and the data members of C2 are accessed using the dot operator.

� As a rule in overloading of binary operators, the left � As a rule in overloading of binary operators, the left hand operand is used to implement the operator function and the right hand operand is passed as an argument.

Sahaj Computer Solutions 18OOPS WITH C++

www.sahajsolns.com

Implementation of the Overload +

Operator

Complex operator +(complex c){

complex temp;

temp.real c.real real;+=

temp

return temp;}

temp.real

temp.img

c.real

c.img

real;

img;+

+=

=

4.1

6.2

1.6

2.7

2.5

3.5

4.1

6.2

C3 = C1 + C2

return

Sahaj Computer Solutions 19OOPS WITH C++

www.sahajsolns.com

Overloading Comparison Operator� Let’s see how to overload different type of operator :

Comparison operators.

� The next program overloads the comparison operator < to compare two distance objects.< to compare two distance objects.

� PROGRAM: CH5PG7.CPP

Sahaj Computer Solutions 20OOPS WITH C++

www.sahajsolns.com

Overloading Arithmetic

Assignment Operators� The arithmetic assignment operator (+=) operator

combines assignment and addition operation into one step.

� The following program uses this operator to add one � The following program uses this operator to add one distance to another leaving the result in the first.

� PROGRAM : CH5PG8.CPP

Sahaj Computer Solutions 21OOPS WITH C++

www.sahajsolns.com

Type Conversion� When an a program uses constants and variables of

different types then the compiler automatically performs type conversion to the operands as per the rules.

� The type of data to the right of an assignment operator is automatically converted to the type of the variable on the left.

� For example:int m;

float x=3.14215;

m=x;

Sahaj Computer Solutions 22OOPS WITH C++

www.sahajsolns.com

Type Conversion� Converts x to an integer before its value is assigned to

m.

� When user-defined data types are considered for exampleexample

v3=cv+v2; //v1,v2,v3 are class type variables

� When the objects are of the same type, the operations of addition and assignment are carried out smoothly and the compiler does not make any complaints.

� The values of right hand side class are simply copied to the left hand side class variables.

Sahaj Computer Solutions 23OOPS WITH C++

www.sahajsolns.com

Type Conversion� What if one of the operands is an object and the other

is built in type variables?

� Since user defined data types are designed by us to suit our requirements, the compiler does not support our requirements, the compiler does not support automatic conversions for such types.

� We must therefore, design the conversion routines by ourselves, if such operations are required.

Sahaj Computer Solutions 24OOPS WITH C++

www.sahajsolns.com

Type Conversion� There are three type of data conversions:

� Conversion from basic type to class type

� Conversion from class type to basic type

� Conversion from one class type to another class type� Conversion from one class type to another class type

Sahaj Computer Solutions 25OOPS WITH C++

www.sahajsolns.com

Basic to Class type� The conversion from basic to class type is easy to

accomplish

� It may be recalled that the use of constructors was explained earlier to initilize objects.explained earlier to initilize objects.

� For example , a constructor can be used to built a vector type from an integer type array.

� Similarly it can convert a *char data to String class type data.

� In all the above examples constructors perform a defacto type conversion from the argument’s type to the constructor’s class type.

Sahaj Computer Solutions 26OOPS WITH C++

www.sahajsolns.com

Class type to Basic type� The Constructors did a fine job in type conversion

from basic type to class type.

� When we convert class type data to basic type constructor will not support this operation.constructor will not support this operation.

� C++ allows to define an overloaded casting operatorthat could be used to convert a class type data to a basic type.

� The general form of an overloaded casting operator function (usually referred to conversion function) is:

Sahaj Computer Solutions 27OOPS WITH C++

www.sahajsolns.com

Class type to basic typeSyntax:

operator typename()

{

function statements

Example:String :: operator char*(){

return( p );}function statements

}

� The casting operator function should satisfy the following conditions� It must be a class member.

� It must not specify a return type

� It must not have any arguments.

}

Sahaj Computer Solutions 28OOPS WITH C++

www.sahajsolns.com

One Class to Another Class Type� We have seen data conversion technique from a basic

type to class type and a class type to basic type.

� But there are situations where we want to convert one class type data to another class type data.class type data to another class type data.

� Example;

obj1=obj2; //objects of different types

� Such conversions between objects can be carried out by either a constructor or a conversion function.

� The compiler treats them the same way.

Sahaj Computer Solutions 29OOPS WITH C++

www.sahajsolns.com

Type conversions

Conversion required Conversion takes place in

Source Class Destination Class

The table below provides the summary of all the three conversions.

Basic � Class Not Applicable Constructor

Class �Basic Casting Operator Not Applicable

Class �Class Casting Operator Constructor

Example: ch5pg12.cpp

Sahaj Computer Solutions 30OOPS WITH C++

www.sahajsolns.com

Sahaj Computer Solutions Near Gomatesh School, Hindwadi Belgaum-11, Phone no: 4200864,2481703

Get Certified in

•Microsoft Office 2010•Tally 9.2•.NET 2008•J2EE•CORE JAVA

� Around 1800 students developed software projects and scored the top scores in exams.

� One stop shop for •CORE JAVA•C PROGRAMMING•OOPS WITH C++•JOOMLA•WORDPRESS•PHP AND MYSQL•SQLSERVER•UNIX AND LINUX•ANDRIOD APPS

� One stop shop for � Software Projects

� Website Development

� Technology Training

� I.T Research Visit: www.sahajsolns.com

Sahaj Computer Solutions 31OOPS WITH C++