Bus Reservation Syestem Using C++ Class and object

The main purpose of this topic is to make a good database for the bus reservation systems. This database will help the admin to perform tasks like installing bus information, reservation bus seats, showing reservation information, and will also show information regarding the buses available.


This document will teach you what is class and object in C++ Language.

And also this document will show you an example of how to construct a database using classes and objects in C++. Here data of bus information is not stored in the file so in every run of the program previous data is lost so you can implement the file handling to store all the bus details. And also with the class implementation and function, it will show all information like bus number, driver’s name, arrival and departure time, and empty and reserved seat of a bus.

A class in C++ is a user-defined data type that contains its own Data Members and Member Functions, which can be accessed and used by creating an instance. For example, We will create a Class of Mobile Phone. There may be many types of Mobile Phone with different names or brand but there are some common properties among them all of them will have a Screen Size, Internal Storage Size, Camera Pixel etc. So here we can say, Mobile Phone is the class and Screen Size, Internal Storage Size, Camera Pixel are their properties, and the properties are the Data Members. Member functions are used to manipulate these variables, In the above example of class Mobile Phone member functions can be applied updates, increase storage etc.

How to define a class it is shown below:

After defining the class then it needs to declare object because by declaring it, we will be able to use the data and access functions that are defined in the class. It helps to allocate memory or storage. It should be declared in the main function.

The Syntax of declaring objects given below:

After declaring the object by using a dot (.) operator the data members and member functions of class can be accessed. For an example: a member function is named as ‘Show_ID()’ and we declared an object name as ‘edu’ so we have to write edu.Show_ID() for accessing the member functions.

Access Specifier Types & Details:

Access specifier is used to assign the accessibility to the class data-members. There are three types of accessibility:

~Public: Everyone will be able to access all the class data-members declared under the public specifier.

~Private: Only the member functions can access the class data-members declared as private, not everyone is allowed to access.

~Protected: This specifier is similar as the private specifier; the only difference is that the class data-members can be accessed by any subclass (derived class) of that class.

As we are implementing a bus reservation database system for admin, he will need to know and access every data-member and member functions so that’s why here we are using public access specifier.

Flow Chart of Working Procedure:

Implementation Code of Bus Reservation System

Implementing this system first we will need to add the Header File or Standard files, so add your needed Header File or Standard files.

As we are implementing this project in C++ so we will need to add:

#include <iostream>

And we will use string values in the code that’s why we will need this Header file:

#include <string.h>

Define the Class and Sub-Class:

As we are building a bus reservation system, here in this database there will be information of different type of bus details. So here we will create a class named of BUSCLASS which contains some needed properties and member functions.

The Code of Class is given Below:

We know that there are different types of bus. Here we are taking two types of bus type: AC and NON-AC. So here we will create two sub-class of the class BUSCLASS. Both sub-classes will inherit properties from the class. As we will need to store the bus information that’s why only busclass() member function is called, from upper code we see that busclass() member function stores the bus information.

The Code of Sub-Class is given Below:

Other Member Functions Code which will be called by Object

There is total four member functions which should be called by the object. The details of the member functions are given below:

  • Void allotment(): This member function helps to reserve the seat for customer. If it is not empty then it will go back to start where it will ask your choice again, otherwise if seat is empty then it will ask for passenger name and will reserve the seat.
  • Void show(): This member function will ask you which bus number information you want to know. After giving the bus number, it will show you the empty number of seats and also the reservation details along with the bus details.
  • Void avail(): This member function will show all bus details without the reservation details.
  • Void position(int i): this member function will help to show you the bus seat details means if the seat is empty then with will show empty otherwise it will show the passenger’s name.

The Code of Member Functions are given below:

The Main Function of the Code:

Now we have to write the main function which will help to run the whole code by asking our choice.

The Code of the Main Function given below.

In the main function we created an object *bus[10]; here pointer object is used because a here pointer used to store the address of an object of a class. And also created an infinite loop where first it shows the choices which process we want to process, after entering our choice it will show the next step according to our own choice which is done by Switch-Case. There is a variable p which is defined as global variable in my code, here this p is mainly use as to store the total number of buses which helps to show different outputs.

The Output of the Code is given below:

1. The first output which it will show first:

2. If we select choice 1 then the output will be:

3. If we choose option 4 then it will show the available bus details but before that we will need to install the bus information. The output is given below:

4. The output of option two Reservation is given below:

Suppose the seat is not empty then the output will be:

5. The Output of option three Show is given below:

Here it is showing the selected bus details and also showing seat reservation details.

Leave a Reply

Your email address will not be published. Required fields are marked *