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

28 April 2024 Today I am going to explain verse 31 - 38 chapter two for you all. There is no opportunity better than a righteous war (सत्य औ...