Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ Program to implement friend class and friend functions :
#include<iostream>
#include<string>
using namespace std;
class second{
private:
string a,b;
public:
second(string x,string y);
friend class first;
};
class first{
public:
void firstname(second);
void secondname(second);
};
second::second(string x,string y){
a=x;
b=y;
}
void first::firstname(second s){
cout<<"\nYour first name is "<<s.a<<endl;
}
void first::secondname(second s){
cout<<"\nYour second name is "<<s.b<<endl;
}
int main()
{
string a,b;
cout<<"Enter your Name : ( First Name followed by second Name )\n";
cin>>a>>b;
second obj(a,b);
first temp;
temp.firstname(obj);
temp.secondname(obj);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment