Skip to main content

What is Operator Overloading?

 

OPERATOR OVERLOADING :

  •             The mechanism of giving such special meaning to an operator is known as operator overloading.
  •             Operator overloading provides flexible options for the creation of new definitions for most of the c++ operators accept the following:

            1] class member access operator(.)

            2] scope resolution operator(::)

            3] size of operator(size of)

            4] condition operator or ternary operator(?:)

  •  Operator overloading is used to redefine the operators available in C++.
  • It is used to perform operation on the user-defined data type.


Example for operator overloading :

  • Overload an operator '+' in a class like string to concatenate two strings using +.


Advantages of operator overloading :

  • Enables programmers to use notation closer to target domin.
  • Provides similar syntactic support to user-defined types.
  • It makes the program easier to understand.


Rules for operator overloading :

  • Cannot overload overloading data type.
  • Arity of operators cannot be changed.
  • Precedence of operators remains same.
  • Only build-in operators can be overloaded.

Overloading Unary Operators:

 

class unary

{

            int a;

            public:

            void getdata()

            {

                        cout<<"Enter number";

                        cin>>a;

            }

            void display()

            {

                        cout<<a;

            }

            void operator_()

            {

                        a=-a;

            }

};

void main()

            {                     

                        unary ob1;

                        ob1.getdata();

                        ob1.display();

                        -obz;

                        obz.display();

           

            }



Slip 1

Operator Overloading : Increment

 

Class increment

{

            int a;

            public;

            void getdata()

            {

                        cout<<”Enter Number”;

                        cin>>a;

            }

            void display()

            {

                        cout<<a;

            }

            void operator ++()

            {

                        a=++a;

            }

};

void main()

{

            increment ob1;

            ob1.getdata();

            ob1.diaplay();

            ++ob1;

            ob1.display();

            ob1++;

            ob1.display();

}

 

Slip 2

Operator Overloading : Decrement

 

Class decrement

{

            int a;

            public;

            void getdata()

            {

                        cout<<”Enter Number”;

                        cin>>a;

            }

            void display()

            {

                        cout<<a;

            }

            void operator --()

            {

                        a=--a;

            }

};

void main()

{

            increment ob1;

            ob1.getdata();

            ob1.diaplay();

            --ob1;

            ob1.display();

            ob1--;

            ob1.display();

}  


Slip 3

Binary Operator Overloading

 

Class binary

{

            int a,b;

            public;

            void getdata()

            {

                        cout<<”Enter Number”;

                        cin>>a>>b;

            }

            void display()

            {

                        cout<<a<<b;

            }

            void operator +(binary ob1)

            {

                        binary ob2;

                        ob2.a=a+ob1.a;

                        ob2.b=b+ob1.b;

                        return ob2;

            }

};

void main()

{

            binary b1,b2,b3 ;

            b1.getdata();

            b2.getdata();

b3=b1+b2

            b3.display();

}

 

Slip 4

Relational Operator Overloading

 

Class relational

{

            int a;

            public;

            void getdata()

            {

                        cout<<”Enter Number”;

                        cin>>a;

            }

            void display()

            {

                        cout<<”a=”<<a;;

            }

            int operator>(Relational r)

            {

                        if(a>r.a)

                        {

                                    Return 1;

                        }

                        Else

                        {

                                    Return 0;

                        }

            }

           

};

void main()

{

            Relational r1,r2;

            r1.getdata();

            r2.diaplay();

            if(r1>r2);

            {

                        r1.display();

                        cout<<”r1 is greater”;

            }

            else

            {

                        r2.display();

                        cout<<”r2 is greater”;

 

            }

}

 

Comments

Popular posts from this blog

Jarvis in Python

  JARVIS IN PYTHON                Python installing steps 1.   Download visual studio code 2.   Install python.org 3.   Verify python was installed on window      Python startup steps   1.   One visual studio code 2.   Welcome window 3.   Open folder 4.   New folder 5.   Select folder 6.   Create new file 7.   Python 3.7 A 32 bit 8.   Right click 9.   Window power shell 10. Write python 11. Write exit() 12. Install pip pandas 13. Print(“Hello world \n”) 14. Go to extention 15. Search code runner 16. Install it          Opening steps to create Jarvis : 1.   Desktop 2.   New folder 3.   Name : Jarvis 4.   Click shift 5.   Open power shell windows here 6.   Type – code 7.   Open editors 8.   Jarvis 9.   Name : jarvis.py          What is voice assistant ?    A voice assistant is a speech recognition process of audio in to test. Voice assistant like Alexa, siri, etc. Python provides an API called speech recognition to convert aud