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;
}

 

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *