Powered by Blogger.

Tuesday, February 5, 2013

friend function

/* Q.N 1. Write a program using friend function to add numerical values of three objects of different classes */
#include<iostream.h>
#include<conio.h>

class class_2;
class class_3;
class class_1
{
      private:
              int x;
              public:
                     void getdata();
                     friend void add(class_1,class_2,class_3);
                     };
                   
      void class_1::getdata()
      {
           cout<<"\nEnter A Value For X : ";
           cin>>x;
           }
         
class class_2
{
      private:
              int y;
              public:
                     void getdata();
                     friend void add(class_1,class_2,class_3);
                     };
                   
      void class_2::getdata()
      {
           cout<<"\nEnter A Value For Y : ";
           cin>>y;
           }
         
class class_3
{
      private:
              int z;
              public:
                     void getdata();
                     friend void add(class_1,class_2,class_3);
                     };
                   
      void class_3::getdata()
      {
           cout<<"\nEnter A Value For Z : ";
           cin>>z;
           }
         
void add(class_1 a,class_2 b,class_3 c)
{
     int sum;
     sum=(a.x+b.y+c.z);
     cout<<"\n**********************************\n";
     cout<<"\nThe Sum Of Given Three Integer : "<<sum<<endl;
     }
   
int main()
{
    class_1 p;
    class_2 q;
    class_3 r;
  
    p.getdata();
    q.getdata();
    r.getdata();
    add(p,q,r);
    getch();
    return 0;
}
   


0 comments:

Post a Comment