| | |
[Ask] cin >> class object?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
-7
#2 Oct 26th, 2009
Use a friend function to overload the >> operator
C++ Syntax (Toggle Plain Text)
class Time { private: int hour, minute, second; public: Time() {hour = mijnute = second = 0;} friend istream& operator >> (istream& in, Time& t); }; // Put this in the *.cpp file, not the *.h file istream& operator >> (istream& in, Time& t) { cout << "Enter time"; cin >> t.hour >> t.minute >> t.second; return in; };
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.
0
#3 Oct 26th, 2009
At this stage(I am assuming), it may be easier( for you) to create a function, like so :
C++ Syntax (Toggle Plain Text)
class Timer { int h,m,s; public : void getTime() { cin >> h >> m >> s; } }
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
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#4 Oct 27th, 2009
•
•
•
•
Use a friend function to overload the >> operator
C++ Syntax (Toggle Plain Text)
class Time { private: int hour, minute, second; public: Time() {hour = mijnute = second = 0;} friend istream& operator >> (istream& in, Time& t); }; // Put this in the *.cpp file, not the *.h file istream& operator >> (istream& in, Time& t) { cout << "Enter time"; cin >> t.hour >> t.minute >> t.second; return in; };
@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.
![]() |
Similar Threads
- Can you create a class object based on user input? (C++)
- Why the parent of a class is - object - ? (Python)
- Write class object in MySql database (Java)
- sort a class object (Python)
- why is casting from a base class object to derived class object is unsafe?please expl (C++)
- Problem declaring class object array (C++)
- JSON to class object (ASP.NET)
- Delete class object in list (Python)
- Problem Passing a Class Object to a Queue (C++)
Other Threads in the C++ Forum
- Previous Thread: relocation error? allocate exception?
- Next Thread: Got a question about GCC and C++ and output file
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






