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.

Use a friend function to overload the >> operator

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;
};

Edited 7 Years Ago by Ancient Dragon: n/a

At this stage(I am assuming), it may be easier( for you) to create a function, like so :

class Timer
{
    int h,m,s;
    public :
     void getTime() { cin >> h >> m >> s; }
}

Use a friend function to overload the >> operator

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;
};

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.

This question has already been answered. Start a new discussion instead.