Incrementing Value by two using Operator Overloading cpp

In this program, we are going see how to Increment value of any number by two 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 Increm
{
    int num;

    public:
    setval(int n)
    {
        num = n;
    }
    Increm operator++()
    {
        num = num + 2;
    }
    void print()
    {
        cout << "The Incremented Value is : " << num;
    }
};

void main()
{
    clrscr();
    int n;
    cout << "Enter a number: ";
    cin >> n;
    Increm i;
    i.setval(n);
    ++i;
    i.print();
    getch();
}

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

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

#include <iostream>
using namespace std;

class Increm
{
    int num;

    public:
    setval(int n)
    {
        num = n;
    }
    Increm operator++()
    {
        num = num + 2;
    }
    void print()
    {
        cout << "The Incremented Value is : " << num;
    }
};

int main()
{
    int n;
    cout << "Enter a number: ";
    cin >> n;
    Increm i;
    i.setval(n);
    ++i;
    i.print();
    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