Results 1 to 5 of 5

Thread: What is unary and binary operator in C++?

  1. #1
    Join Date
    Mar 2010
    Posts
    732

    What is unary and binary operator in C++?

    Hey guys, I am having my prelims in college after a week. I having a problem in unary and binary operator in C++?I don’t understand this term. I tried to but all the time I am not able to make so. I want to clear my concept in this two terms which is very important as per my exams and even for future programming. So, if anyone having any knowledge or information related to this topic than please let me know.

  2. #2
    Join Date
    May 2009
    Posts
    543

    Re: What is unary and binary operator in C++?

    Any unary operator can be overloaded. When we write a function to overload unary operator we must not pass argument to the function. It is necessary to note that a unary function does not pass an argument. Many of them make mistakes when they write an unary function but they forget that there should be no argument in it.

  3. #3
    Join Date
    May 2009
    Posts
    527

    Re: What is unary and binary operator in C++?

    Code:
    class   negate
                  {
                     private : int k ;
                     public : negate(  ) 
                        {
                          k  =  10;
                          }
                   public  : void  show ( )
                                      {
                                      cout << k  ;
                                       }
                   public  :  void operator -( )
                                     {
                                      k = - k ;
                                        }
                    };
         
                  main ( )
                       {
                      negate   ob;
                     ob.show ( );
    -ob ;
    ob.show ( );
    }
    
    output :-  10      -10 .

    This is an perfect example of unary operator where you will find that how the operator is been overloaded in unary. You can watch that there are no argument passed in this example which in real term is an unary operator.

  4. #4
    Join Date
    May 2009
    Posts
    511

    Re: What is unary and binary operator in C++?

    In the above program we have overloaded the unary operator – ( minus ). Normally the – operator makes some value negative. But it is used only with built in data types i . e. on variable of int , float. The above problem statement suggest to make the value of object negative. So in order to do that we required to put addition responsibility to operator – ( minus ) so that it will work on object also here in function.
    void operator -( )
    Operator is reserved keyword and - is the operator which is to be overloaded. Here this function makes the value of k negative. In main the object of negate class ob is created which have the value of k=10 ; the show function display it. Then – ob will call the ‘-‘ function on object ob and make the value of k as -10.

  5. #5
    Join Date
    May 2009
    Posts
    539

    Re: What is unary and binary operator in C++?

    Binary operator are operators which requires two operands to performs the operation. When they are overloaded they takes one argument. Say we want to perform the addition of two object as
    ob2 = ob + ob1
    Observe the above statement here the two objects ob and ob1 are added and result is stored back in ob2. so here the ‘+’ overloaded function will be called on object ob and it will take ob1 as argument. And it will return the object of type ob &ob1 which contain the addition of object ob & ob1 ;

    Code:
    For ex :-
    
                   class    x
                         {
                           int  k ;
                     public:  x  ( )
                         {
                          k =10 ;
                          }
                   void show ( )
                         {
                          cout << k ;
                          }
                      x  operator + + ( x  ob3 )
                          {
                             x  temp  ;
                             temp.k = k + ob3.k  ;
                             return ( temp)
                          }
                    } ;
    
                main ( )
                   {
                  x  ob, ob1;
                  x  ob3 ;
                 ob3 = ob + ob1  ;
                 ob3 .show ( )   ;
                    }

Similar Threads

  1. Replies: 4
    Last Post: 27-12-2010, 06:32 AM
  2. No match for operator<
    By Laurense in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:03 PM
  3. L4D - Server Operator
    By boos in forum Video Games
    Replies: 1
    Last Post: 06-10-2009, 02:52 PM
  4. Choose your ISD/STD operator
    By Rahul45 in forum India BroadBand
    Replies: 1
    Last Post: 05-02-2009, 02:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,292,279.88390 seconds with 17 queries