What is Operator Overloading?

 Operator Overloading

C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can make use of the addition operator (+) for string class to concatenate two strings. We know that the task of this operator is to add two operands. So a single operator ‘+’, when placed between integer operands, adds them and when placed between string operands, concatenates them.


Introduction

• Overloading an operator

– Write function definition as normal.

– Function name is keyword operator followed by the symbol for the operator being overloaded.

– operator+ used to overload the addition operator (+).

• Using operators

– To use an operator on a class object it must be overloaded unless the 

assignment operator(=)or the address operator(&)

  1.Assignment operator by default               performs memberwise assignment 

  2.Address operator (&) by default returns      the address of an object


Rules for Operator Overloading

1. Only existing operators can be overloaded. New operators cannot be overloaded.

2. The overloaded operator must have at least one operand that is of user defined type.

3. We cannot change the basic meaning of an operator. That is to say, We cannot redefine the plus(+) operator to subtract one value from the other.

4. Overloaded operators follow the syntax rules of the original operators. They cannot be overridden.

5. There are some operators that cannot be overloaded like size of operator(sizeof), membership operator(.), pointer to member operator(.*), scope resolution operator(::), conditional operators(?:) etc

6. We cannot use “friend” functions to overload certain operators.However, member function can be used to overload them. Friend Functions can not be used with assignment operator(=), function call operator(()), subscripting operator([]), class member access operator(->) etc.

7. Unary operators, overloaded by means of a member function, take no explicit arguments and return no explicit values, but, those overloaded by means of a friend function, take one reference argument (the object of the relevent class).

8. Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments.

9. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class.

10. Binary arithmetic operators such as +,-,* and / must explicitly return a value. They must not attempt to change their own arguments.


Unary Operator Overloading

#include<iostream.h>

#include<conio.h>

class complex

{

private:

int a,b,c;

public:

complex()

{}

void getvalue()

{

cout<<"Enter

the Two Numbers:";

cin>>a>>b;

}

void operator++()

{

a=++a;

b=++b;

}

void operator--()

{

a=--a;

b=--b;

}

void display()

{ cout<<a<<"+\t"<<b

<<"i"<<endl; }

};

void main()

{ clrscr();

complex obj;

obj.getvalue();

obj++;

cout<<"Increment

Complex Number\n";

obj.display();

obj--;

cout<<"Decrement

Complex Number\n";

obj.display();

getch();

}


Binary Operator Overloading

#include<iostream.h>

#include<conio.h>

class complex

{

private:

int a,b;

public:

void getvalue()

{

cout<<"Enter the value of 

Complex Numbers a,b:";

cin>>a>>b;

}

void display()

{

cout<<a<<"+"<<b<<"i"<<"\n"

;

}

complex operator+(complex ob)

{

complex t;

t.a=a+ob.a;

t.b=b+ob.b;

return(t);

}

complex operator-(complex ob)

{

complex t;

t.a=a-ob.a;

t.b=b-ob.b;

return(t);

}

};

void main()

{

clrscr();

complex obj1,obj2,result,result1;

obj1.getvalue();

obj2.getvalue();

result = obj1+obj2;

result1=obj1-obj2;

cout<<"Input Values:\n";

obj1.display();

obj2.display();

cout<<"Result:";

result.display();

result1.display();

getch();

}

Comments

Popular posts from this blog

Artificial Intelligence in Education

Puran Poli – The Sweet Soul of Maharashtrian Tradition

Future of AIML Engineering