### Archive

Archive for January 12, 2010

## C++ Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE – Q12

Q12. Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE:

Calculate the toll tax for cars passing by a toll bridge @ Rs 5 per car (use the concept static data members). Calculate and Print the following details:
– Total no of cars passed with paying the tax
– Total amount of tax paid
– Total no of cars passed without paying tax.

… from College notes (BCA/MCA assignments):

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

class TollBridge{
private:
static int car_yes, car_no;
static int amt;
public:
static void GetCar();
static void PutCar();
};

int TollBridge::car_yes = 0;
int TollBridge::car_no = 0;
int TollBridge::amt = 0;

void TollBridge :: GetCar(){
char ch;
cout<<"\n Total Cars Passed: "<<car_yes+car_no;
cout<<"\n This Car:- ";
cout<<"\n Paid Tax (y/n) ?: ";
cin>>ch;
if(ch == 'y'){
car_yes++;
amt += 5;
}
else
car_no++;
}

void TollBridge :: PutCar(){
cout<<"\n Total Car Passed: "<<car_yes+car_no<<endl;
cout<<"\n\t Car Passed by giving Tax: "<<car_yes;
cout<<"\n\t\t Amount Recieved: "<<amt<<endl;
cout<<"\n Car Passed by not giving Tax: "<<car_no;
}

void main(){
int ch;
char choice;
while(1){
clrscr();
cout<<"\n TOLL TAX PLAZA";
cout<<"\n ~~~~~~~~~~~~~~";
cout<<"\n 1 -> Entry of CARS.";
cout<<"\n 2 -> Report of CARS.";
cout<<"\n 3 -> Exit.";
cin>>ch;
switch(ch){
case 1:
while(1){
TollBridge::GetCar();
cout<<"\n Do you want to continue
(y/n) ?: ";
cin>>choice;
if(choice =='n') break;
}
break;
case 2:
TollBridge::PutCar();
getch();
break;
default:
exit(1);
} // end of switch.
} // end of while.
} // end of main.

Output:

TOLL TAX PLAZA
1 -> Entry of CARS.
2 -> Report of CARS.
3 -> Exit.

Total Cars Passed: 0
This Car:-
Paid Tax (y/n) ?: y

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

Total Cars Passed: 1
This Car:-
Paid Tax (y/n) ?: y

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

Total Cars Passed: 2
This Car:-
Paid Tax (y/n) ?: y

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

Total Cars Passed: 3
This Car:-
Paid Tax (y/n) ?: n

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

Total Cars Passed: 4
This Car:-
Paid Tax (y/n) ?: n

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

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

TOLL TAX PLAZA
1 -> Entry of CARS.
2 -> Report of CARS.
3 -> Exit.