Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:49:00+00:00 2026-06-05T18:49:00+00:00

What is the main purpose of overloading operators in C++? In the code below,

  • 0

What is the main purpose of overloading operators in C++?

In the code below, << and >> are overloaded; what is the advantage to doing so?

#include <iostream>
#include <string>

using namespace std;

class  book {
    string name,gvari;
    double cost;
    int year;
    public:
    book(){};

    book(string a, string b, double c, int d) { a=name;b=gvari;c=cost;d=year; }
    ~book() {}
    double setprice(double a) { return a=cost; }
    friend ostream& operator <<(ostream& , book&);
    void printbook(){
        cout<<"wignis saxeli "<<name<<endl;
        cout<<"wignis avtori "<<gvari<<endl;
        cout<<"girebuleba "<<cost<<endl;
        cout<<"weli "<<year<<endl;
    }
};

ostream& operator <<(ostream& out, book& a){
    out<<"wignis saxeli "<<a.name<<endl;
    out<<"wignis avtori "<<a.gvari<<endl;
    out<<"girebuleba "<<a.cost<<endl;
    out<<"weli "<<a.year<<endl;
    return out;
}

class library_card : public book {
    string nomeri;
    int raod;
    public:
    library_card(){};
    library_card( string a, int b){a=nomeri;b=raod;}
    ~library_card() {};
    void printcard(){
        cout<<"katalogis nomeri "<<nomeri<<endl;
        cout<<"gacemis raodenoba "<<raod<<endl;
    }
    friend ostream& operator <<(ostream& , library_card&);
};

ostream& operator <<(ostream& out, library_card& b) {
    out<<"katalogis nomeri "<<b.nomeri<<endl;
    out<<"gacemis raodenoba "<<b.raod<<endl;
    return out;
}


int main() {
    book A("robizon kruno","giorgi",15,1992);
    library_card B("910CPP",123);
    A.printbook();
    B.printbook();
    A.setprice(15);
    B.printbook();

    system("pause");
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:49:02+00:00Added an answer on June 5, 2026 at 6:49 pm

    It doesn’t ever have to be used; it’s just a convenience, a way of letting user-defined types act more like built-in types.

    For example, if you overload operator<<, you can stream books the same way as integers and strings:

    cout << "Book #" << n << " is " << books[n] << endl;
    

    If you don’t, you’d have to write the same thing like this:

    cout << "Book #" << n << " is ";
    books[n].printbook();
    cout << endl;
    

    Similarly, if you create a Fraction class and give it an operator+, you can use fractions the same way you use integers, and so on.

    It’s sometimes a tough design choice whether your class should act like a native type in one way or another (for example, does string’s operator+ make sense?), but the point is that C++ gives you the choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What exactly the main purpose of using RequestDispatcher , for example when it's executed
What is the main purpose of using CROSS APPLY ? I have read (vaguely,
I have created a namespace extension that is rooted under Desktop. The main purpose
What is the main purpose of the following JavaScript code? <script> var QunarUtil=new function(){var
What is main purpose of using Clone() in C#? What is the benefit of
I installed GitExtensions for the main purpose of using GitHub with my visual studio
I wanna add server controls by using javascript. The main purpose of why I
what is the main purpose of a destructor? could you give any examples of
what is the purpose of automation testing? According to me the main purpose is
I've created my own split view controller for the iPad. My main purpose in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.