| |

VerySource

 Forgot password?
 Register
Search
View: 786|Reply: 7

Operator >> overloading problem

[Copy link]

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-7 00:30:01
| Show all posts |Read mode
class Time {
friend ostream&operator << (ostream&output, Time&t)
{
cout << t.hour << ":" << t.minute << ":" << t.second;
return output;
}
friend istream&operator >> (istream&input, Time&t)
{
}
private:
int hour; int minute; int second;
};
Requires the input of an object of class Time in the format of hh-mm-ss. I don't know how to write that overloaded >> function. Please enlighten me
Reply

Use magic Report

0

Threads

14

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-3-28 00:15:01
| Show all posts
friend istream&operator >> (istream&input, Time&t)
{
    string temp;
    input >> temp;
    // Parse temp, substring, string-"int ...
}
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-3-28 13:30:01
| Show all posts
friend istream&operator >> (istream&input, Time&t)
{
    string temp;
    input >> temp;
    // Parse temp, substring, string-"int ...
}
====================================================
Can you write it completely, thanks a lot :)
Reply

Use magic Report

0

Threads

14

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-3-30 00:15:01
| Show all posts
#include <string>
#include <iostream>
#include <sstream>
using namespace std;

class Time
{
friend ostream&operator << (ostream&output, Time&t)
{
cout << t.hour << ":" << t.minute << ":" << t.second;
return output;
}
friend istream&operator >> (istream&input, Time&t)
{
string temp;
input >> temp;
istringstream iss;
iss.str (temp);
iss.str (temp.substr (0, 2));
iss >> t.hour;
iss.clear ();
iss.str (temp.substr (3, 2));
iss >> t.minute;
iss.clear ();
iss.str (temp.substr (6, 2));
iss >> t.second;
return input;
}
private:
int hour;
int minute;
int second;
};

int main (void)
{
    Time t;
    cin >> t;
    cout << t << endl;
    return 0;
}
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-2 18:30:02
| Show all posts
I understand
Is there any other way
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-4-8 10:45:01
| Show all posts
hh-mm-ss
simple way:

int hh, mm, ss;
string b;
cin >> hh >> b >> mm >> b >> ss;
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-13 16:00:01
| Show all posts
simple way:

int hh, mm, ss;
string b;
cin >> hh >> b >> mm >> b >> ss;
==================================================
?
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-13 17:00:02
| Show all posts
simple way:

int hh, mm, ss;
string b;
cin >> hh >> b >> mm >> b >> ss;
==========================================
?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list