C++ Programming
Slip24
int sum( int, int) – returns the addition of two integer arguments.
float sum(flaot, float, float) – returns the addition of three float arguments.
int sum( int [ ] ,int) – returns the sum of all elements in an array of size ‘ n’.
[Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
class sumdata
{
int a,b,s[20],n,temp,i,j;
float x,y,z;
public:
void accept(int p,int q,float r,float s,float t)
{
a=p;
b=q;
x=r;
y=s;
z=t;
}
int sum(int a,int b);
float sum(float x,float y,float z);
int sum(int s[],int n);
};
int sumdata:: sum(int a,int b)
{
return (a+b);
}
float sumdata:: sum(float x,float y,float z)
{
return (x+y+z);
}
int sumdata:: sum(int s[],int n)
{
int am=0;
for(i=0;i<n;i++)
{
am=am+s[i];
}
return am;
}
void main()
{
clrscr();
sumdata s1;
int p,q,ans;
int i,n,e[20],d1;
float r,s,t,ans1;
cout<<"Enter the value:";
cin>>p>>q;
cout<<"Enter the value:";
cin>>r>>s>>t;
s1.accept(p,q,r,s,t);
ans=s1.sum(p,q);
ans1=s1.sum(r,s,t);
cout<<"Enter how many element in array:";
cin>>n;
cout<<"Enter array element:";
for(i=0;i<n;i++)
{
cin>>e[i];
}
d1=s1.sum(e,n);
cout<<"\nAddition of two integer is:"<<ans;
cout<<"\nAddition of three Float is:"<<ans1;
cout<<"\nAddition of Array Element is:"<<d1;
getch();
}
Output
Enter the value: 5
6
Enter the value:4.5
5.6
6.3
Enter how many element in array:3
Enter array element:1
2
3
Addition of two integer is:11
Addition of three Float is:16.4
Addition of Array Element is:6
Q.2) Write a C++ program to create two classes Class1 and Class2. Each class contains one
float data member. Write following functions:
i. To accept float numbers
ii. To display float numbers in right justified format with precision of two digits
iii. To Exchange the private values of both these classes by using Friend function.
[Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class class2;
class class1
{
private:
float a;
public:
void accept_a()
{
cout<<"Enter value for a : ";
cin>>a;
}
void display_a()
{
cout<<"value of a = ";
cout.width(10);
cout.precision(2);
cout<<setiosflags(ios::right);
cout<<a;
}
friend void exchange(class1,class2);
};
class class2
{
private:
float b;
public:
void accept_b()
{
cout<<"\nEnter value for b : ";
cin>>b;
}
void display_b()
{
cout<<"\nvalue of b = ";
cout.width(10);
cout.precision(2);
cout<<setiosflags(ios::right);
cout<<b;
}
friend void exchange(class1,class2);
};
void exchange(class1 c1, class2 c2)
{
float temp;
temp= c1.a;
c1.a=c2.b;
c2.b=temp;
cout<<"value of a = ";
cout.width(10);
cout.precision(2);
cout<<setiosflags(ios::right);
cout<<c1.a;
cout<<"\nvalue of b = ";
cout.width(10);
cout.precision(2);
cout<<setiosflags(ios::right);
cout<<c2.b;
}
int main()
{
class1 m;
class2 n;
clrscr();
m.accept_a();
n.accept_b();
cout<<"\nOriginal private values of both classes\n";
m.display_a();
n.display_b();
cout<<"\nExchange private values of both classes\n";
exchange(m,n);
getch();
return 0;
}
Output
Enter value for a : 9.6
Enter value for b : 9.5
Original private values of both classes
value of a = 9.6
value of b = 9.5
Exchange private values of both classes
value of a = 9.5
value of b = 9.6
Tags:
c++