Hi,
   I have a small query. why we need to return reference in prefix operator 
overloading. Because, when we call as ++obj , the operator ++ method will be 
called and we increment the values using this pointer. I will paste the code. 
Please let me know where I failed.

#include<stdio.h>
#include<conio.h>
#include<iostream>

using namespace std;

class mydata
{
private:
    int x,y;
public:
    mydata()
    {
        cout<<"I m i9n cons"<<endl;
    }
    mydata(int a,int b):x(a),y(b)
    {
        cout<<"I m in parm"<<endl;
    }
    void sow()
    {
        cout<<"x = "<<x<<"   y = "<<y<<endl;
    }
    mydata & operator +(mydata &obj)
    {
        x=x+obj.x;
        y=y+obj.y;
        return *this;
    }
    void operator ++()
    {
       this->x++;
        this->y++;
    }

    friend mydata& operator +(int val,mydata &obj)
    {
        obj.x=val+obj.x;
        obj.y=val+obj.y;
        return obj;
    }
    mydata(mydata &obj)
    {
        this->x=obj.x;
        this->y=obj.y;
    }
    mydata & operator =(mydata &obj)
    {
        if(this!=&obj)
        {
            this->x=obj.x;
            this->y=obj.y;
        
        }
            return *this;
    }
    

};
void main()
{
    mydata obj1(2,4);
    obj1.sow();
    mydata obj2(4,7);
    obj2.sow();
    obj1=obj1+obj2;
    obj1.sow();
#ifdef _DEBUG
    __asm{int 3}
#endif

    ++obj1;
    obj1.sow();
    mydata obj3=2+obj1;
    obj3.sow();

}

      GopiKrishna Komanduri
Software engineer
[EMAIL PROTECTED]
   

--- On Thu, 27/11/08, Gopi Krishna Komanduri <[EMAIL PROTECTED]> wrote:
From: Gopi Krishna Komanduri <[EMAIL PROTECTED]>
Subject: [c-prog] query on  Exception handling (in constructor and desctructor)
To: [email protected]
Date: Thursday, 27 November, 2008, 10:24 AM










    
            Hi,

    I have a query related to exceptional handling. I pasted the code I tried 
following this mail.

My intention is , what happens in a sistuation when any exception is caught in 
main , so while destructing the objects till that instant again if any 
exception is caught (stack unwinding concept).

similary in constructor. For constructor I know that the desctructors of the 
sub objects will be called . But how that exception is handled .



#include<stdio. h>

#include<conio. h>

#include<iostream>

using namespace std;

class var1

{

public:

    var1()

    {

        cout<<"I m in var1 conc"<<endl;

    }

    ~var1()

    {

        cout<<"I m in var1 desc"<<endl;

    }

};



class MyCls

{

public:

    var1 *obj;

    MyCls():obj( NULL)

    {

        obj=new var1();

        cout<<"I m in cons"<<endl;

    }

    ~MyCls()

    {

        

        cout<<"I m in desc"<<endl;

        throw "excepto";

        delete obj;

        cout<<"Deleted obj"<<endl;

    }

};



void main()

{

    try

    {

        MyCls obj;

        cout<<"Obj Created"<<endl;

        throw "error";

        cout<<"Obj after throwing err"<<endl;

    }

    catch(char *x)

    {

        cout<<x<<endl;

    }

    catch(...)

    {

        cout<<"I m in last"<<endl;

    }

}



GopiKrishna Komanduri

Software engineer

Hyderabadgopikomand [EMAIL PROTECTED] com

   



--- On Tue, 25/11/08, Brett McCoy <[EMAIL PROTECTED] com> wrote:

From: Brett McCoy <[EMAIL PROTECTED] com>

Subject: Re: [c-prog] Exception handling

To: [EMAIL PROTECTED] com

Date: Tuesday, 25 November, 2008, 9:29 PM



On Tue, Nov 25, 2008 at 9:10 AM, crystalcat_75



<crystalcat_ 75@ yahoo.co. in> wrote:



> Following program is showing an error while running in turbo c :



> undefined symbol for "try"



> statement missing after try .



I don't think Turbo C supports exceptions. You need to upgrade to a



modern compiler like Dev-C++ or MS Visual C++ Express (they are free)



and not use something that is 15+ years old.



-- Brett



------------ --------- --------- --------- --------- --------- -



"In the rhythm of music a secret is hidden;



If I were to divulge it, it would overturn the world."



-- Jelaleddin Rumi



        

         

        

        



        



        

        



Add more friends to your messenger and enjoy! Go to http://messenger. 
yahoo.com/ invite/



[Non-text portions of this message have been removed]




      

    
    
        
         
        
        








        


        
        


      Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/

[Non-text portions of this message have been removed]

Reply via email to