Вы находитесь на странице: 1из 3

C++ Programming 61 Operator Overloading

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 statement in the main function as r = p + q;

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 :

Rtype operator oprSymbol(argList)


{ Where, Rtype -> return type of the operator function.
: operator -> is a keyword
: oprSymbol -> operator symbol
} argList -> argument list.

Rules for operator overloading:


1. Only existing operators can be overloaded. New operators can not be created.
2. The precedence of the operator cannot be changed.
3. The number of operands that an operator takes cannot be altered.
4. Overloaded operators follow the syntax of the original operators. They can not be
overridden.
5. We can not change the basic meaning of an operator. That is we can not redefine the
+ operator for subtraction.
6. Operator functions must be either member functions or friend functions.

P61. WAP to iilustrate overload unary ++ operator.


P62. WAP to overloading binary + operator to find addition of two complex numbers
P63. WAP to over load binary + operator to concatenate two strings and compare the
two string using == as operator overloading.
P64. WAP to over load binary + operator to find the sum of two matrices.

Mahesha. S SSC, Tumkur Cell: 9886044689


C++ Programming 62 Operator Overloading

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.

P65. WAP to illustrate this pointer.


class xyz
void main()
{
{
private : int a;
clrscr();
xyz p;
public :
p.putaddress();
xyz()
p.putdata();
{
getch();
a = 10;
}
}
void putaddress()
{
cout <<endl<<”running object address = “<<this;
}
void putdata()
{
cout <<endl<<”running object content a = “<<this->a;
}
}

Output:
running object address =FFA10
running object content a = 10

Note: Some of the operator cannot be overloaded, such as


 The dot operator (.)
 The scope resolution operator(::)
 The conditional operator (?:)
 The pointer to member operator(.*)
 The sizeof() operator.

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.

DECLARATION OF THE POINTER:

Data type *pointer –variable;

Mahesha. S SSC, Tumkur Cell: 9886044689


C++ Programming 63 Operator Overloading

Where, data type is any valid or derived data type.


*the pointer.
Pointer-variable is the user defined variable name.

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.

W.A.P. to illustrate the pointer.

POINTER OF A POINTER:

It is a pointer variable it can hold the address of another pointer variable. It is denoted by
**Variable-name;

Eg. int a, *p,**q;

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.
**q is the integer pointer of a pointer variable, which holds the address of the anther integer
pointer variable.

W A P to illustrate pointer of a pointer.

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

Mahesha. S SSC, Tumkur Cell: 9886044689

Вам также может понравиться