Comments

Thursday, November 8, 2012

OOP Write program using Operator Overloading

Posted by at 9:59 AM Read our previous post
Object :


Write program for add and subtract and check equal numbers real and imaginary numbers
using operator overloading

Source code:
#include <iostream>
using namespace std;

class complex
{
private:
    unsigned int real,real1,real2;//////////////////// this number never be negetive////////////////////
    int imaginary,imaginary1,imaginary2;
public:

    complex()
    {}
    void getvalue(int x,int y,int z,int w)
    {
        real=x;
        real1=z;

        imaginary=y;
        imaginary1=w;
       
    }
   
    void operator++()
    {
        real2=real+real1;
        imaginary2=imaginary+imaginary1;

        cout<<"Real"<<real2<<endl;
        cout<<"Imaginary"<<imaginary2<<endl;
    }
    void operator--()
    {
        real2=real+real1;
        imaginary2=imaginary+imaginary1;

        cout<<"Real"<<real2<<endl;
        cout<<"Imaginary"<<imaginary2<<endl;
   
    }
    void operator==(int i)
    {
        if(real==real1)
        {
            cout<<"Real numbers are equals"<<endl;
        }
        else if(real!=real1)
        {    cout<<"Real number are not equal"<<endl;}
       
        if(imaginary==imaginary1)
        {
            cout<<"Imaginary numbers are equals"<<endl;
        }
        else if(imaginary!=imaginary1)
        {    cout<<"Imaginary number are not equal"<<endl;}
    }

   
};
void main()
{
    char again;
    do{char press;
    complex C1;

    cout<<"Press I to input value"<<endl;
    cout<<"Press A to add real and imaginary"<<endl;
    cout<<"Press S to subtract real and imaginary"<<endl;
    cout<<"Press C to check real and imaginary are equal or not "<<endl;
    cout<<"Press E to Exit"<<endl;
    cin>>press;
    switch(press)
    {    case 'i':
    {
   
    int real,real2,imaginary2,imaginary;
    cout<<"Enter 1st real number: "<<endl;
    cin>>real;
    cout<<"Enter 2nd real number"<<endl;
    cin>>real2;
    cout<<"Enter 1st imaginary number: "<<endl;
    cin>>imaginary;
    cout<<"Enter 2nd imaginary number"<<endl;
    cin>>imaginary2;

    C1.getvalue(real,imaginary,real2,imaginary2);
    break;}

        case 'a':
   
            {
               
    int real,real2,imaginary2,imaginary;
    cout<<"Enter 1st real number: "<<endl;
    cin>>real;
    cout<<"Enter 2nd real number"<<endl;
    cin>>real2;
    cout<<"Enter 1st imaginary number: "<<endl;
    cin>>imaginary;
    cout<<"Enter 2nd imaginary number"<<endl;
    cin>>imaginary2;

    C1.getvalue(real,imaginary,real2,imaginary2);
    C1++;
                break;}
        case 's':
            {int real,real2,imaginary2,imaginary;
    cout<<"Enter 1st real number: "<<endl;
    cin>>real;
    cout<<"Enter 2nd real number"<<endl;
    cin>>real2;
    cout<<"Enter 1st imaginary number: "<<endl;
    cin>>imaginary;
    cout<<"Enter 2nd imaginary number"<<endl;
    cin>>imaginary2;
    C1.getvalue(real,imaginary,real2,imaginary2);
   
    C1--;
    break;}

    case 'c':
            {
    int real,real2,imaginary2,imaginary;
    cout<<"Enter 1st real number: "<<endl;
    cin>>real;
    cout<<"Enter 2nd real number"<<endl;
    cin>>real2;
    cout<<"Enter 1st imaginary number: "<<endl;
    cin>>imaginary;
    cout<<"Enter 2nd imaginary number"<<endl;
    cin>>imaginary2;
        C1.getvalue(real,imaginary,real2,imaginary2);
            int a=0;
            C1==(a);
            }

           
    }cin>>again;
    }
    while(again=='y');
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template