LAB Practical III

a) write a c++ program to implement binary and unary operator overloading  

Comments

  1. AIM : TO create a program to implement unary operator overloading
    PROCEDURE :
    The unary operators operate on a single operand and following are the examples of Unary operators −
    i)The increment (++) and decrement (--) operators.
    ii)The unary minus (-) operator.
    iii)The logical not (!) operator.
    The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in obj, -obj, and ++obj but sometime they can be used as postfix as well like obj++ or obj--.
    PROGRAM :
    #include
    using namespace std;

    class Distance
    {
    private:
    int feet;
    int inches;

    public:

    Distance()
    {
    feet = 0;
    inches = 0;
    }
    Distance(int f, int i)
    {
    feet = f;
    inches = i;
    }


    void displayDistance()
    {
    cout << "F: " << feet << " I:" << inches <<endl;
    }


    Distance operator- ()
    {
    feet = -feet;
    inches = -inches;
    return Distance(feet, inches);
    }
    };

    int main()
    {
    Distance D1(11, 10), D2(-5, 11);

    -D1;
    D1.displayDistance();

    -D2;
    D2.displayDistance();

    return 0;
    }

    Error : 0
    Output :
    F: -11 I: -10
    F: 5 I: 11
    Result :
    I have successfully done my program.

    ReplyDelete
  2. AIM : TO create a program to implement binary operator overloading
    PROCEDURE :
    Step 1: Start the program.
    Step 2: Declare the class.
    Step 3: Declare the variables and its member function.
    Step 4: Using the function getvalue() to get the two numbers.
    Step 5: Define the function operator +() to add two complex numbers.
    Step 6: Define the function operator –()to subtract two complex numbers.
    Step 7: Define the display function.
    Step 8: Declare the class objects obj1,obj2 and result.
    Step 9: Call the function getvalue using obj1 and obj2
    Step 10: Calculate the value for the object result by calling the function operator + and operator -.
    Step 11: Call the display function using obj1 and obj2 and result.
    Step 12: Return the values.
    Step 13: Stop the program.
    PROGRAM :
    #include
    using namespace std;

    class complex
    {
    int a, b;
    public:

    void getvalue()
    {
    cout << "Enter the value of Complex Numbers a,b:";
    cin >> a>>b;
    }

    complex operator+(complex ob)
    {
    complex t;
    t.a = a + ob.a;
    t.b = b + ob.b;
    return (t);
    }

    complex operator-(complex ob)
    {
    complex t;
    t.a = a - ob.a;
    t.b = b - ob.b;
    return (t);
    }

    void display()
    {
    cout << a << "+" << b << "i" << "\n";
    }
    };

    int main()
    {

    complex obj1, obj2, result, result1;

    obj1.getvalue();
    obj2.getvalue();

    result = obj1 + obj2;
    result1 = obj1 - obj2;

    cout << "Input Values:\n";
    obj1.display();
    obj2.display();

    cout << "Result:";
    result.display();
    result1.display();

    return 0;
    }
    Error : 0
    Output :
    Enter the value of Complex Numbers a, b
    4
    5
    Enter the value of Complex Numbers a, b
    2
    2
    Input Values
    4 + 5i
    2 + 2i
    Result
    6 + 7i
    2 + 3i
    Result :
    I have successfully done my program.

    ReplyDelete
  3. Gold prices were flat early on Friday as the dollar steadied ahead of the G20 meet in Argentina this weekend
    share-tips-expert

    ReplyDelete
  4. AIM:To show the program of unary operator in c++
    PROCEDURE: In this program declare the class and also declare the variable and its member function.And in this program use the function getsample()to get the two number.The function operator ++ to increment the values and the function -- to decrement the values and atlast call the getsample() and dispsample().
    PROGRAM:
    #include
    using namespace std;
    class sample
    {
    private:
    int a;
    public:
    void getsample(int x)
    {
    a=x;
    }
    void dispsample(void)
    {
    cout<<"value of a is:"<<a;
    }
    void operator ++(void)
    {
    a=++a;
    }
    void operator --(void)
    {
    a=--a;
    }
    };
    int main()
    {
    sample obj;
    obj.getsample(10);
    ++obj;
    cout<<"After increment -";
    obj.dispsample();
    cout<<endl;
    --obj;
    cout<<"After decrement-";
    obj.dispsample();
    cout<<endl;
    return 0;
    }
    ERROR:0
    OUTPUT:After increment-value of a is:11
    After decrement-value of b is:10
    RESULT:I have done my program successfully.

    ReplyDelete
  5. AIM:-To create a program to implement unary operator overloading.
    PROCEDURE:-Binary operator(operator that operates on two operands) one of the operands should be passed argument to the operator function.
    1.Three objects type of complex are created and user is asked to enter the real and imaginary parts for two complex numbers which are stored in object c1.
    2.Then statement result=c1 is excuted the statement invokes the operator function.
    PROGRAM:-
    #include
    using namespace std;
    class complex
    {
    int real,imaginary;
    public:
    complex()
    {
    }
    complex(int a,int b)
    {
    real=a;
    imaginary=b;
    }
    void operator-();
    void display()
    {
    cout<<"real value:"<<real<<endl;
    cout<<"imginary value:"<<imaginary<<endl;
    }
    };
    void complex::operator-()
    {
    real= -real;
    imaginary= -imaginary;
    }
    int main()
    {
    complex c1(10,12);
    cout<<"real and imaginary value before operation:"<<endl;
    c1. display();
    -c1;
    cout<<"real and imaginary value after operation:"<<endl;
    c1.display();
    return 0;
    }
    ERROR:-0
    OUTPUT:-
    Real and imaginary value before operation:
    Real value=10
    Imaginary value=12
    Real and imaginary value after operation:
    Real value=-10
    Imaginary value=-12
    RESULT:-I have successfully done my program.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete

  7. Aim:
    Write a program to implement binary and unary operator overloading
    PROCEDURE:
    This program is made where the unary operator ‘–‘ is used and binary operator ‘*’ is used to add operate the program .
    PROGRAM:
    #include
    using namespace std;
    class overload
    {
    public:
    int a;
    public:
    void getdata(int x);
    void display(void);
    void operator -();
    };
    void overload::getdata(int x)
    {
    a=x;
    }
    void overload::display(void)
    {
    cout<<a<<" ";
    }
    void overload::operator-()
    {
    a=-a;
    }
    class binary
    {
    public:
    int q,r;
    public:
    binary(){ }
    binary(int e,int f)
    {
    q=e;
    r=f;}

    binary operator*(binary);
    void show(void);

    };
    binary binary::operator*(binary b)
    {
    binary t;
    t.q=q*b.q;
    t.r=r*b.r;
    return(t);
    }
    void binary:: show(void)
    {
    cout<<q<<"*"<<r<<"\n";
    }
    int main()
    {
    overload s;
    s.getdata(12);

    cout<<"result :"<<endl;
    -s;
    s.display();

    binary b1,b2,b3;
    cout<<"\n";
    b1=binary(5,3);
    b2=binary(5,4);
    b3=b1*b2;
    cout<<"b1 =";
    b1.show();
    cout<<"b2 =";
    b2.show();
    cout<<"b3 =";
    b3.show();
    return 0;
    }
    OUTPUT:
    Result:
    -12
    b1=(5*3)
    b2=(5*4)
    b3=25*12
    ERROR 0


    ReplyDelete
  8. Aim:- TO create a program to implement unary operator overloading.
    Procedure:- In this C++ program, Binary operator(operator that operates on two operands) one of the operands should be passed argument to the operator function.
    The unary operators operate on a single operand and following are the examples of Unary operators −
    i)The increment (++) and decrement (--) operators.
    ii)The unary minus (-) operator.
    iii)The logical not (!) operator.
    Program:-
    #include
    using namespace std;
    class complex
    {
    private:
    int real;
    int img;
    public:
    complex(int r=e, int i=e)
    {
    real=r;
    imag=i;
    }
    complex operator +(complex const obj) //binary opearor
    {
    complex sum;
    sum. real=real + obj.real;
    sum. imag=imag + obj.imag;
    return sum;
    }
    void print()
    {
    cout<<real<<" +i"<<imag<<endl;
    }
    };
    class incr
    {
    private:
    int i;
    public:
    incr()
    {i=e;}
    void operator ++() //unary operator
    {++i;}
    void display()
    {
    cout<<"i="<<i<<endl;
    }
    };
    int main()
    incr obj;
    obj.display();
    ++obj;
    obj.display();
    complex c1(1e,8);
    complex c2(2,3);
    complex c3= c1+c2;
    c3.print();
    return 0;
    }
    Error:- No error found.
    Output:- real=1
    image=1
    Result:- This program executed successfully.
    Aim:- TO create a program to implement unary operator overloading.
    Procedure:- In this C++ program, Binary operator(operator that operates on two operands) one of the operands should be passed argument to the operator function.
    The unary operators operate on a single operand and following are the examples of Unary operators −
    i)The increment (++) and decrement (--) operators.
    ii)The unary minus (-) operator.
    iii)The logical not (!) operator.
    Program:-
    #include<iostream>
    using namespace std;
    class complex
    {
    private:
    int real;
    int img;
    public:
    complex(int r=e, int i=e)
    {
    real=r;
    imag=i;
    }
    complex operator +(complex const obj) //binary opearor
    {
    complex sum;
    sum. real=real + obj.real;
    sum. imag=imag + obj.imag;
    return sum;
    }
    void print()
    {
    cout<<real<<" +i"<<imag<<endl;
    }
    };
    class incr
    {
    private:
    int i;
    public:
    incr()
    {i=e;}
    void operator ++() //unary operator
    {++i;}
    void display()
    {
    cout<<"i="<<i<<endl;
    }
    };
    int main()
    incr obj;
    obj.display();
    ++obj;
    obj.display();
    complex c1(1e,8);
    complex c2(2,3);
    complex c3= c1+c2;
    c3.print();
    return 0;
    }
    Error:- No error found.
    Output:- real=1
    image=1
    Result:- This program executed successfully.
    v

    ReplyDelete

Post a Comment

Popular posts from this blog

INTRODUCTION OF HADOOP & SPARK

B.TECH DIP- System Programming