簡體   English   中英

c ++中構造函數中的析構函數?

[英]Destructor in Constructor in c++?

我正在編寫一個票務預訂系統,我只是計划如何編寫它我面臨的問題有點棘手。 我的要求是:

  • 一輛巴士一次只有 100 個預訂。
  • 如果超過 100,則無法預訂。

現在我面臨的問題是,當超出限制時,我沒有得到不為乘客預訂機票的邏輯。 並自動將其預訂轉移到下一班車。

這是我迄今為止所做的代碼。

#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 {

};

在撰寫此答案時,我正在考慮我們有無限數量的可用總線。 現在我們 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM