Find age using friend function in class

OOP gives data hiding, i.e., a nonmember function cannot access an object’s private or protected data.
Have you heard about Friend function?
We use this function In object-oriented programming. A friend function, that is a “friend” of a given class, is a function that is given the same access as methods to private and protected data.
It works same as methods of class specially on the Encapsulation.

For accessing the data, the declaration of a friend function should be made inside the body of the class (can be anywhere inside class either in private or public section) starting with keyword friend.

Let’s Take an example

#include
using namespace std;

class C1; // Forward declaration of class c1 in order for example to compile.
class B1{
private:
int a;
public:
B1(): a(0) {}
void show(B1& x, C1& y);
friend void show(B1& x, C1& y); // declaration of global friend
};

class C1 {
private:
int b;
public:
C1(): b(6) {}
friend void show(B1& x, C1& y); // declaration of global friend
friend void B1::show(B1& x, C1& y); // declaration of friend from other class
};

// Definition of a member function of class B1; this member is a friend of class C1
void Bar::show(B1& x, C1& y) {
cout << "Show via function member of B1" << endl;
cout << "B1::a = " << x.a << endl;
cout << "C1::b = " << y.b << endl;
}

// Friend for Bar and Foo, definition of global function
void show(B1& x, C1& y) {
cout << "Show via global function" << endl;
cout << "B1::a = " << x.a << endl;
cout << "C1::b = " << y.b << endl;
}

int main() {
B1 a; // class B1 and object a of class B1
C1 b;

show(a,b); // method function
a.show(a,b); // friend function call
}

Let’s solve a question

Find age using friend function in class

In this question if date month and year of birth is given we just have to find the Age of that person.
But we have to use class and friend function for that specific task.

Solution

#include<cstdio>
#include<iostream>

using namespace std;
class student; // class synatx prototype

class date
{
	int year;
	int dd;
	int mm;
public:

	date(int a,int b,int c)
	{
		dd=a;
		mm=b;
		year=c;

	}
//date class
friend student; // friend function
};
class student
{
	date DOB;
	char name[50];
public:
student(char *str):DOB(3,10,1996)
{
	 strcpy(name,str);
	 cout<<"Name="<<name<<endl;
	 cout<<"Date of Birth"<<endl<<DOB.dd<<"/"<<DOB.mm<<"/"<<DOB.year<<endl;
}
int duration()
	{
	int cd,cm,cy;
	int d1,d2,d3;
	cout<<"\nEnter the current date ,month and year\n";
	cin>>cd>>cm>>cy;
		if(DOB.dd<cd)
		d1=cd-DOB.dd;
		else
		d1=DOB.dd=cd;
		if(DOB.mm<cm)
		d2=cm-DOB.mm;
		else
		d2=DOB.mm-cm;
		d3=cy-DOB.year;
		 cout<<"Age= "<<d1<<"/"<<d2<<"/"<<d3;
		 return 0;
	}
};

int main()
{
	student s("Mariam");

	s.duration();
	//getch();
}

II Method

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class student;

class date
{	

	int year;
	int dd;
	int mm;
public:

	date(int y)
	{
		dd=03;
		mm=10;
		year=y;
		
	}
friend student;
};
class student
{
	date DOB;
	char name[50];
public:
student():DOB(1996)
{
	 strcpy(name,"Mariam");
	 cout<<"Name="<<name<<endl;
	 cout<<"Date of Birth"<<endl<<DOB.dd<<"/"<<DOB.mm<<"/"<<DOB.year<<endl;
}
int duration()
	{
		int d;
		d=2014-DOB.year;
		 cout<<"Age=="<<d;
	}
};

int main()
{
	student s;
	
	s.duration();
	getch();
}
	

Find age using friend function in class

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.