A Simple Ticket Management System in C++ with Multiple Inheritance

PROBLEM:

Bangladesh Railway Services has decided to develop a simple ticket management system. The company wants to use Object Oriented Programming concept. More specifically, the company wants you to use multiple inheritance.

See the specification below from the company --

Class Name

Member Data

Member Function

CustomerDetails

protected:

string cusName="name";

string cusTelNum="0000000";

string cusNationalId="0000000";

public:

void setCusName(string name)

{//set customer name}

 void setCusTelNum(string num)

{// set customer number}

void setCusID(string id)

{// set customer id }

 

void displayCusDetails()

{//show customer details}

 

JourneyDetails

protected:

string travelDate="00/00/1900";

string travelTo="Bangladesh";

string travelFrom="Bangladesh";

int distance=0;

public:

 string setTravelDate(string date)

{//set travel date}

 string setTravelTo(string trTo)

{//set destination to }

 string setTravelFrom(string Fr)

{// set destination from}

 int setDistance(int d)

{// set distance }

 void showJourneyDetails()

{//show journey details}


TicketIssue

protected:

float ticketPrice=0.00;

float chrg=0.00

public:

void printTicket()

{

In this method, you must calculate and print the ticket price. When the journey distance is less than 100km, the company applies a 20% charge on the actual ticket price.

When it is between 101 to 199km, the charge is 10% and above 200, the charge is 0%.

Here you must implement the conditions and show the final ticket price.

}



Class TicketIssue inherits from JourneyDetails and CustomerDetails classes. Write the main() of the program and create two cus1, cus2 objects with reference to TicketIssue class. Show the use of accessing all the methods and print the ticket price. See the output below:


A Simple Ticket Management System in C++ with Multiple Inheritance



SOLUTION:


EXPLANATION:

In this problem, we are told to use two modifiers - protected and public. The protected modifier will include the variables. So that, the variables' data cannot be changed outside the class. And the public modifier will include method functions to set data to the variables. Also, we will need one method function to get or display the data.

We will have to create three classes as mentioned in the problem. The classes are - CustomerDetails, JourneyDetails and TicketIssue. The first class will include the information of a customer. The second class will include the customer's journey details. And the last class will show the ticket price according to the customer's travel information.


We are also told to use the multiple inheritance system to inherit the first two classes into the third one. We can do this by following this syntax :

class thirdClass: public firstClass, public secondClass{};

The problem also wants us to calculate the charge of the ticket price using three conditions. So, we used if - else if statement to calculate the charge based on these conditions. Later if one condition is met, we will calculate the real amount of charge from the charge percentage using this formula :

charge amount = ticket price *  charge percentage / 100

After getting the charge, we can just add this charge with the ticket price to print the final price.

Post a Comment

Previous Post Next Post