首页 > 解决方案 > c ++中构造函数中的析构函数?

问题描述

我正在编写一个票务预订系统我正计划如何对其进行编码我面临的问题有点棘手。我的要求是:

现在我面临的问题是,当超出限制时,我没有逻辑不为乘客预订机票。并自动将其预订转移到下一班车。

这是我到目前为止所做的代码。

#include<iostream>
using namespace std;
class BUS {
int SOD = 6;
int EOD = 22;
int capacity=0;
int BUS_NO;
public:
BUS(int no) :BUS_NO(no) {
    if (capacity == 100) {
        cout << "Capacity is Full\n";
        return;
    }
    else {
            if (capacity != 100) 
                capacity++;
    }
}
int bus_No() { return BUS_NO; }
void routes() {
    cout << "=========================================\n";
    cout << "1.Saddar " << "2.Marrir Chowk\n";
    cout << "3.Liaquat Bagh " << "4.Committe Chowk\n";
    cout << "5.waris Chowk " << "6.Chandni Chowk\n";
    cout << "7.Rehman " << "8.6th Road\n";
    cout << "9.Shamsabad " << "10.Faizabad\n";
    cout << "11.Pothar " << "12.Khayan e Johar\n";
    cout << "11.Pothar " << "12.Khayan e Johar\n";
    cout << "13.Faiz Ahmad Road " << "14.Kashmir Highway\n";
    cout << "15.Chaman " << "16.Ibn e Sina\n";
    cout << "=========================================\n";
}
void reserveTicket() {
    if (capacity != 100) capacity++;
    else cout << "Wait for Next Bus plzz! This is full.\n";
}
};
class PASSENG {

};

标签: c++

解决方案


在撰写此答案期间,我正在考虑我们有无限数量的可用巴士。现在我们有 3 个函数。

1. ticket_book(int n)=> 此功能将为我们想要的乘客数量预订机票。如果乘客数量增加,则会自动预订下一班可用巴士的车票。

2. get_bus_capacity()=> 此函数将返回当前巴士上当前可用的座位。3. get_bus_number()=> 这个函数会返回当前的总线号。

#include<iostream>
using namespace std;
class BUS {
int SOD = 6;
int EOD = 22;
int capacity=0;
int BUS_NO = 1;
public:
void book_ticket(int n)
{
    if(capacity + n <= 100)
    {
        capacity += n;
    }
    else
    {
        int cur_bus_seat = 100 - capacity;
        int next_bus_seat = capacity + n - 100;
        capacity = next_bus_seat;
        BUS_NO += 1;
    }
}
int get_bus_capacity()
{
    return 100 - capacity;
}
int get_bus_no()
{
    return BUS_NO;
}
void routes() {
    cout << "=========================================\n";
    cout << "1.Saddar " << "2.Marrir Chowk\n";
    cout << "3.Liaquat Bagh " << "4.Committe Chowk\n";
    cout << "5.waris Chowk " << "6.Chandni Chowk\n";
    cout << "7.Rehman " << "8.6th Road\n";
    cout << "9.Shamsabad " << "10.Faizabad\n";
    cout << "11.Pothar " << "12.Khayan e Johar\n";
    cout << "11.Pothar " << "12.Khayan e Johar\n";
    cout << "13.Faiz Ahmad Road " << "14.Kashmir Highway\n";
    cout << "15.Chaman " << "16.Ibn e Sina\n";
    cout << "=========================================\n";
}
};
class PASSENG {

};


int main()
{
    BUS bus;
    int n;
    
    while(1)
    {
        int choice;
        cout<<"Enter Your Choice"<<"\n";
        cout<<"Enter 1 to book ticket"<<"\n";
        cout<<"Enter 2 to check number of seats"<<"\n";
        cout<<"Enter 3 to check Bus No"<<"\n";
        cin>>choice;
        if(choice == 1)
        {
            cout<<"Enter the number of tickets to be booked";
            cin>>n;
            bus.book_ticket(n);
        }
        else if(choice == 2)
        {
            cout<<bus.get_bus_capacity()<<"\n";
        }
        else if(choice == 3)
        {
            cout<<bus.get_bus_no()<<"\n";
        }

    }
    return 0;
}

推荐阅读