Performing Arithmetic Operation using constructors in C++ || Constructor || C++
In this, we are going to see how to perform Arithmetic Operations using Constructor, in C++.
 


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

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

class Addition
{
private:
    float c;

public:
    Addition(float a, float b);
};

Addition::Addition(float a, float b)
{
    c = a + b;
    cout << "Sum of the given 2 number is:- " << c << endl;
}

class Subtraction
{
private:
    float c;

public:
    Subtraction(float a, float b);
};

Subtraction::Subtraction(float a, float b)
{
    c = a - b;
    cout << "Difference of the given 2 number is:- " << c << endl;
}

class Multiplication
{
private:
    float c;

public:
    Multiplication(float a, float b);
};

Multiplication::Multiplication(float a, float b)
{
    c = a * b;
    cout << "Product of the given 2 number is:- " << c << endl;
}

class Division
{
private:
    float c;

public:
    Division(float a, float b);
};

Division::Division(float a, float b)
{
    c = a / b;
    cout << "Division of the given 2 number is:- " << c << endl;
}

void main()
{
    clrscr();
    float x, y;
    cout << "Enter 2 numbers:-\n";
    cin >> x >> y;

    Addition add(x, y);
    Subtraction sub(x, y);
    Multiplication mult(x, y);
    Division div(x, y);

    getch();
}

//........Coded by YASH ALAPURIA
 


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

#include <iostream>
#include <stdlib.h>
using namespace std;

class Addition
{
private:
    float c;

public:
    Addition(float a, float b);
};

Addition::Addition(float a, float b)
{
    c = a + b;
    cout << "Sum of the given 2 number is:- " << c << endl;
}

class Subtraction
{
private:
    float c;

public:
    Subtraction(float a, float b);
};

Subtraction::Subtraction(float a, float b)
{
    c = a - b;
    cout << "Difference of the given 2 number is:- " << c << endl;
}

class Multiplication
{
private:
    float c;

public:
    Multiplication(float a, float b);
};

Multiplication::Multiplication(float a, float b)
{
    c = a * b;
    cout << "Product of the given 2 number is:- " << c << endl;
}

class Division
{
private:
    float c;

public:
    Division(float a, float b);
};

Division::Division(float a, float b)
{
    c = a / b;
    cout << "Division of the given 2 number is:- " << c << endl;
}

int main()
{
    system("cls");
    float x, y;
    cout << "Enter 2 numbers:-\n";
    cin >> x >> y;

    Addition add(x, y);
    Subtraction sub(x, y);
    Multiplication mult(x, y);
    Division div(x, y);

    return 0;
}

//........Coded by YASH ALAPURIA


#ENJOY CODING

Post a Comment

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

Previous Post Next Post