C++ Program For Friend Function and Accessing Private Data
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Program for Friend Function
#include<iostream>
#include<string.h>
#define clrscr() system("cls")
using namespace std;
class Student
{
private :
int rno;
char name[50];
float per;
public:
Student(int rno,char name[],float per)
{
this->rno=rno;
strcpy(this->name,name);
this->per=per;
}
void display(); // member method
friend void show(Student S); // friend function
};
void Student::display()
{
cout<<"\nDisplay Method of class member method";
cout<<"\nRoll No is: "<<rno;
cout<<"\nName : "<<name;
cout<<"\nPercentange: "<<per;
}
void show(Student S)
{
cout<<"\nShow Method of class friend method";
cout<<"\nRoll No is: "<<S.rno;
cout<<"\nName : "<<S.name;
cout<<"\nPercentange: "<<S.per;
}
int main()
{
clrscr();
Student S1(101,"Vivek",90.88);
show(S1);
S1.display();
return 0;
}
Your opinion matters
Please write your valuable feedback about DataFlair on Google

