[Ask] cin >> class object?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 2
Reputation: fhast is an unknown quantity at this point 
Solved Threads: 0
fhast fhast is offline Offline
Newbie Poster

[Ask] cin >> class object?

 
0
  #1
Oct 26th, 2009
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.
Visit my blog..

@dBusiness - Blog
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #2
Oct 26th, 2009
Use a friend function to overload the >> operator
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,219
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 150
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #3
Oct 26th, 2009
At this stage(I am assuming), it may be easier( for you) to create a function, like so :
  1. class Timer
  2. {
  3. int h,m,s;
  4. public :
  5. void getTime() { cin >> h >> m >> s; }
  6. }
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: fhast is an unknown quantity at this point 
Solved Threads: 0
fhast fhast is offline Offline
Newbie Poster
 
0
  #4
Oct 27th, 2009
Originally Posted by Ancient Dragon View Post
Use a friend function to overload the >> operator
  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.
Visit my blog..

@dBusiness - Blog
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC