×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program for unary increment (++) and decrement (--) operator overloading

This program will demonstrate example of unary increment and unary decrement operator overloading in c++ programming language.

Unary increment (++) and decrement (--) operator overloading program in C++

// C++ program for unary increment (++) and
// decrement (--) operator overloading

#include <iostream>
using namespace std;

class NUM {
private:
    int n;

public:
    // function to get number
    void getNum(int x)
    {
        n = x;
    }
    // function to display number
    void dispNum(void)
    {
        cout << "value of n is: " << n;
    }
    // unary ++ operator overloading
    void operator++(void)
    {
        n = ++n;
    }
    // unary -- operator overloading
    void operator--(void)
    {
        n = --n;
    }
};

int main()
{
    NUM num;
    num.getNum(10);

    ++num;
    cout << "After increment - ";
    num.dispNum();
    cout << endl;

    --num;
    cout << "After decrement - ";
    num.dispNum();
    cout << endl;

    return 0;
}

Output

    After increment - value of n is: 11
    After decrement - value of n is: 10

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.