Pages

100 Contemporary Living Room Designs

Friday, August 17, 2012

OPERATOR OVERLOADING - ADDING TWO 'TIME' OBJECTS


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 operator function to add time, member function to display time in HH:MM:SS format. Write a main function to create two time objects, use operator function to 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();
  time operator+(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;
}
time time::operator+(time t2)
{
  time t;
  t.h=h+t2.h;
  t.m=m+t2.m;
  t.s=s+t2.s;
  return t;
}
void main()
{
  clrscr();
  time t1,t2,t3;
  t1.read();
  t1.print();
  t2.read();
  t2.print();
  t3=t1+t2;
  cout<<"\nTime1+ Time2:\n";
  t3.print();
  getch();
}

output




1 comment:

  1. https://d2498fijg8ztfy3etotznbpl9t.hop.clickbank.net/?tid=MATBIN

    ReplyDelete