Slip21
Q.1) Write
a C++ program to swap two integer values and two float values by using function
template.
#include<iostream.h>
#include<conio.h>
template<class
T>
void
swap(T &value_1,T &value_2)
{
T
temp;
temp=value_2;
value_2=value_1;
value_1=temp;
}
int
main()
{
clrscr();
int
int_1,int_2;
float
float_1,float_2;
cout<<"\n\n***********
Data Type - Int ***********";
cout<<"\nEnter
the value of int_1=";
cin>>int_1;
cout<<"\nEnter
the value of int_2=";
cin>>int_2;
cout<<"\n\n***********
Data Type - Float ***********";
cout<<"\nEnter
the value of float_1=";
cin>>float_1;
cout<<"\nEnter
the value of float_2=";
cin>>float_2;
cout<<"\n\n***********
After Swap ***********";
cout<<"\nValues
of Int:";
swap(int_1,int_2);
cout<<"\nValue
of int_1="<<int_1;
cout<<"\nValue
of int_2="<<int_2;
cout<<"\n\nValues
of Floats:";
swap(float_1,float_2);
cout<<"\nValue
of float_1="<<float_1;
cout<<"\nValue
of float_2="<<float_2;
getch();
return
0;
}out<<"\n\n***********
After Swap ***********";
cout<<"\nValues
of Int:";
swap(int_1,int_2);
cout<<"\nValue
of int_1="<<int_1;
cout<<"\nValue
of int_2="<<int_2;
cout<<"\n\nValues
of Floats:";
swap(float_1,float_2);
cout<<"\nValue
of float_1="<<float_1;
cout<<"\nValue
of float_2="<<float_2;
getch();
return
0;
}
Q.2) Imagine a ticket Selling booth at a
fair. People passing by are requested to purchase a ticket. A ticket is priced
as Rs. 5/- . The ticketbooth keeps the track of the number of peoples that have
visited the booth and of the total amount of cash collected.
Create a class ticketbooth that contains
No_of_people_visited, Total_Amount_collected. Write C++ program to perform
following functions:
i.
To assign
initial values.
ii.
To increment
the No_of_people_visited if visitors have
just visited the booth.
iii.
To increment
the No_of_people_visited and Total_amount_collected if tickets have
been purchased.
iv. To display both totals
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class
ticketbooth
{
int
visited,totalamt;
public:
ticketbooth()
{
visited=0;
totalamt=0;
}
void
paypeople()
{
visited=1+visited;
totalamt=5+totalamt;
return;
}
void
visitpeople()
{
visited=1+visited;
return;
}
void
display()
{
cout<<"\nPeople visited so
far:\t"<<visited;
cout<<"\nTotal amount
collected:\t"<<totalamt;
}
};
void
main()
{
ticketbooth
t;
int
choice;
clrscr();
cout<<"\n1.Increase
people visited";
cout<<"\n2.Increase
people visited and amount collected";
cout<<"\n3.Display
both totals";
cout<<"\n4.Exit";
do
{
cout<<"\n\nEnter
your choice";
cin>>choice;
switch(choice)
{
case
1:
t.visitpeople();
cout<<"\n\nNumber
of people visited incremented";
break;
case
2:
t.paypeople();
cout<<"\n\nNumber
of people visited and total cash incremented";
break;
case
3:
t.display();
break;
case
4:
exit(0);
default:
cout<<"\n\nWrong
choice";
}
}while(choice!=4);
getch();
}