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

Sacred Thought

5 May 2024 Hari Om Verse 50-51, chapter two:  In this chapter two Shree krishna explains a simple way of living. Free from desires and void ...