944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1247
  • C++ RSS
Oct 26th, 2009
0

[Ask] cin >> class object?

Expand Post »
hi, i want to ask..

is it can to get an input from the user by only call the class object?

here's the code :

#include <iostream>

class Time{
private:
int time;
int minute;
int sec;
...
};

void main(){
Time t;
cin >> t;
cout << "The time is : " << t << endl;
}

Is that code wrong or true, but need some additional parts?

please give me a detail and simple explanation because i'm still a newbie. Thank You.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fhast is offline Offline
4 posts
since Oct 2009
Oct 26th, 2009
-7
Re: [Ask] cin >> class object?
Use a friend function to overload the >> operator
C++ Syntax (Toggle Plain Text)
  1. class Time
  2. {
  3. private:
  4. int hour, minute, second;
  5. public:
  6. Time() {hour = mijnute = second = 0;}
  7. friend istream& operator >> (istream& in, Time& t);
  8. };
  9.  
  10. // Put this in the *.cpp file, not the *.h file
  11. istream& operator >> (istream& in, Time& t)
  12. {
  13. cout << "Enter time";
  14. cin >> t.hour >> t.minute >> t.second;
  15. return in;
  16. };
Last edited by Ancient Dragon; Oct 26th, 2009 at 12:48 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 26th, 2009
0
Re: [Ask] cin >> class object?
At this stage(I am assuming), it may be easier( for you) to create a function, like so :
C++ Syntax (Toggle Plain Text)
  1. class Timer
  2. {
  3. int h,m,s;
  4. public :
  5. void getTime() { cin >> h >> m >> s; }
  6. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Oct 27th, 2009
0
Re: [Ask] cin >> class object?
Use a friend function to overload the >> operator
C++ Syntax (Toggle Plain Text)
  1. class Time
  2. {
  3. private:
  4. int hour, minute, second;
  5. public:
  6. Time() {hour = mijnute = second = 0;}
  7. friend istream& operator >> (istream& in, Time& t);
  8. };
  9.  
  10. // Put this in the *.cpp file, not the *.h file
  11. istream& operator >> (istream& in, Time& t)
  12. {
  13. cout << "Enter time";
  14. cin >> t.hour >> t.minute >> t.second;
  15. return in;
  16. };
Thank You Ancient Dragon, i can use it. now the problem is solved.

@firstperson

yeah, i know it is easier that way, but i see this code (cin >> object class) and i want to know if it is possible to do it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fhast is offline Offline
4 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: relocation error? allocate exception?
Next Thread in C++ Forum Timeline: Got a question about GCC and C++ and output file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC