FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
Â
14 operator overloading
1. UNIT III
Overloading
09/04/131 VIT - SCSE
By
G.SasiKumar., M.E., (Ph.D).,
Assistant Professor
School of Computing Science and Engineering
VIT University
2. Functions in C++
ïExperience has shown that the best way to develop and
maintain large programs is to construct it from smaller pieces
(Modules)
ïThis technique Called âDivide and Conquerâ
âąEaser To
ïŒDesign
ïŒBuild
ïŒDebug
ïŒExtend
ïŒModify
ïŒUnderstand
ïŒReuse
ïŒBetter Organization
Wise Development Approach
main()
{
-----
----
}
function f1()
{
---
---
}
function f2()
{
---
---
}
3. Function Overloading
ïC++ supports writing more than one function with the same
name but different argument lists. This could include:
ïdifferent data types
ïdifferent number of arguments
ïThe advantage is that the same apparent function can be
called to perform similar but different tasks. The following
will show an example of this.
5. 09/04/135 VIT - SCSE
âą Operator Overloading refers to giving the normal C++ Operators,
such as +,*,<= etc., additional meanings when they are applied
to user defined data types.
âą Simply defined as to create new definitions for operators.
Syntax :
<ret.datatype> operator <operator name>()
{
---
---
}
Operator Overloading
6. The steps involved an operator are :
1. Create a class that defines a data type that is to be
used in the overloading operation
2. Declare the operator function as either a member
function or a friend function inside the class
3. Define the operator function either inside or outside
the class
4. Use the operator in the main() function
7. âą All the operators can be overloaded using friend
function except () [] -> and =. These operators must be
defined by using a member function.
ASSIGNMENT OPERATOR OVERLOADING RULES :
âą The operator function for the assignment operator are
inherited by any derived class.
âą Friend functions cannot be used to overload the
assignment operator
8. The operators that can be overloaded
are
+ - * / % ^ &
| _ != < > <= >= +=
-+ *= != ++ -- [ ] ()
|| &= && -> , new delete
The operators that cannot be overloaded are
#
.(member operator)
::
sizeof
?:
10. 09/04/1310 VIT - SCSE
void main()
{
sample ob1(10,4.5);
sample ob2(20,6.2);
ob1=ob2;
cout<<âcontents of the first object nâ;
ob1.display();
cout<<âcontents of the second object nâ;
ob2.display();
}
16. #include<iostream.h>
const int SIZE=5;
class test
{
private :
int a[SIZE];
public:
int operator [] (int i)
{
return i;
}
};
void main()
{
test t1;
int i;
OVERLOADING THE SUBSRIPTOPERATOR [ ]
for(i=1;i<=SIZE;i++)
{
// control is transferred to the operator
function call int operator [] (int i)
cout<<t1[i]<<"t";}
}
OUTPUT :
1
2
3
4
5
20. Increment and Decrement Operators
ïWe have used n++; and ++n; to replace for n = n + 1;
ïand we have used --n and n--; to replace for n = n - 1;
ïThe expressions n++ and ++n have values.
ïThe expression n++ returns the value of n before to
incrementing, then increments the value of n.
ï++n increments the value of n, then returns the
incremented value.
ïThe expressions n-- and --n have values as well.
ïThe expression n-- returns the value of n before to
decrementing, then decrements the value of n.
ï--n decrements the value of n, then returns the decremented
value.
21. Overloading ++ and - -
ïWith C++, you use ++ to increment variables, and - -
to decrement variables
ïWhen a prefix operator such as ++ is used in an
expression, the mathematical operation takes place
before the expression is evaluated
ïWhen the postfix operator is used, the expression is
evaluated before the mathematical operation takes
place
23. Generic Programming for Templates
ïA methodology for the development of reusable software
libraries
ïThree primary tasks:
ïCategorize the abstractions in a domain into concepts
ïImplement generic algorithms based on the concepts
ïBuild concrete models of the concepts
Concepts make templates easier to use
ïExpress requirements directly in code
ïProvide complete type-checking of templates
24. Characteristics of Generic Libraries
ïReusable: able to operate on user-defined data types
ïComposable: able to operate on data types defined in
another library
ïEfficient: performance on par with non-generic, hand-
coded implementations
25. C++ Templates
ï C++ Function Templates
-- C++ Function templates are those functions which can
handle different data types without separate code for each of
them.Â
ï C++ Class Templates
-- C++ Class Templates are used where we have multiple
copies of code for different data types with the same logic.
26. Templates
ïConstructs a family of related functions or class
ïDifferent Approach â Function
 Â
Example 1 & 2 : int Add(int a,int b) { return a+b;} // function Without C++ template
float Add(float a, float b) { return a+b;} // function Without C++ template
1. NaĂŻve Approach
Different Function Definitions
Different Function Names
2. Function Overloading
Different Function Definitions
Same Function Name
3. Template Functions
One Function Definition (a function template)
Compiler Generates Individual Functions
27. Approach 3: Function Template
âą A C++ language construct that allows the compiler
to generate multiple versions of a function by
allowing parameterized data types.
Template < TemplateParamList >
FunctionDefinition
FunctionTemplate
TemplateParamDeclaration: placeholder
class typeIdentifier
typename variableIdentifier
28. Example of a Function Template
template<class T>
T Add(T a,T b)//C++ Fucntion Template sample
{
return a+b;
}
Template parameter
(class, user defined
type, built-in types)
29. Class Template
âą A C++ language construct that allows the compiler
to generate multiple versions of a class by allowing
parameterized data types.
Template < TemplateParamList >
ClassDefinition
Class Template
TemplateParamDeclaration: placeholder
class typeIdentifier
typename variableIdentifier
30. Example of a Class Template
template<class ItemType>
class GList
{
public:
bool IsEmpty() const;
bool IsFull() const;
int Length() const;
void Insert( /* in */ ItemType item );
void Delete( /* in */ ItemType item );
bool IsPresent( /* in */ ItemType item ) const;
void SelSort();
void Print() const;
GList(); // Constructor
private:
int length;
ItemType data[MAX_LENGTH];
};
Template
parameter
31. Advantages of C++ Class Templates:Â
ïOne C++ Class Template can handle different types of
parameters.
ïCompiler generates classes for only the used types. If the
template is instantiated for int type, compiler generates only
an int version for the c++ template class.
ïTemplates reduce the effort on coding for different data types
to a single set of code.
ïTesting and debugging efforts are reduced.
32. Standard Template Library
ïIn the late 70s Alexander Stepanov first observed that some
algorithms do not depend on some particular
implementation of a data structure but only on a few
fundamental semantic properties of the structure
ïDeveloped by Stepanov and Lee at HP labs in 1992
ïBecome part of the C++ Standard in 1994
33. Whatâs in STL?
ïContainer classes: vector, list, deque, set, map, and etcâŠ
ïA large collection of algorithms, such as reverse, swap, heap,
and etc.
Vector
ïA sequence that supports random access to elements
ïElements can be inserted and removed at the beginning, the
end and the middle
ïConstant time random access
ïCommonly used operations
ïbegin(), end(), size(), [], push_back(âŠ), pop_back(), insert(âŠ),
empty()
34. Recap
ïTemplates are mechanisms for generating functions
and classes on type parameters. We can design a single
class or function that operates on data of many types
ïfunction templates
ïclass templates