Program to illustrate Friend Classes


#include <iostream.h>
#include<conio.h>
class time
{
        int seconds;
        friend class date;    // friend class declared
    public :
        void set(int);
};
void time::set(int n)
{
    seconds = n;
}
class date
{
        int day, month, year;
    public :
        void set(int, int, int);
        void print_date();
        void print_time(time);
};
void date::set(int d, int m, int y)
{
    day = d; month = m; year = y;
}
void date::print_date()
{
    cout << "Date : " << day << "/"
                      << month << "/"
                      << year << "\n";
}
void date::print_time(time t)
{
    int h = t.seconds / 3600,
        m = (t.seconds % 3600) / 60;
    cout << "Time : " << h << ":" << m << "\n";
}
void main(void)
{
    clrscr();
    date todays_date;
    time current_time;
    todays_date.set(16,2,2002);
    current_time.set(12000);
    todays_date.print_date();
    todays_date.print_time(current_time);
    getch();
}

0 comments:

Post a Comment

 
 
 
 


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