C++ Program For Friend Function and Accessing Private Data
Free C++ course with real-time projects 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; }
Did we exceed your expectations?
If Yes, share your valuable feedback on Google