c++ - 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;
}
推荐阅读
- python - 继承一个虚拟类方法 - 如何从基类调用它?
- django - 如何在 django-tenants 中以编程方式执行交互式命令
- javascript - 自定义工具提示或自定义标签(chart.js 2)?
- python - 无法在 Jupyter Notebook 上运行 fastai 库
- java - java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)
- sql - Power Designer 中的自动增量代理键
- java - Selenium 脚本在本地传递,但在构建时在 Jenkins 上失败
- vue.js - 为什么 v-on 在外部模板中使用时对 Vue 组件不起作用?
- laravel - Laravel 5.5 预期响应代码 250 但得到代码“530”
- html - css:样式图像网格