You are on page 1of 4

AIM:

To a c++ program using friend function.

ALGORITHM:
STEP1: Start the program. STEP2: Then declare all variables. STEP3: Get input from user such that hour , min , sec. STEP4: Calculate all things using friend function. STEP5: And display the result. STEP6: Stop the program.

#include<iostream.h> #include<conio.h> class sample { private: int hour, min, sec; public: void getdata() { cout<<endl<<"Enter the hours, mins and sec"; cin>>hour>>min>>sec; } void display() { cout<<endl<<All total is: cout<<endl<<hour<<" : "; cout<<min<<" : "; cout<<sec; } Friend sample operator +(sample m, sample n) { sample temp; temp.hour=m.hour+n.hour; temp.min=m.min+n.min; temp.sec=m.sec+n.sec; while(temp.sec>=60) { temp.min++;

temp.sec=temp.sec-60; while(temp.min>=60) { temp.hour++; temp.min=temp.min-60; } } return(temp); } }; void main() { clrscr(); sample s1,s2,s3; s1.getdata(); s2.getdata(); s3=s1+s2; s3.display(); getch(); }

Output:

Enter the hour, min, sec: 1 30 30 Enter the hour, min, sec: 1 30 30 All total is: 3: 1: 0

You might also like