+ Reply to Thread
Results 1 to 4 of 4

Thread: Question 3

  1. #1
    Moderator
    Join Date
    Feb 2006
    Posts
    1,411
    Rep Power
    24
    hello



    Write a code to for simple operator overloading program



    bye



    sowmya






    http://livetolead.blogspot.com/
    all the best
    Arise Awake N Stop Not Until Ur Goal Is Reached!

  2. #2
    Member
    Join Date
    Apr 2006
    Location
    India
    Posts
    62
    Rep Power
    8
    #include<iostream.h>

    class CBase

    {

    public:

    const CBase& operator,(const CBase&) const

    {

    cout<<"CBase::operator,()"<<endl;

    return *this;

    }

    };


    class CDerive{};


    CDerive& operator,(int, CDerive& m_der)

    {

    cout<<"CDerive::operator,()"<<endl;

    return m_der;

    }


    int main()

    {

    CBase m_base1, m_base2;

    m_base1, m_base2; // operator comma called


    CDerive m_der;

    1,m_der; // operator comma called


    return 0;

    }



    Thanks,

    Veeru.

  3. #3
    Junior Member
    Join Date
    May 2006
    Location
    India
    Posts
    9
    Rep Power
    8


    #include<iostream.h>



    class Oper
    {
    int aa;
    public:
    Oper(int a):aa(a)
    {
    }
    Oper()
    {
    }
    ~Oper()
    {
    }


    int operator + (int a)
    {
    aa+=a;
    return aa;
    }


    operator int ()
    {
    return aa;
    }


    };





    int main()
    {


    int tt = 10;
    Oper aa(2);
    int c = aa + tt;
    c = tt + aa;
    return 0;
    }


    sk,
    Bangalore,
    India.

  4. #4
    Senior Member
    Join Date
    Mar 2006
    Posts
    607
    Rep Power
    16


    HI,


    HI,

    class complex

    {

    public:

    float x,y;

    public:

    complex()

    {

    }

    complex(float real,float ima)

    {

    x=real;

    y=ima;

    }

    complex operator +(complex);


    complex operator -(complex);

    void disp();

    };


    complex complex::operator +(complex c1)

    {

    complex f;

    f.x=x+c1.x;

    f.y=y+c1.y;

    return f;

    }


    complex complex::operator -(complex c1)

    {

    complex f;

    f.x=x-c1.x;

    f.y=y-c1.y;

    return f;

    }


    void complex::disp()

    {

    cout<<x<<"+j"<<y<<endl;


    cout<<x<<"-j"<<y<<endl;

    }


    void main()

    {

    complex c1,c2,c3,c4;

    c1=complex(2.5,7.5);

    c2=complex(2.5,8.5);

    c3=c1+c2;

    c4=c1-c2;

    c1.disp();

    c2.disp();

    c3.disp();

    c4.disp();

    }


    \"A GREAT PLEASURE IN LIFE IS DOING WHAT PEOPLE SAY YOU CANNOT DO\".

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Content Relevant URLs by vBSEO 3.5.1 PL1