Division of Two numbers using Operator Overloading cpp

In this program, we are going see how to perform and display Division of Two numbers using Function Overloading  in C++ Programming Language.


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>

class Division
{
    int n1, n2, res;

    public:
    void setn1(int num1 = 0)
    {
        n1 = num1;
    }
    void setn2(int num2 = 0)
    {
        n2 = num2;
    }
    Division operator /(Division n)
    {
        Division m;
        m.res = n1 / n.n2;
        return m;
    }
    void display()
    {
        cout << "Result of division: " << res;
    }
};

void main()
{
    clrscr();
    int dividend, divisor;
    cout << "Enter the Dividend: ";
    cin >> dividend;
    cout << "Enter the Divisor: ";
    cin >> divisor;
    Division d1;
    d1.setn1(dividend);
    Division d2;
    d2.setn2(divisor);
    Division d3;
    d3 = d1 / d2;
    d3.display();
    getch();
}

//.......Coded by SAHIL SHAIKH

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class Division
{
    int n1, n2, res;

    public:
    void setn1(int num1 = 0)
    {
        n1 = num1;
    }
    void setn2(int num2 = 0)
    {
        n2 = num2;
    }
    Division operator /(Division n)
    {
        Division m;
        m.res = n1 / n.n2;
        return m;
    }
    void display()
    {
        cout << "Result of division: " << res;
    }
};

int main()
{
    int dividend, divisor;
    cout << "Enter the Dividend: ";
    cin >> dividend;
    cout << "Enter the Divisor: ";
    cin >> divisor;
    Division d1;
    d1.setn1(dividend);
    Division d2;
    d2.setn2(divisor);
    Division d3;
    d3 = d1 / d2;
    d3.display();
    return 0;
}

//.......Coded by SAHIL SHAIKH

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post