Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
// Program to illustrate friend function example

#include<iostream.h>
class sample
{
     private:
          int sub1;
          int sub2;
          int sub3;
     public:
          friend int totalmarks(sample temp);
          void getmarks();
};

          void sample::getmarks()
          {

               cout<<"\n Enter three  marks";
               cin>>sub1>>sub2>>sub3;
          }


          int totalmarks(sample temp)
          {
               int total;
               total=temp.sub1+temp.sub2+temp.sub3;
               return total;
          }


int main()
{
     sample obj;
     obj.getmarks();
     cout<<"\n Total marks :"<<totalmarks(obj);
     return 0;
}


Test data

Enter three marks
30 35 40

Output
Total marks : 105