ObjectOrientedProgrammingusingC++Lab
CSE Section:A
Regd.No.:17R21A0510                 Week:03         Date:13/08/2018
Programnumber:01
TitleofExperiment:perform addition of 2 no in c++ using friend function on 2 classses
Sourcecode:
#include<iostream>
using namespace std;
class B;
class A
{
    int a;
    public:
    void getdata1()
    {
       cout<<"enter a";
       cin>>a;
    }
    friend void sum(A ob1,B ob2);
};
class B
{
    int b;
    public:
    void getdata2()
    {
        cout<<"enter b";
        cin>>b;
    }
    friend void sum(A ob1,B ob2);
};
void sum(A ob1,B ob2)
{
    cout<<ob1.a+ob2.b;
}
int main()
{
    A ob1;
    B ob2;
    ob1.getdata1();
    ob2.getdata2();
   // cout<<"enter values";
    //cin>>ob1.a>>ob2.b;
    sum(ob1,ob2);
    return 0;
}
Output
Programnumber:02
TitleofExperiment:write a c++ program to read and print elements of array using
pointers
Sourcecode:
#include<iostream>
using namespace std;
int main()
{
   int a[20],i,n;
   cout<<"enter size";
   cin>>n;
   for(i=0;i<n;i++)
      cin>>*(a+i);
   cout<<"enter the ele";
   for(i=0;i<n;i++)
      cout<<*(a+i);
}
Output
Programnumber:03
TitleofExperiment:write a c++ program to read and print elements of array using
pointers and print sum of the lements
Sourcecode:
#include<iostream>
using namespace std;
int main()
{
   int a[20],i,n,s=0;
   cout<<"enter size";
   cin>>n;
   for(i=0;i<n;i++)
   {
      cin>>*(a+i);
   }
   for(i=0;i<n;i++)
   {
      s=s+*(a+i);
   }
   cout<<"sum="<<s;
}
Output
Programnumber:04
TitleofExperiment:write a c++ program to ferform factorial of a number by using friend
function
Sourcecode:
#include<iostream>
using namespace std;
class A
{
   int a;
   public:
   void getdata()
   {
      cout<<"enter a";
      cin>>a;
   }
   friend void fact(A ob);
};
void fact(A ob)
{
   int i,fact=1;
   for(i=1;i<=ob.a;i++)
   {
       fact=fact*i;
   }
   cout<<"fact="<<fact;
}
int main()
{
   A ob;
   ob.getdata();
   fact(ob);
}
Output