This is a sample program using Friend Classes. Using Friend Classes, here we add two different time together & display the final time.
#include <iostream.h>
#include <conio.h>
class time
{
private:
int h, m, s;
public:
void read()
{
cout<<"Enter Time In Hours:Minutes:Seconds \n";
cin>>h>>m>>s;
}
friend void add(time t1, time t2, time t3)
{
t3.h=t1.h+t2.h;
t3.m=t1.m+t2.m;
t3.s=t1.s+t2.s;
if (t3.s>=60)
{
t3.m+=t3.s/60;
t3.s=t3.s%60;
}
if(t3.m>=60)
{
t3.h+=t3.m/60;
t3.m=t3.m%60;
}
cout<<"\n New Time:"<<t3.h<<" Hours "<<t3.m<<" Minutes "<<t3.s<< " Seconds ";
}
};
void main()
{
clrscr();
time t1,t2,t3;
t1.read();
t2.read();
add(t1,t2,t3);
getch();
}
OUTPUT:
Enter Time In Hours:Minutes:Seconds
9
20
10
Enter Time In Hours:Minutes:Seconds
1
20
5
New Time: 10 Hours 40 Minutes 15 Seconds
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com