Pages

100 Contemporary Living Room Designs

Friday, August 17, 2012

ADDING 'TIME' OBJECTS AND DISPLAYING THE RESULT IN HH:MM:SS FORMAT USING MEMBER FUNCTION


Create a class called 'time' that has three integer data members for hours, minutes and seconds, define a member function to read the values, member function to add two time objects, member function to display time in HH:MM:SS format. Write a main function to create two time objects, add them and display the results in HH:MM:SS format.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class time
{
  int h,m,s;
  public:
  void read();
  void print();
  void add(time t2);
};
void time::read()
{
  cout<<"\nEnter hour,minutes and seconds\n";
  cin>>h>>m>>s;
}
void time::print()
{
  cout<<"\nTime is-> "<<setfill('0')<<setw(2)<<h;
  cout<<":"<<setfill('0')<<setw(2)<<m;
  cout<<":"<<setfill('0')<<setw(2)<<s<<endl;
}
void time::add(time t2)
{
  h=h+t2.h;
  m=m+t2.m;
  s=s+t2.s;
}
void main()
{
  clrscr();
  time t1,t2;
  t1.read();
  t1.print();
  t2.read();
  t2.print();
  t1.add(t2);  // member function call.
  cout<<"\nTime1+ Time2:\n";
  t1.print();
  getch();
}

output
                                                                                                                                                                                                           

No comments:

Post a Comment