by Dinesh Thakur Category: Classes in C++

 

#include <iostream.h>

#include <conio.h>
//******Class Definition********
class Minus
{
      private:
      int a, b, c ;
                      public:
 //**********Member Functions ***************
                      Minus() {} // Default Constructor
                      Minus(int A, int B, int C)
                     {a=A; b=B; c=C;}
                     void display(void)
                       {
                            cout << "\t a = " << a<< endl ;
                            cout << "\t b = " << b << endl ;
                            cout << "\t c = "<< c << endl ;
                       }
//********Operator Function Definition *******
                Minus operator - (void) // Declarative with the return type Minus
          {
                Minus temp;
                temp.a = -a ;
                temp.b = -b ;
                temp.c = -c ;
                return temp;
          }
} ; // End of the class definition
//*******Main Functiion************
void main(void)
        {
                  clrscr();
                  Minus M1(5, 10, -15); // Initialization of the object M1 of type Minus
                  cout<< "\n Before activating the operator -( ):\n";
                  M1.display( ) ;
                  Minus M2 ; // Definining the object M2 of type Minus
                  M2 = -M1 ; // Activates the function operator -( ) and
                  // assigns the value of -M1 to M2
                  cout<< "\n After activating the operator -( ):\n" ;
                  M2.display( ) ;
                  getch();
        }

Overloading Of Unary Minus (-) Operator And Return Value



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles