CPP - Operator Overloading

 

CPP

Operator Overloading



Fill in the blanks

                1.Sizeof operator can be Overloaded in Operator Overloading.[T/F].   False

          2. :: Operator can’t be Overloaded.[T/F]. True.

          3. Write a type of overloading  2  (Function Overloading , Operator Overlading)

 

Answer the following Question

                                                                              (10 marks)

                1.Write a Program for Arithmetic Operator Overloading Program

#include <iostream.h>

#include<conio.h>

class a

{

    private:

    int data;

    public:

    void getvalue()

    {

        cin>>data;

    }

    a operator+(a ob)

    {

        a t;

        t.data=data + ob.data;

        return t;

    }

    a operator- (a ob)

    {

    a t;

    t.data = data - ob.data;

    return t;

    }

operator* (a ob)

    {

    a t;

    t.data = data * ob.data;

    return t;

    }

operator/ (a ob)

    {

    a t;

    t.data = data / ob.data;

    return t;

    }

operator<(a ob)

    {

    a t;

    t.data = data < ob.data;

    return t;

    }

operator>(a ob)

    {

    a t;

    t.data = data > ob.data;

    return t;

    }

 

    int display()

    {

       

        return data;

       

    }

};

void  main()

{

 

    a obj1, obj2, sum, sub,mul,div,lt,gt;

    clrscr();

    cout<<"enter an integer value for obj1: ";

    obj1.getvalue();

    cout<<"Enter an integer value for obj2: ";

    obj2.getvalue();

    sum= obj1+obj2;

    sub=obj1-obj2;

    mul=obj1*obj2;

    div=obj1/obj2;

    lt=obj1<obj2;

    gt=obj1>obj2;

    cout<<"Addition result is = "<<sum.display()<<endl;

    cout<<"Subtraction result is = "<<sub.display()<<endl;

    cout<<"Multiplication result is ="<<mul.display()<<endl;

    cout<<"Division result is = "<<div.display()<<endl;

    cout<<"Less than result is = "<<lt.display()<<endl;

    cout<<"greater than result is = "<<gt.display()<<endl;

    getch();

   

}

No comments:

Post a Comment