Program to illustrate Friend Functions


#include <iostream.h>
#include<conio.h>
class time
{
        int seconds;
    public :
        void set(int);
        friend void print_time(time);  // Friend function Declaration
};
void time::set(int n)
{
    seconds = n;
}
void print_time(time dt) // Friend function definition
{
    int h = dt.seconds / 3600,            // Friend function can access
    m = (dt.seconds % 3600) / 60;        // private data members
    cout << "Time : " << h << ":" << m << "\n";
}
void main(void)
{
    clrscr();
    time current_time;
    current_time.set(12000);
    print_time(current_time);
    getch();
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|