Slip30
Q.1) Write a C++ program to sort integer and
float array elements in ascending order by using function overloading.
#include<iostream.h>
#include<conio.h>
class
bubble
{
public:
void
array(int ans);
void
array(float ans);
};
void
bubble::array(int ans)
{
cout<<"\n"<<ans;
}
void
bubble::array(float ans)
{
cout<<"\n"<<ans;
}
void
main()
{
bubble
bb;
int
i,j,n,a[20],temp=0;
float
b[20];
clrscr();
cout<<"Enter
how many integer numbers you want to sort:";
cin>>n;
cout<<"\nEnter
integer elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(j=0;i<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
cout<<"\nInteger
array after sorting:\n";
for(i=0;i<n;i++)
bb.array(a[i]);
cout<<"Enter
how many float numbers you want to sort:";
cin>>n;
cout<<"\nEnter
float elements:\n";
for(i=0;i<n;i++)
cin>>b[i];
for(i=0;i<n;i++)
{
for(j=0;i<n-i;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[i];
b[i]=temp;
}
}
}
cout<<"\nInteger
array after sorting:\n";
for(i=0;i<n;i++)
bb.array(b[i]);
getch();
}
Q.2) Create
a class Fraction containing data members as Numerator and Denominator.
Write a C++ program to overload operators ++ , -- and * to increment ,
decrement a Fraction and multiply two Fraction respectively. (Use constructor
to initialize values of an object).
#include<iostream.h>
#include<conio.h>
class
fraction
{
int
num;
int
deno;
public:
fraction()
{
}
fraction(int
a,int b)
{
num=a;
deno=b;
}
void
display()
{
cout<<"\n
The numerator is-"<<num;
cout<<"\n
The denominator is-"<<deno;
cout<<"\nFraction
is-"<<num<<"/"<<deno;
}
void
operator ++()
{
num=++num;
deno=++deno;
}
void
operator --()
{
num=--num;
deno=--deno;
}
friend
fraction operator *(fraction &, fraction &)
};
fraction
operator *(fraction &p, fraction &q)
{
fraction
a;
a.num=p.num
* q.num;
a.deno=p.deno
* p.deno;
return
a;
}
void
main()
{
fraction
f1(2,1);
fraction
f2(3,2);
fraction
f3;
clrscr();
f1.display();
f2.display();
cout<<"\nAfter
decrement-";
--f1;
f1.display();
--f2;
f2.display();
cout<<"\nAfter
increment-";
++f1;
f1.display();
++f2;
f2.display();
f3=f1*f2;
cout<<"\nAfter
multiplication-";
f3.display();
getch();
}