Sunday, February 25, 2018

C++ : Friend Function and Friend Class example program

#include <iostream>
using namespace std;
class Date
{
int da; // 2 digit day
int mo; // 2 digit month
int yr; // 4 digit year
public:
Date(int d, int m, int y): da(d), mo(m), yr(y) { }
friend ostream& operator<<(ostream& os, const Date& dt); // Fill the return type
};
ostream& operator<<(ostream& os, const Date& dt) // Fill the return type
{
os << dt.mo<< "/" << dt.da<<"/"<< dt.yr<< endl; // Fill the implementation
return os;
}

int main()
{
    int day = 0, month = 0, year = 0;
    cin >> day;
    cin >> month;
    cin >> year;
    Date dt(day, month, year);
    cout << dt;
    return 0;
}

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows