0% found this document useful (0 votes)
21 views

To A C++ Program Using Friend Function

The document describes a C++ program that uses a friend function to add the time stored in two objects of a class and display the total. The program takes input of hours, minutes, and seconds for two time objects, uses a friend function to add the values of the objects, and displays the total time.

Uploaded by

9976726158
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

To A C++ Program Using Friend Function

The document describes a C++ program that uses a friend function to add the time stored in two objects of a class and display the total. The program takes input of hours, minutes, and seconds for two time objects, uses a friend function to add the values of the objects, and displays the total time.

Uploaded by

9976726158
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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