Menu
Coddy logo textTech

Comparison Operator Overload

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 42 of 104.

Comparison operators like ==, !=, <code><, , <=, and >= let you compare objects of your custom classes. These operators return a bool and should be marked const since they don't modify either operand.

class Date {
    int year, month, day;
    
public:
    Date(int y, int m, int d) : year(y), month(m), day(d) {}
    
    bool operator==(const Date& other) const {
        return year == other.year && 
               month == other.month && 
               day == other.day;
    }
    
    bool operator<(const Date& other) const {
        if (year != other.year) return year < other.year;
        if (month != other.month) return month < other.month;
        return day < other.day;
    }
};

A useful technique is implementing other comparison operators in terms of the ones you've already defined. Once you have == and <code><, the rest follow naturally:

bool operator!=(const Date& other) const {
    return !(*this == other);
}

bool operator>(const Date& other) const {
    return other < *this;
}

bool operator<=(const Date& other) const {
    return !(other < *this);
}

bool operator>=(const Date& other) const {
    return !(*this < other);
}

This approach reduces code duplication and ensures all your comparison operators behave consistently. If you later change how equality or less-than works, the other operators automatically stay in sync.

challenge icon

Challenge

Easy

Let's build a Version class that represents software version numbers (like 2.1.0 or 3.0.5) and supports all six comparison operators. This is a practical use case where comparing versions determines which software is newer — perfect for practicing comparison operator overloading.

You'll create two files to organize your code:

  • Version.h: Define a Version class that stores three integers representing major, minor, and patch version numbers. Your class should support:
    • A constructor that takes major, minor, and patch values
    • Getters for each component: getMajor(), getMinor(), getPatch() (all const)
    • All six comparison operators: ==, !=, <, >, <=, >=
    • A toString() method that returns the version as a string in the format "major.minor.patch"

    Implement == and < directly, then define the remaining four operators in terms of these two. For the less-than comparison, compare major first, then minor if majors are equal, then patch if both major and minor are equal. All comparison operators should be const member functions.

  • main.cpp: Read six integers from input representing two versions (major1, minor1, patch1, major2, minor2, patch2 on separate lines). Create two Version objects and compare them using all your operators.

    Output format:

    Version 1: <v1>
    Version 2: <v2>
    v1 == v2: <true/false>
    v1 != v2: <true/false>
    v1 < v2: <true/false>
    v1 > v2: <true/false>
    v1 <= v2: <true/false>
    v1 >= v2: <true/false>

    Print true or false (lowercase) for each comparison result.

The key technique here is implementing the four derived operators (!=, >, <=, >=) using only == and <. This reduces code duplication and ensures all operators stay consistent if you ever change the comparison logic.

Convert input strings to integers using std::stoi(). Use std::to_string() for building the version string. Don't forget header guards in your header file.

Cheat sheet

Comparison operators (==, !=, <code><, , <=, >=) allow you to compare objects of custom classes. These operators return a bool and should be marked const since they don't modify either operand.

class Date {
    int year, month, day;
    
public:
    Date(int y, int m, int d) : year(y), month(m), day(d) {}
    
    bool operator==(const Date& other) const {
        return year == other.year && 
               month == other.month && 
               day == other.day;
    }
    
    bool operator<(const Date& other) const {
        if (year != other.year) return year < other.year;
        if (month != other.month) return month < other.month;
        return day < other.day;
    }
};

Implement other comparison operators in terms of == and <code>< to reduce code duplication and ensure consistency:

bool operator!=(const Date& other) const {
    return !(*this == other);
}

bool operator>(const Date& other) const {
    return other < *this;
}

bool operator<=(const Date& other) const {
    return !(other < *this);
}

bool operator>=(const Date& other) const {
    return !(*this < other);
}

Try it yourself

#include <iostream>
#include <string>
#include "Version.h"

using namespace std;

int main() {
    // Read six integers for two versions
    string line;
    
    getline(cin, line);
    int major1 = stoi(line);
    
    getline(cin, line);
    int minor1 = stoi(line);
    
    getline(cin, line);
    int patch1 = stoi(line);
    
    getline(cin, line);
    int major2 = stoi(line);
    
    getline(cin, line);
    int minor2 = stoi(line);
    
    getline(cin, line);
    int patch2 = stoi(line);

    // TODO: Create two Version objects
    
    // TODO: Output Version 1 and Version 2 using toString()
    
    // TODO: Compare using all six operators and output results
    // Use "true" or "false" (lowercase) for each comparison
    // Format: "v1 == v2: true" or "v1 == v2: false"
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming