Proxy > Gmail Facebook Yahoo!

Problems on Operator Overloading Part-2



Problem #4: What would be the output of the following code:

     // Problem #4:
     // Problem related to 
     // Operators Overloading
     #include <iostream.h>

     class myclass
     {
       int a;
       int b;

    public:
      myclass(){}
      myclass(int x,int y){a=x;b=y;}
      void show()
      {
        cout<<a<<endl<<b<<endl;
      }

      friend myclass operator++(myclass);
    };

    myclass operator++(myclass ob)
    {
      ob.a++;
      ob.b++;

      return ob;
    }

    void main()
    {
      myclass a(10,20);

      ++a;

      a.show();
    }
Problem #5: What would be the output of the following code:

     // Problem #5:
     // Problem related to 
     // Operators Overloading
     #include <iostream.h>

     class myclass
     {
       int a;
       int b;

    public:
      myclass(){}
      myclass(int x,int y){a=x;b=y;}
      void show()
      {
        cout<<a<<endl<<b<<endl;
      }

      friend myclass operator+=(myclass,

    myclass);
    };

    myclass operator+=(myclass ob1, myclass ob2 )
    {
      ob1.a+=ob2.a;
      ob1.b+=ob2.b;

      return ob1;
    }

    void main()
    {
      myclass a(10,20);
      myclass b(100,200);

      a+=b;
      b+=a;

      a.show();
      b.show();
    }
Problem #6: Is there any error(s) in the following code:

     // Problem #6:
     // Problem related to 
     // Operators Overloading
     #include <iostream.h>

     class myclass
     {
       int a;
       int b;

    public:
      myclass(){}
      myclass(int x,int y){a=x;b=y;}
      void show()
      {
        cout<<a<<endl<<b<<endl;
      }

      myclass operator++(myclass &);
    };

    myclass myclass::operator++(myclass &ob)
    {
      ob.a++;
      ob.b++;

      return ob;
    }

    void main()
    {
      myclass a(10,20);

      ++a;

      a.show();
    }


Responses

0 Respones to "Problems on Operator Overloading Part-2"


Send mail to your Friends.  

Expert Feed

 
Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar