Skip to content
Blog

Overload the + and ! Operators in C++

By Published A short read

     In this article we are looking how we can overload a operator in c++. Operator overloading might sound tempting to you. But it's very simple to understand and use. You are just changing the default behavior of the operators.

    You are good with the + operator which adds two numbers. Suppose you have create a class called myFile which create a new file with some content in it when initialized. And you have created two instances of the myFile class with different content. 

    Now you want to create a new instances of the class where we can do something like this 

f3 = f1 + f2. If try this without operator overloading it will throw error. But here we will overload the + operator to add two files using the + operator. You will understand once you look add the code itself.

C++ Operator Overloading

we will over the following operator

Operator Example Purpose
+ F3 = F1 + F2 Concatenate File 1 and File 2 into File 3
! !F3 Changes the case of alternate characters in file 3

code: 

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// class myFile
class myFile
{
private:
    // data members
    string fileName;
    string fileContent;

public:
    // contructor with parameters to create a file with name fileName
    // and content fileContent
    myFile(string name, string content)
    {
        fileName = name;
        fileContent = content;
        // create file with name fileName and content fileContent and open it in write mode
        ofstream myFile(fileName.c_str());
        myFile << fileContent;
        myFile.close();
    }

    // dispaly fuction to the file content on the screen
    void display()
    {
        cout << "This is the content of the file " << fileName << ": " << endl;
        cout << fileContent << endl;
    }
    // + operator overloading to concatenate file A and file B into file C
    myFile operator+(myFile &obj)
    {
        myFile temp(fileName, fileContent + obj.fileContent);
        return temp;
    }
    // ! operator overloading to change the case of alternate characters in file C
    myFile operator!()
    {
        string temp = "";
        for (int i = 0; i < fileContent.length(); i++)
        {
            if (i % 2 == 0)
            {
                temp += toupper(fileContent[i]);
            }
            else
            {
                temp += tolower(fileContent[i]);
            }
        }
        myFile tempFile(fileName, temp);
        return tempFile;
    }
};

int main()
{
    // create two files
    myFile f1("file1.txt", "The Quick Brown fox jumps over the lazy dog in file 1");
    myFile f2("file2.txt", "The dog was not lazy in file 2");
    // create a third file
    myFile f3("file3.txt", "");

    // concatenate the files
    f3 = f1 + f2;
    // change the case of alternate characters in file 3
    f3 = !f3;

    // display the content of the files
    f1.display();
    f2.display();
    f3.display();

    system("pause");
    return 0;
}

output:

C++ Program To Overload  + and ! Operator | Operator Overloading - letsbug
output