Use of Modulus Operator in Constructor and Destructor|| Constructor || Destructor || C++
In this, we are going to see how to use Modulus Operator using Constructor and Destructor, in C++.
 


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

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

class Modulus
{
private:
    int x, y;
    float mod;

public:
    Modulus(int num1, int num2)
    {
        cout << "Constructor is called.\n";
        x = num1;
        y = num2;
        mod = x % y;
    }
    int display()
    {
        cout << "\nEntered numbers are:-\nnum1 = " << x << "\nnum2 = " << y << endl;
        cout << "The Remainder after dividing both numbers is " << mod;
    }
    ~Modulus()
    {
        cout << "\n\nDESTRUCTOR is called.";
    }
};

void main()
{
    clrscr();
    float x, y;
    cout << "Enter 2 number:-\n";
    cin >> x >> y;
    Modulus ag(x, y);
    ag.display();
    getch();
}
//.........Coded by YASH ALAPURIA
 


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

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

class Modulus
{
private:
    int x, y;
    float mod;

public:
    Modulus(int num1, int num2)
    {
        cout << "Constructor is called.\n";
        x = num1;
        y = num2;
        mod = x % y;
    }
    int display()
    {
        cout << "\nEntered numbers are:-\nnum1 = " << x << "\nnum2 = " << y << endl;
        cout << "The Remainder after dividing both numbers is " << mod;
    }
    ~Modulus()
    {
        cout << "\n\nDESTRUCTOR is called.";
    }
};

int main()
{
    system("cls");
    float x, y;
    cout << "Enter 2 number:-\n";
    cin >> x >> y;
    Modulus ag(x, y);
    ag.display();
    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