Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-12-2010
Member
 
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.
Reply With Quote
  #2  
Old 28-12-2010
Member
 
Join Date: May 2009
Posts: 533
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.
Reply With Quote
  #3  
Old 28-12-2010
Member
 
Join Date: May 2009
Posts: 523
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.
Reply With Quote
  #4  
Old 28-12-2010
Member
 
Join Date: May 2009
Posts: 503
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.
Reply With Quote
  #5  
Old 28-12-2010
Member
 
Join Date: May 2009
Posts: 532
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 ( )   ;
                }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is unary and binary operator in C++?"
Thread Thread Starter Forum Replies Last Post
what is the difference between unary and binary operator overloading in C #? Asis Software Development 4 27-12-2010 06:32 AM
No match for operator< Laurense Software Development 5 08-02-2010 02:03 PM
L4D - Server Operator boos Video Games 1 06-10-2009 02:52 PM
Choose your ISD/STD operator Rahul45 India BroadBand 1 05-02-2009 02:21 PM


All times are GMT +5.5. The time now is 10:32 AM.