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

     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


Comments

Categories

Big Data Analytics Binary Search Binary Search Tree Binary To Decimal binary tree Breadth First Search Bubble sort C Programming c++ Chemical Reaction and equation class 10 class 10th Class 9 Climate Complex Numbers computer network counting sort CSS Cyber Offenses Cyber Security Cyberstalking Data Science Data Structures Decimal To Binary Development diamond pattern Digital Marketing dust of snow Economics Economics Lesson 4 Email Validation English fire and ice Food Security in India Footprints Without feet Forest And Wildlife Resources game Geography Geography lesson 6 glassmorphism Glossary Graph HackerRank Solution hindi HTML image previewer India-Size And Location Insertion Sort Internet Network Status Interview Questions Introduction to cyber crime and cyber security IT javascript tricks json to CSV converter lesson 2 lesson 1 lesson 2 Lesson 3 Lesson 6 lesson 7 Life lines of National Economy life processes Linear Search Linked List lowest common ancestor Machine Learning MCQs median in array Merge sort min and max of two numbers Moment Money and Credit My Childhood Natural Vegetation and Wildlife NCERT Network connectivity devices Network Models Network Security No Men Are foreign Node.js operator overloading P5.js PHP Physical features of India Population Prime Numbers python Quick sort R language Rain on the roof Regular Expression Resources and development reversing array saakhi science Searching Algorithm Selection sort Social Media Marketing social science Software Engineering Software Testing Sorting Algorithm Stacks staircase pattern System Concepts Text Recognition The last Leaf time converter Time Passed From A Date Todo List App Tree Trending Technologies Understanding Economic Development username and password video player Visualization water resources Wired And Wireless LAN साखी
Show more

Popular Posts

Big Data MCQs(multiple choice questions) with answers - letsbug

Digital Marketing MCQ(Multiple Choice Questions) with Answers | part 1 | letsbug

Software Engineering MCQs questions with answers - letsbug