Operator Overloading Demo - CSU1287P - CSE 2026 - Shoolini University

Program to define a Counter class with overloaded increment and decrement operators to modify and display count value in C++

Problem Statement

Demonstrate a `Counter` class with overloaded increment and decrement operators to modify and display the count value.

Concepts and Explanation

This C++ program defines a `Counter` class that represents a simple counter and provides the capability to increment and decrement its count value using overloaded operators. The program aims to showcase how operator overloading can be used to make the code more intuitive and readable.

Header Files: #include <iostream>

The `iostream` header file is included in this program to provide input and output stream functionality.

Main Function

The `main()` function is the entry point of the program and has a return type of `int`.

Counter Class

The `Counter` class is defined with a private member variable `count` and four member functions that overload the increment and decrement operators. The pre-increment operator `operator++()` increments the count value of the `Counter` object and returns a reference to the modified object. The post-increment operator `operator++(int)` creates a temporary object with the current count value, increments the count value of the original object, and returns the temporary object. The pre-decrement operator `operator--()` decrements the count value of the `Counter` object and returns a reference to the modified object. The post-decrement operator `operator--(int)` creates a temporary object with the current count value, decrements the count value of the original object, and returns the temporary object.

Console Input and Output

The `main()` function creates a `Counter` object `c` with an initial count value of 5 and demonstrates the use of the overloaded operators by incrementing and decrementing the count value of `c`. The program prints out the current count value after each operation using the `getCount()` member function.

Ending the Program

The program ends by returning `0` to indicate successful execution.

Method 1: Simplest Method

Code

                    
/*
 * -----------------------------------------------------------
 * OOPS using C++ Language (CSU1287P)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <iostream>

class Counter {
private:
    int count;
public:
    Counter() {
        count = 0;
    }
    Counter(int startCount) {
        count = startCount;
    }

    int getCount() const {
        return count;
    }

    Counter operator++() {
        count++;
        return *this;
    }

    Counter operator++(int) {
        Counter temp(count);
        count++;
        return temp;
    }

    Counter operator--() {
        count--;
        return *this;
    }

    Counter operator--(int) {
        Counter temp(count);
        count--;
        return temp;
    }
};

int main() {
    std::cout << "This program demonstrates a Counter class with overloaded increment and decrement operators to modify and display the count value. Value passed is 5";
    Counter c(5);
    std::cout << c.getCount() << std::endl; // output: 5

    ++c;
    std::cout << c.getCount() << std::endl; // output: 6

    c++;
    std::cout << c.getCount() << std::endl; // output: 7

    --c;
    std::cout << c.getCount() << std::endl; // output: 6

    c--;
    std::cout << c.getCount() << std::endl; // output: 5

    return 0;
}

Output

This program demonstrates a Counter class with overloaded increment and decrement operators to modify and display the count value. Value passed is 5.

5
6
7
6
5