Answer to Question #213286 in C++ for Hemambar

Question #213286

Write a program that has a class Train with data members seats_first_class, seats_second_class

and seats_ac_2tier and member functions to set and display data. Derive a class Reservation that has

data members booked_first_class, booked_second_class and booked_ac_2tier and functions to book

and cancel tickets and display status.


1
Expert's answer
2021-07-29T23:33:55-0400
#include <iostream>
using namespace std;
class Train{
    protected:
    int seats_first_class, seats_second_class, seats_ac_2tier;
    public:
    Train(){
        setValues();
    }
    void setValues(){
        cout<<"Enter first class seats available: ";
        cin>>seats_first_class;
        cout<<"Enter second class seats available: ";
        cin>>seats_second_class;
        cout<<"Enter ac 2 tier seats available: ";
        cin>>seats_ac_2tier;
    }
};
class Reservation:public Train{
    int booked_first_class, booked_second_class, booked_ac_2tier;
    public:
    Reservation():Train(){
        booked_first_class = booked_second_class = booked_ac_2tier = 0;
    }
    void bookFirstClassTicket(int tickets){
        if(seats_first_class >= tickets){
            booked_first_class += tickets;
            seats_first_class -= tickets;
            cout<<"\nSuccess";
        }
        else{
            cout<<"\nNot enough spots left";
        }
    }
    void bookSecondClassTicket(int tickets){
        if(seats_second_class >= tickets){
            booked_second_class += tickets;
            seats_second_class -= tickets;
            cout<<"\nSuccess";
        }
        else{
            cout<<"\nNot enough spots left";
        }
    }
    void bookAc2TierTicket(int tickets){
        if(seats_ac_2tier >= tickets){
            booked_ac_2tier += tickets;
            seats_ac_2tier -= tickets;
            cout<<"\nSuccess";
        }
        else{
            cout<<"\nNot enough spots left";
        }
    }
    void cancelFirstClassTicket(int tickets){
        if(booked_first_class - tickets >= 0){
            booked_first_class -= tickets;
            seats_first_class += tickets;
        }
    }
    void cancelSecondClassTicket(int tickets){
        if(booked_second_class - tickets >= 0){
            booked_second_class -= tickets;
            seats_second_class += tickets;
        }
    }
    void cancelAc2TierTicket(int tickets){
        if(booked_ac_2tier - tickets >= 0){
            booked_ac_2tier -= tickets;
            seats_ac_2tier += tickets;
        }
    }
    void displayStatus(){
        cout<<"\nFirst class seats\nAvailable: "<<seats_first_class<<"\nBooked: "<<booked_first_class<<endl;
        cout<<"\nSecond class seats\nAvailable: "<<seats_second_class<<"\nBooked: "<<booked_second_class<<endl;
        cout<<"\nAc 2 tier seats\nAvailable: "<<seats_ac_2tier<<"\nBooked: "<<booked_ac_2tier<<endl;
    }
};
int main(){
    Reservation reservation;
    int choice, tickets;
    do{
        cout<<"\n1. Book 1st class\n2. Book 2nd class\n3. Book ac 2 tier";
        cout<<"\n4. Cancel 1st class\n5. Cancel 2nd class\n6. Cancel ac 2 tier";
        cout<<"\n7. View Status\n8. Exit\n";
        cin>>choice;
        switch(choice){
            case 1: cout<<"Input tickets: ";
                    cin>>tickets;
                    reservation.bookFirstClassTicket(tickets);
                    break;
            case 2: cout<<"Input tickets: ";
                    cin>>tickets;
                    reservation.bookSecondClassTicket(tickets);
                    break;
            case 3: cout<<"Input tickets: ";
                    cin>>tickets;
                    reservation.bookAc2TierTicket(tickets);
                    break;
            case 4: cout<<"Input tickets: ";
                    cin>>tickets;
                    reservation.cancelFirstClassTicket(tickets);
                    break;
            case 5: cout<<"Input tickets: ";
                    cin>>tickets;
                    reservation.cancelSecondClassTicket(tickets);
                    break;
            case 6: cout<<"Input tickets: ";
                    cin>>tickets;
                    reservation.cancelAc2TierTicket(tickets);
                    break;
            case 7: reservation.displayStatus();
                    break;
            default: break;
        }
    }while(choice > 0 && choice < 8);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS