Proxy > Gmail Facebook Yahoo!

Problems on Operator Overloading Part-2



Problem #1: Point out the error(s) if any in the following code:
     // Problem #1:
     // 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+(int);
    };

    myclass myclass::operator+(int x)
    {
      myclass temp;

      temp.a=a + x;
      temp.b=b + x;

      return temp;
    }

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

      a=a+10;
      a.show();
    }
Problem #2: Point out the error(s) if any in the following code:

     // Problem #2:
     // 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;
      }

      int operator=(int);
    };

    int myclass::operator=(int x)
    {
      a=x;
      b=x;

      return x;
    }

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

      c=b=a;

      c.show();
    }
Problem #3: Point out the error(s) if any 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::operator++()
    {
      a++;
      b++;

      return this;
    }

    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