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 8837131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:38:56+00:00 2026-06-14T09:38:56+00:00

// The original code is from link: http://hoeven.blogbus.com/logs/37324287.html #include<iostream> #include<vector> #include <stdlib.h> using namespace

  • 0
// The original code is from link: http://hoeven.blogbus.com/logs/37324287.html
#include<iostream>
#include<vector>
#include <stdlib.h>

using namespace std;

class test{
public:
     int v;
   /*构造函数*/
     test():v(0){}
     test(const int &a):v(a){}
     test(const test &t1):v(t1.v){}

   /*以下重载小于号 < */
     //比较两个对象的大小
     bool operator<(const test &t1) const{
         return (v < t1.v);
     }
     //比较对象和int的大小
     bool operator<(const int &t1) const{
         return (v < t1);
     }
     //友元函数,比较int和对象的大小
     friend inline bool operator<(const int &a, const test & t1){
         return (a < t1.v);
     }

   /*以下重载赋值号 = */
     //对象间赋值
     test & operator=(const test &t1){
         v = t1.v;
         return *this;
     }
     //int赋值给对象
     test & operator=(const int &t1){
         v = t1;
         return *this;
     }

   /*以下重载加号 + */
     //对象加上 int
     test operator+(const int & a){
         test t1;
         t1.v = v + a;
         return t1;
     }
     //对象加对象
     test operator+(test &t1){
         test t2;
         t2.v = v + t1.v;
         return t2;
     }

   /*以下重载加等号 += */  
     //对象加上对象
     test &operator+=(const test &t1){
         v += t1.v;
         return *this;
     }  
     //对象加上int
     test &operator+=(const int &a){
         v += a;
         return *this;
     }

   /*以下重载双等号 == */  
     //对象==对象
     bool operator==(const test &t1)const{
         return (v == t1.v);
     }  
     //对象==int
     bool operator==(const int &t1)const{
         return (v == t1);
     }  

   /*以下重载 输入>> 输出<< */
     /*友元函数,输出对象*/
     friend inline ostream & operator << (ostream & os, test &t1){
         cout << "class t(" << t1.v << ")" << endl;
         return os;
     }
     /*友元函数,输入对象*/
     friend inline istream & operator >> (istream & is, test &t1){
         cin >> t1.v;
         return is;
     }
};

int main(){
     test t0, t1(3);  // t0 has no initial value, so use default value 0
     test t2(t1);
     cout << t0 << t1 << t2;
     cin >> t1;
     t2 = t1;
     t2 += t1;
     t1 += 10;
     cout << t2;
     if(t1 < t2) cout << "t1 < t2";
     else if(t1 == t2) cout << "t1 = t2";
     else /* t1 > t2*/ cout << "t1 > t2";
     cout <<endl;
     system("echo Tom");
     return 0;
}

/*
 $ ./a.out 
 class t(0)
 class t(3)
 class t(3)
 45
 class t(90)
 t1 < t2
 Tom
 */

The complete code is above. But I don’t understand why “ostream & os” (see below) must be there in the bracket? If I remove “ostream & os”, a lot of errors were given.

 friend inline ostream & operator << (ostream & os, test &t1){
     cout << "class t(" << t1.v << ")" << endl;
     return os;
 }
  • 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-14T09:38:57+00:00Added an answer on June 14, 2026 at 9:38 am

    You have 2 ways of overloading operator<< :

    1. unary (this->operator<<(other_object)),
    2. binary (operator<<(ostream& os, other_object).

    Only one of them is possible (second), first is impossible because to implement it you have to overload function of ostream class operator<< which cant be done.
    When you write statement like cout << "some text"; binary operator is invoked, which gets as arguments: object of class ostream and object of another class

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

Sidebar

Related Questions

with the following code (a boiled-down version of my original code) #include <iostream> #include
The code from original web page <html> <div class=clear></div> <div class=slider> <ul> <li> <a
I am trying the RSS Feed code from this website (http://javamix.wordpress.com/category/programs/rss-feed/) It worked fine.
I want to generate Javascript code from an existing Java project ( original question
I have an Android project I've inherited from another developer. The original code was
from this code will show in original color how can i change it to
I have built an open-source application from the source code. Unfortunately, the original executable
I have some pretty basic C++ code: #include <iostream> #include <string.h> #include <msclr\/marshal_cppstd.h> #using
I've successfully implemented a bit of code that strips all HTML from a pasted
This is ridiculous. I am trying to download some code from scala trac: http://lampsvn.epfl.ch/trac/scala/browser/scala-tool-support/trunk/src/vim

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.