Академический Документы
Профессиональный Документы
Культура Документы
Chapter 7
Operator Overloading
Definition: “Operator overloading means giving a new definition for the predefined
(existing) operator”.
For example, the addition operator + has the meaning only for adding two
primitive data items. Suppose we consider three complex object defined as
Complex p, q, r;
The operator + is not allowed to appear between p and q, because these are
non-primitive data items. C++ will flag an error message under this situation.
The compiler allows the programmer to use this binary operator for any user
defined data type provided there should be a function or instruction set to take care
this addition.
In other words, the programmer can use the predefined operator to suit his
requirements using the concept of operator overloading or operator polymorphism.
Syntax :
this pointer: The member of every class has to access implicit pointer / inbuilt
pointer is called this pointer, which points to the running object.
In other words, this pointer can hold the address of the running object. This
pointer is available to the programmer without declaring explicitly.
Output:
running object address =FFA10
running object content a = 10
Pointer: Pointer is variable, which holds the address of the other variable of same data type.
The computer memory consists of storage cells. Each cell has one byte of memory space. Each
byte in memory uniquely identified by a sequence number, are called as address.
Advantages:
Access the data indirectly.
Save the memory block i.e., reduce the program size.
Access bit, byte and word.
Faster accessing the data.
To stimulate function can return more than one values
Allocate and de allocates the memory block dynamically.
We can access CPU register directly.
Ex :
int a, *p;
Here ‘a’ is a integer variable, which holds the integer value.
*p is the integer pointer variable, which holds the address of the integer variable.
POINTER OF A POINTER:
It is a pointer variable it can hold the address of another pointer variable. It is denoted by
**Variable-name;
Review Questions
One mark Questions:
1. What do you mean by operator overloading?
2. What are the advantages of operator overloading?
3. Give the syntax of operator overloading function.
4. Which are the operators cannot be overloaded?
Three marks Questions:
1. Mention the rules of operator overloading.
Seven marks Questions:
1. a. Write a program to overload unary operator ++ to find the next date of the given
date.
b. Write a program to concatenate two strings by overloading operator binary ‘+’.
2. a. Write a program to overload binary ‘ + ‘ to find the sum of two matrices.
b. What is operator overloading? Mention any 2 operator which are not overloaded in
CPP