结合qt和C++面向对象的代码
时间: 2024-03-27 13:36:42 浏览: 61
好的,下面是一个简单的结合Qt和C++面向对象的航班订票系统的示例代码:
flight.h
```cpp
#ifndef FLIGHT_H
#define FLIGHT_H
#include <QString>
class Flight
{
public:
Flight(QString flightNumber, QString departureCity, QString arrivalCity, int availableSeats);
QString getFlightNumber();
QString getDepartureCity();
QString getArrivalCity();
int getAvailableSeats();
void reserveSeat();
void releaseSeat();
private:
QString m_flightNumber;
QString m_departureCity;
QString m_arrivalCity;
int m_availableSeats;
};
#endif // FLIGHT_H
```
flight.cpp
```cpp
#include "flight.h"
Flight::Flight(QString flightNumber, QString departureCity, QString arrivalCity, int availableSeats)
: m_flightNumber(flightNumber), m_departureCity(departureCity), m_arrivalCity(arrivalCity), m_availableSeats(availableSeats)
{
}
QString Flight::getFlightNumber()
{
return m_flightNumber;
}
QString Flight::getDepartureCity()
{
return m_departureCity;
}
QString Flight::getArrivalCity()
{
return m_arrivalCity;
}
int Flight::getAvailableSeats()
{
return m_availableSeats;
}
void Flight::reserveSeat()
{
if (m_availableSeats > 0) {
m_availableSeats--;
}
}
void Flight::releaseSeat()
{
m_availableSeats++;
}
```
mainwindow.h
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QListWidgetItem>
#include "flight.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_flightListWidget_itemClicked(QListWidgetItem *item);
void on_reserveButton_clicked();
void on_releaseButton_clicked();
private:
Ui::MainWindow *ui;
QList<Flight*> m_flights;
Flight* m_selectedFlight;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Create some sample flights
m_flights.append(new Flight("ABC123", "New York", "Los Angeles", 50));
m_flights.append(new Flight("DEF456", "Chicago", "Houston", 75));
m_flights.append(new Flight("GHI789", "San Francisco", "Seattle", 100));
// Populate the flight list widget
foreach (Flight* flight, m_flights) {
ui->flightListWidget->addItem(flight->getFlightNumber());
}
}
MainWindow::~MainWindow()
{
delete ui;
// Delete the flights
foreach (Flight* flight, m_flights) {
delete flight;
}
}
void MainWindow::on_flightListWidget_itemClicked(QListWidgetItem *item)
{
// Get the selected flight
QString flightNumber = item->text();
foreach (Flight* flight, m_flights) {
if (flight->getFlightNumber() == flightNumber) {
m_selectedFlight = flight;
break;
}
}
// Update the available seats label
ui->availableSeatsLabel->setText(QString::number(m_selectedFlight->getAvailableSeats()));
}
void MainWindow::on_reserveButton_clicked()
{
// Reserve a seat for the selected flight
m_selectedFlight->reserveSeat();
// Update the available seats label
ui->availableSeatsLabel->setText(QString::number(m_selectedFlight->getAvailableSeats()));
}
void MainWindow::on_releaseButton_clicked()
{
// Release a seat for the selected flight
m_selectedFlight->releaseSeat();
// Update the available seats label
ui->availableSeatsLabel->setText(QString::number(m_selectedFlight->getAvailableSeats()));
}
```
这个示例代码演示了如何用Qt实现一个简单的航班订票系统,其中使用了C++面向对象编程的一些基本概念,例如类和对象、构造函数和析构函数、公有和私有成员函数、以及访问控制等。在这个示例代码中,Flight类表示一个航班,包含航班号、出发城市、到达城市和可用座位数等信息,同时提供了订票和释放座位等方法。MainWindow类表示主窗口,包含一个航班列表部件、一个选择航班的标签、一个显示可用座位数的标签、一个订票按钮和一个释放座位按钮等控件,同时也负责管理航班列表和处理用户的交互操作。
阅读全文