I have created a Time class as suggested by u..Thank you so so much for the post.Cant tell u how much it helped me
This is my file
#ifndef TIME_H
#define TIME_H
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>
class Time
{
private:
int hours,minutes;
char *time;
public:
Time()
{
int hours=minutes=0;
}
Time(char *tim)
{
char *min=strpbrk(tim,":"

;
*min++='\0';
minutes=atoi(min);
hours=atoi(tim);
cout<<" the hours passed is"<<hours<<" and minutes passed is "<<minutes<<endl;
}
int operator- (Time t1)
{
if(hours<t1.hours||((hours==t1.hours)&&(minutes<t1.minutes)))
{
cerr<<"the calling time time is lesser than called time";
return 0;
}
if (minutes<t1.minutes)
{
minutes+=60;
}
// cout<<"value of called minutes is"<<t1.minutes<<endl;
int mindiff=minutes-t1.minutes;
//cout<<"calling hour="<<hours<<endl;
//cout<<"called hour="<<t1.hours<<endl;
int hourdiff;
if (minutes<t1.minutes)
hourdiff=hours-t1.hours-1;
else
hourdiff=hours-t1.hours;
//cout<<"difference in hours="<<hourdiff<<endl;
int diffinmin=((hourdiff*60)+mindiff);
return diffinmin;
}
Time operator+(Time t2)
{
Time sum;
sum.minutes=minutes+t2.minutes;
if(sum.minutes>60)
{
sum.minutes-=60;
sum.hours=sum.hours+1;
}
sum.hours=sum.hours+hours+t2.hours;
if (sum.hours>24)
{
sum.hours=sum.hours-24;
}
return sum;
}
friend ostream& operator<< (ostream&,Time& t3)
{
cout<<"The time is "<<setfill('0')<<setw(2)<<t3.hours<<":"<<setfill('0')<<setw(2)<<t3.minutes<<endl;
}
};
#endif
I am having a city that has time as one of its private data. So can I have a constructor that takes a Time datatype? If yes, later, how do I access the private data members of Time? Can I have something like the following?
create object of
Time time inside City class
City *c1;
Time t=c1->time ?
ANd if this is the way, how do I retrieve the data in time? Can I use a double arrow?c1->time->hours;
Sorry if I am giving u a lot of data to think over.
Even if u r not able to find time to answer my maybe stupid question, I am very grateful to u for ur help to get me started in thinking in OO terms.
Rgds,
Sudha Visit
to know more about me