Wednesday, March 28, 2018

Write a C++ program to overload unary operator as member function.


AIM: Write a C++ program to overload unary operator as member function.
THEORY:
. The unary operators operate on a single operand and following are the examples of Unary operators −
The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometime they can be used as postfix as well like obj++ or obj--.
SOURCE CODE:
#include<iostream>
using namespace std;
class num                           
{
            private:
            int a,b,c;
            public:
            num(int j,int k,int m)
            {
                        a=j;b=k;c=m;
             }
                        void show(void);
                        void operator ++( );    
};
void num::show()
{
            cout<<"\n a= "<<a<<"\n b= "<<b<<"\n c= "<<c;
}
void num::operator ++( )
{
            ++a;
            ++b;
            ++c;
}
int main(){
            num n(3,2,5);
            n.show();
            ++n;
            n.show();
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home