Slip21 C++

C++ Programming

Slip21

Q.1) Write a C++ program to swap two integer values and two float values by using function
template. [Marks 15]

Solution

#include<iostream.h>
#include<conio.h>
template <class X>
void swapargs(X &a, X &b)
{
  X temp;

  temp = a;
  a = b;
  b = temp;
}

int main()
{  clrscr();
  int i=10, j=20;
  float x=1.1, y=2.1;

  cout << "Original i, j: " << i << ' ' << j << endl;
  cout << "Original x, y: " << x << ' ' << y << endl;

  swapargs(i, j); // swap integers
  swapargs(x, y); // swap floats

  cout << "Swapped i, j: " << i << ' ' << j << endl;
  cout << "Swapped x, y: " << x << ' ' << y << endl;
  getch();
  return 0;
}


Output

Original i, j: 10 20
Original x, y: 1.1 2.1
Swapped i, j: 20 10
Swapped x, y: 2.1 1.1


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 [Marks 25]

Solution

#include<iostream.h>
#include<conio.h>

class ticketbooth
{
  // price of ticket = 5
  private:
   static int no_ppl;
   static double total;
   int tkt;

  public:
    void accept()
    {

      cout<<"\n Enter no. of people visited till now:";
      cin>>no_ppl;
      cout<<"\n Enter total tickets purchased:" ;
      cin>>tkt;
      total = tkt * 5;
    }
    void display()
    {
      cout<<"\n Total amount collected : "<<total;
      cout<<"\n No. of people visited: "<<no_ppl;
    }
    void trans();

};


  void ticketbooth :: trans()
  {
       int n; char ch,cont;
       do
       {
       cout<<"\nEnter no. of tickets you need:"; cin>>n;
       cout<<"\nPurchase tickets? (y/n) :"; cin>>ch;
       no_ppl = no_ppl+n;

       if(ch=='y')
       {
  total = total + (n*5);
       }
       cout<<"\n Do you want to continue?(y/n): ";
       cin>>cont;
       } while(cont=='y');
  }
double ticketbooth :: total;
int ticketbooth :: no_ppl;
int main()
{
    clrscr();
    ticketbooth t;
    t.accept();
    t.display();
    t.trans();
    t.display();
    getch();
    return 0;
}

Output

 Enter no. of people visited till now:5

 Enter total tickets purchased:15

 Total amount collected : 75
 No. of people visited: 5
 Enter no. of tickets you need:10

 Purchase tickets? (y/n) :y

 Do you want to continue?(y/n): y

 Enter no. of tickets you need:5

 Purchase tickets? (y/n) :n

 Do you want to continue?(y/n): n

 Total amount collected : 125
 No. of people visited: 20
BCA Pratical Solution

My name is Vivek And I from Mumbai and Complete my Graduation Bca.my Age is 23 Years.

Post a Comment

Previous Post Next Post