EXERCISE - 8

A) Write a c ++ program to implement the overloading unary operator.
B) Write a c ++ program to implement the overloading binary operator.
C) Write a c ++ program to implement the overloading assignment = operator.
A) AIM: Write a c ++ program to implement the overloading unary operator.
SOURCE CODE:
#include<iostream>
using namespace std;
class data
{
    private:
        int num1,num2;
    public:
        data(int n1,int n2)
        {
            num1=n1;
            num2=n2;
        }
        void display()
        {
            cout<<"num1="<<num1<<endl;
            cout<<"num2="<<num2<<endl;
        }
        void operator++()
        {
            ++num1;
            ++num2;
        }
};
int main()
{
    int n1,n2;
    cout<<"Enter two numbers:"<<endl;
    cin>>n1>>n2;
    data d(n1,n2);
    cout<<"Before Increment numbers are:"<<endl;
    d.display();
    ++d;
    cout<<"After Increment numbers are:"<<endl;
    d.display();
}
OUTPUT:
Enter two numbers:
8
5
Before Increment numbers are:
num1=8
num2=5
After Increment numbers are:
num1=9
num2=6

B) AIM: Write a c ++ program to implement the overloading binary operator.
SOURCE CODE:
#include<iostream>
using namespace std;
class ComplexNum
{
    private:
        double real,imaginary;
    public:
        void numbers(double r,double i)
        {
            real=r;
            imaginary=i;
        }
        void display()
        {
            cout<<real<<"+"<<imaginary<<"i"<<endl;
        }
        ComplexNum operator+(ComplexNum cno)
        {
            ComplexNum c;
            c.real=this->real+cno.real;
            c.imaginary=this->imaginary+cno.imaginary;
            return c;
        }
};
int main()
{
    double r1,i1,r2,i2;
    cout<<"Enter first complex number:"<<endl;
    cin>>r1>>i1;
    cout<<"Enter second complex number:"<<endl;
    cin>>r2>>i2;
    ComplexNum c1,c2,c3;
    c1.numbers(r1,i1);
    c2.numbers(r2,i2);
    cout<<"First complex number is:"<<endl;
    c1.display();
    cout<<"Second complex number is:"<<endl;
    c2.display();
    c3=c1+c2;
    cout<<"Sum of two complex numbers is:"<<endl;
    c3.display();
}
OUTPUT:
Enter first complex number:
5.1
2.3
Enter second complex number:
4.6
1.8
First complex number is:
5.1+2.3i
Second complex number is:
4.6+1.8i
Sum of two complex numbers is:
9.7+4.1i

C) AIM: Write a c ++ program to implement the overloading assignment = operator.
SOURCE CODE:
#include<iostream>
using namespace std;
class AssignOp
{
    private:
        int a;
        double b;
    public:
        void input(int n1,double n2)
        {
            a=n1;
            b=n2;
        }
        void output()
        {
            cout<<"Given values are:"<<endl;
            cout<<a<<"\t"<<b<<endl;
        }
        void operator=(AssignOpao)
        {
            a=ao.a;
            b=ao.b;
        }
};
int main()
{
    int n1;
    double n2;
    cout<<"Enter integer and double values:"<<endl;
    cin>>n1>>n2;
    AssignOp a1,a2;
    a1.input(n1,n2);
    a1.output();
    a2.operator=(a1);
    a2.output();
}
OUTPUT:
Enter integer and double values:
5
2.13
Given values are:
5       2.13
Given values are:
5       2.13