***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***
Showing posts with label Operator. Show all posts
Showing posts with label Operator. Show all posts

Monday, June 14, 2021

Overloading Binary operators

Binary operators are also used in overloading. They are also overloaded either using member functions or friend functions.

The member function requires one argument, whereas friend function requires two arguments because friend function is not a member function.

Syntax:-

returntype operator(classname)

{

-----------

}

 

Example. //Demonstrating binary operators in overloading

#include<iostream.h>

#include<conio.h>

class sample

{

private:

int n;

public:

sample()

{

 n=0;

}

sample(int k)

{

n=k;

}

void show(char *msg)

{

cout<<msg<<n<<endl;

}

sample operator+(sample s)

{

sample temp;

temp.n=n+s.n;

return temp;

}

sample operator-(sample s)

{

sample temp;

temp.n=n-s.n;

return temp;

}

sample operator*(sample s)

{

sample temp;

temp.n=n*s.n;

return temp;

}

};

void main()

{

clrscr();

sample s1(10),s2(20),s3;

s1.show("S1 : ");

s2.show("S2 : ");

s3=s1+s2;

s3.show("S1+S2 : ");

s3=s1-s2;

s3.show("S1-S2 : ");

s3=s1*s2;

s3.show(“S1*S2 : “);

getch();

}

Overloading Unary Operators

The operator overloading can allow the unary operators to overload. The unary operators can perform the operations on a single operand.

The unary operators can be overloaded using a friend function and member function.

Using a member function to overload a unary operator

In operator overloading, the unary operators can be overloaded using a class member function. These member functions are defined as follows:

Syntax:-

public:

returntype operator op()

{

---------

}

Here, op is any unary operator that can be overloaded

 

Example. //overloading the prefix increment and decrement operators

#include<iostream.h>

#include<conio.h>

class sample

{

private:

int n;

public:

sample()

{

 n=0;

}

sample(int k)

{

 n=k;

}

void operator++()

{

n=n+2;

}

void operator--()

{

n=n-2;

}

void show(char *msg)

{

cout<<msg<<n<<endl;

}

};

void main()

{

clrscr();

sample s1(10),s2(10);

s1.show("S1 : ");

s2.show("S2 : ");

++s1;

--s2;

cout<<"After using operator overloading...\n";

s1.show("++S1 : ");

s2.show("--S2 : ");

getch();

}

Overload a unary operator using a Friend function

Like member functions, friend functions are also used in operator overloading. When a unary operator is overloaded using a friend function consider the following:

1) The function will take one operand as an argument.

2) The operand will be an object of the class.

3) The function will use the private members of the class only with the object.

4) The function may or may not return a value.

5) The function may take the object by using a value or by reference.

6) The friend function does not allow this pointer.

A friend function is declared inside the class and is defined outside the class.

Syntax:-

public:

friend returntype operator op ( classname obj);


OPERATOR OVERLOADING

The operators such as +, -, *, /, >, < etc is pre-defined in programming languages. They can perform the operations on built-in (standard) data types only. They do not work for user-defined data types such as objects.

C++ allows programmers to perform the operations on user-defined data types called objects. This feature called operator overloading.

With operator overloading, a programmer is allowed to provide his own definition for an operator to a class.

Like function overloading, operator overloading is also a form of compile time polymorphism.

The operator overloading function is preceded by the keyword operator followed by an operator.

Operator overloading function must be defined in public section of the class.

Syntax:-

class class_name

{

public:

returntype operator op (args_list)

{

statements;

}

};

Here, op is the operator that has to be overloaded

The following operators can be used in operator overloading.

         a) Unary operators - ++, --, -, !

         b) Binary operators – arithmetic, relational, and logical operators

         c) Other operators – [], (), =>, ->, new and delete operators

The operators that cannot be overloaded

         a) Scope resolution operator (: :)

         b) Member selection operator (.)

         c) Ternary operator (? : )

 

Implementing Operator Overloading

Operator overloading is usually implemented in two ways.

1) Through member function

2) Through friend function


1) Through Member Function

 

·     Number of explicit parameters is reduced by one, because calling object implicitly supplies one operand.

·         Unary operators take no explicit parameter.

·         Binary operators take one explicit parameter.

·         Left-hand operand has to be the calling object.

·         Obj2=obj1+10 is permissible, but obj2=10+obj1 is not permissible.

 

2) Through Friend Function

 

·         Number of explicit parameters is more.

·         Unary operators take only one parameter.

·         Binary operators take two parameters.

·         Left-hand operand need not be an object.

·         Obj2=obj1+10 as well as obj2=10+obj1 is permissible.