OBJECTIVES
Overloading Binary Operators
Overloading Binary + Operator
Overloading Binary Operators using Friend
Overloading * Operator
Overloading Assignment Operator
What is Memory Allocation?
Dynamic memory Allocation in C++ using malloc()
Malloc() method
C++ free() method
Case Study: Array Class
Case Study: Array Class (Cont.)
Array Default Constructor
Array Copy Constructor
Guidelines
Important Note
Project Evaluation
922.50K
Категория: ПрограммированиеПрограммирование

Operator Overloading

1.

1
Operator
Overloading
2006 Pearson Education, Inc. All rights reserved.

2. OBJECTIVES

2
OBJECTIVES
1. What operator overloading is and how it makes programs more
readable and programming more convenient.
2. To redefine (overload) operators to work with objects of userdefined 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 singleargument constructors to perform implicit conversions.
2006 Pearson Education, Inc. All rights reserved.

3. Overloading Binary Operators

3
Overloading Binary Operators
1. 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
2006 Pearson Education, Inc. All rights reserved.

4. Overloading Binary + Operator

4
2006 Pearson Education, Inc. All rights reserved.

5. Overloading Binary Operators using Friend

5
Overloading Binary Operators using Friend
The complex number program to overload + operator may be modified
using a friend operator function as follows:
1.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));
}
2006 Pearson Education, Inc. All rights reserved.

6. Overloading * Operator

6
Overloading * Operator
2006 Pearson Education, Inc. All rights reserved.

7.

7
2006 Pearson Education, Inc. All rights reserved.

8. Overloading Assignment Operator

8
Overloading Assignment Operator
2006 Pearson Education, Inc. All rights reserved.

9.

9
2006 Pearson Education, Inc. All rights reserved.

10.

10
2006 Pearson Education, Inc. All rights reserved.

11. What is Memory Allocation?

11
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.
2006 Pearson Education, Inc. All rights reserved.

12. Dynamic memory Allocation in C++ using malloc()

12
C++ Dynamic memory allocation can be defined as a procedure in
which size of data structure(like Array) is changed during the runtime.
C++ provides some functions to achieve these tasks.There are 4
library functions provided by C++ defined under <stdlib.h> header
file to facilitate dynamic memory allocation in C++
programming.They are:
malloc()
free()
new
delete
2006 Pearson Education, Inc. All rights reserved.

13. Malloc() method

13
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.
2006 Pearson Education, Inc. All rights reserved.

14. C++ free() method

14
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);
2006 Pearson Education, Inc. All rights reserved.

15. Case Study: Array Class

15
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 !=
2006 Pearson Education, Inc. All rights reserved.

16. Case Study: Array Class (Cont.)

16
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…
2006 Pearson Education, Inc. All rights reserved.

17.

17
2006 Pearson Education, Inc. All rights reserved.

18.

18
2006 Pearson Education, Inc. All rights reserved.

19.

19
2006 Pearson Education, Inc. All rights reserved.

20. Array Default Constructor

20
Array Default Constructor
1. Default constructor for the class specifies a default size of 10
elements.
2. Default constructor in this example actually receives a single int
argument that has a default value 10.
3. 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.
4. Constructor uses a for statement to set all the elements of array to
zero.
2006 Pearson Education, Inc. All rights reserved.

21. Array Copy Constructor

21
Array Copy Constructor
1. This constructor initializes an Array by making a copy of an
existing Array object.
2. 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.
3. The copy constructor is called in a declaration when an object of
class Array is instantiated and initialized with another object of
class Array.
2006 Pearson Education, Inc. All rights reserved.

22.

22
2006 Pearson Education, Inc. All rights reserved.

23.

23
2006 Pearson Education, Inc. All rights reserved.

24.

24
2006 Pearson Education, Inc. All rights reserved.

25.

25
2006 Pearson Education, Inc. All rights reserved.

26.

26
2006 Pearson Education, Inc. All rights reserved.

27. Guidelines

27
Guidelines
1.
Design an application using C++ which can perform a defined task.
2.
Your application should exhibit proper user interface and should shows
appropriate menus wherever required.
3.
Application should also handle basic validation checks applicable to your
application.
4.
Your application should incorporate the basic features of object oriented
programming (eg. constructor, overloading, polymorphism, inheritance,
static, final, file handling, exceptional handling etc.).
5.
Each project can be made either individually or in a team of two members of
the same group.
2006 Pearson Education, Inc. All rights reserved.

28. Important Note

28
Important Note
1. You have to upload the source code along with 3
screen shots of your application on eclass
2. You have to give short presentation of approx. 7-10
min to explain and demonstrate your work.
2006 Pearson Education, Inc. All rights reserved.

29. Project Evaluation

29
Project Evaluation
1. Your work will be evaluated on following parameters
2. Problem Understanding (Clarity of your problem undertaken)
3. Usability ( design of user interfaces)
4. Scope (features/functions covered in your application)
5. Course Usage ( number of C++ features used in an application)
6. Validations handled.
2006 Pearson Education, Inc. All rights reserved.
English     Русский Правила