Operator Overloading

Содержание

Слайд 2

OBJECTIVES What operator overloading is and how it makes programs more

OBJECTIVES

What operator overloading is and how it makes programs more readable

and programming more convenient.
To redefine (overload) operators to work with objects of user-defined classes.
3. The differences between overloading unary and binary operators.
4. To convert objects from one class to another class.
5. When to, and when not to, overload operators.
6. To use keyword explicit to prevent the compiler from using single-argument constructors to perform implicit conversions.
Слайд 3

Overloading Binary Operators Non-static member function, one argument As a rule,

Overloading Binary Operators

Non-static member function, one argument
As a rule, in overloading

of binary operators, the left hand operator is used to invoke the operator function and right-hand operand is passed as an argument.
2. Global function, two arguments
One argument must be class object or reference
Слайд 4

Overloading Binary + Operator

Overloading Binary + Operator

Слайд 5

Overloading Binary Operators using Friend The complex number program to overload

Overloading Binary Operators using Friend

The complex number program to overload +

operator may be modified using a friend operator function as follows:
Replace the member function declaration by the friend function declaration.
friend complex operator+(Complex, Complex);
2. Redefine the operator function as follows:
Complex operator+(Complex a, Complex b)
{
return Complex((a.x+b.x),(a.y+b.y));
}
Слайд 6

Overloading * Operator

Overloading * Operator

Слайд 7

Слайд 8

Overloading Assignment Operator

Overloading Assignment Operator

Слайд 9

Слайд 10

Слайд 11

What is Memory Allocation? There are two ways via which memories

What is Memory Allocation?

There are two ways via which memories can

be allocated for storing data. The two ways are:
Compile time allocation or static allocation of memory: where the memory for named variables is allocated by the compiler. Exact size and storage must be known at compile time and for array declaration, the size has to be constant.
Runtime allocation or dynamic allocation of memory: where the memory is allocated at runtime and the allocation of memory space is done dynamically within the program run and the memory segment is known as a heap or the free store. In this case, the exact space or number of the item does not have to be known by the compiler in advance. Pointers play a major role in this case.
Слайд 12

Dynamic memory Allocation in C++ using malloc() C++ Dynamic memory allocation

Dynamic memory Allocation in C++ using malloc()

C++ Dynamic memory allocation can

be defined as a procedure in which size of data structure(like Array) is changed during the run-time.
C++ provides some functions to achieve these tasks.There are 4 library functions provided by C++ defined under header file to facilitate dynamic memory allocation in C++ programming.They are:
malloc()
free()
new
delete
Слайд 13

Malloc() method The “malloc” or “memory allocation” method in C is

Malloc() method

The “malloc” or “memory allocation” method in C is used to dynamically allocate a

single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.
Syntax: 
ptr = (cast-type*) malloc(byte-size)
For Example
ptr=(int*)malloc(100*sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory.
Слайд 14

C++ free() method free” method in C is used to dynamically

C++ free() method

free” method in C is used to dynamically de-allocate the memory. The

memory allocated using functions malloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.
Syntax: 
free(ptr);
Слайд 15

Case Study: Array Class Pointer-based arrays in C++ No range checking

Case Study: Array Class

Pointer-based arrays in C++
No range checking
Cannot be compared

meaningfully with ==
No array assignment (array names are const pointers)
If array passed to a function, size must be passed as a separate argument
Example: Implement an Array class with
Range checking
Array assignment
Arrays that know their own size
Outputting/inputting entire arrays with << and >>
Array comparisons with == and !=
Слайд 16

Case Study: Array Class (Cont.) Copy constructor Used whenever copy of

Case Study: Array Class (Cont.)

Copy constructor
Used whenever copy of object is

needed:
Initializing an object with a copy of another of same type
Array newArray( oldArray ); or Array newArray = oldArray (both are identical)
newArray is a copy of oldArray
Prototype for class Array
Array( Array & );
Must take reference
Otherwise, the argument will be passed by value…
Слайд 17

Слайд 18

Слайд 19

Слайд 20

Array Default Constructor Default constructor for the class specifies a default

Array Default Constructor

Default constructor for the class specifies a default size

of 10 elements.
Default constructor in this example actually receives a single int argument that has a default value 10.
The default constructor validate and assigns the argument to data member size, uses new to obtain the memory for the internal pointer based representation of this array and assigns the pointer returned by new to data member ptr.
Constructor uses a for statement to set all the elements of array to zero.
Слайд 21

Array Copy Constructor This constructor initializes an Array by making a

Array Copy Constructor

This constructor initializes an Array by making a copy

of an existing Array object.
Copy constructors are invoked whenever a copy of an object is needed, such as in passing an object by value to a function, returning an object by value from a function or initializing an object with a copy of another object of the same class.
The copy constructor is called in a declaration when an object of class Array is instantiated and initialized with another object of class Array.
Слайд 22

Слайд 23

Слайд 24

Слайд 25

Слайд 26

Слайд 27

Guidelines Design an application using C++ which can perform a defined

Guidelines

Design an application using C++ which can perform a defined task.


Your application should exhibit proper user interface and should shows appropriate menus wherever required.
Application should also handle basic validation checks applicable to your application.
Your application should incorporate the basic features of object oriented programming (eg. constructor, overloading, polymorphism, inheritance, static, final, file handling, exceptional handling etc.).
Each project can be made either individually or in a team of two members of the same group.
Слайд 28

Important Note You have to upload the source code along with

Important Note

You have to upload the source code along with 3

screen shots of your application on eclass
You have to give short presentation of approx. 7-10 min to explain and demonstrate your work.