Answer to Question #213209 in C++ for Hemambar

Question #213209

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-25T08:24:31-0400
#include <iostream>


using namespace std;


class Train {
protected:
    int seats_first_class;
    int seats_second_class;
    int seats_ac_2tier;
public:
    void setData() {
        cout << "\nEnter seats first class: ";
        cin >> seats_first_class;
        cout << "\nEnter seats second class: ";
        cin >> seats_second_class;
        cout << "\nEnter seats ac 2 tier: ";
        cin >> seats_ac_2tier;

    }

    void displayData() {
        cout << "seats first class" << seats_first_class << endl;
        cout << "seats second class" << seats_second_class << endl;
        cout << "seats ac 2 tier" << seats_ac_2tier << endl;
    }
};

class Reservation : public Train {
private:
    int booked_first_class;
    int booked_second_class;
    int booked_ac_2tier;
public:
    void bookSeat() {
        cout << "\nyou have booked the seat";
    }

    void cancelSeat() {
        cout << "\nyou have cancelled the seat";
    }

    void display() {
        cout << booked_first_class;
        cout << booked_second_class;
        cout << booked_ac_2tier;
    }
};

int main() {
    Train t;
    t.setData();
    t.displayData();
    Reservation r;
    r.bookSeat();
    r.cancelSeat();
    r.display();
    

    return 0;
}

Example:
Enter seats first class: 2


Enter seats second class: 5


Enter seats ac 2 tier: 1
seats first class2
seats second class5
seats ac 2 tier1


you have booked the seat
you have cancelled the seat

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