0% found this document useful (0 votes)
15 views

Objectorientedprogrammingusingc++Lab Cse Section Regd - No Week Date Programnumber Titleofexperiment Sourcecode

This document contains the source code and outputs for 4 programs written in C++. The first program uses friend functions to add two numbers stored in different classes. The second program uses pointers to read and print array elements. The third program extends this to also print the sum of the array elements. The fourth program calculates the factorial of a number using a friend function.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Objectorientedprogrammingusingc++Lab Cse Section Regd - No Week Date Programnumber Titleofexperiment Sourcecode

This document contains the source code and outputs for 4 programs written in C++. The first program uses friend functions to add two numbers stored in different classes. The second program uses pointers to read and print array elements. The third program extends this to also print the sum of the array elements. The fourth program calculates the factorial of a number using a friend function.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like