Hi guys, i want to overload input stream for class Date_Time

class Date_Time{		//Containment class
	Date d;
	Time t;
public:
	Date_Time();
	Date_Time(short date, Date::EMonth month, CYear year, short hour, short minutes, short seconds, bool meri);
	Date_Time(short date, Date::EMonth, CYear year);
	Date_Time(short hour, short minutes, short seconds, bool meri);
	Date_Time(Date dd, Time tt);
	~Date_Time();
	short Day() const;
	short Month() const;
	CYear Year() const;
	short Hours() const;
	short Minutes() const;
	short Seconds() const;
	bool Meridian() const;
	Date DObj() const;
	Time TObj() const;
	char *format(char *fmt);
	
};

i dont kmoe how to do it........ pls tell me

Here is the prototype :

friend istream& operator >>(istream& stream, Data_Time& date){
 /* code to read here */
 /* return ... */
};

Make that your member function.

Do you mean that you want to choose which input stream you want at each usage as a function input or a saved member variable? if so then the cin/cout are of type istream, ostream respectively. An open file in read or write mode would also be of types istream, ostream. USing a function taking either as its input you could save the stream the class is currently using in a member var and write to it something like the following.

ifstream myFile; //make a file handle
myFile.open("someFile"); //open the file

Date_Time someDate; //create instance of your class
someDate.setIStream(myFile); //tell it where to take its input from 
someDate.setOStream(cout); //tell it where to output to

// somewhere in a class function assuming the saved ostream is called m_output
m_output<<"Todays Date is: "<<...;

Of course this may be totally not what you want in which case can you explain more clearly what it is you are trying to do?

Hi Kanoisa, i want to overload istream to get inputs for my date & time class private members.

class Date{
short d, m;
CYear y;

public:
enum EMonth{JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
enum EDay{SUN = 0, MON, TUE, WEN, THU, FRI, SAT};
private:
EMonth EnumM;
public:
Date();
Date(short date, EMonth month, CYear year);
~Date();
short Day() const;
short Month() const;
CYear Year() const;

I think here what you want then is what firstPerson posted. What the prototype he provides will do is allow you to do something like the code snippet below. I may be misusing what firstPerson said for this prototype, if so please feel free to correct me as i havent done allot of operator overloading, and my aplogies :(.

/* inside your class decleration */
  friend istream& operator >>(istream& stream, Data_Time& date);

/* in your .cpp */ 
  friend istream& CLASSNAME::operator >>(istream& stream, Data_Time& date)
  {
     if(stream) //check the last read was ok 
     {
         //code that is really just consecutive  reads from the istream

         stream.clear();
     }/*if used as if the stream is bad before we started we dont want to mess with it
        incase another function will be responsible for recovering the data. if this is
        not neccessary in your code then feel free to remove it*/
 
     return stream;
  }

/* somewhere in your code */
cin>>myClassInstance; //read data from cin into the class data mambers

This is what you would like to do?

a simple program for overloading input and output operators
in this program we use overloading of istream(>>) and ostream(<<) operators to change their behaviour.
#include<iostream.h>
class sowmya
{
int a,b;
public:
friend istream &operator >>(istream &x,sowmya &s)/*we use friend function because it gives direct access to private members a and b for istream and ostream*/
{
x>>s.a>>s.b;
return x;
}
friend ostream &operator <<(ostream &y,sowmya &s)
{
y<<s.a<<"\t"<<s.b;
return y;
}
main()
{
sowmya obj;
cin>>obj; //invoking input operator
cout<<"\ndisplay:"
cout<<obj;//invoking output operator
return 0;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.