Object-Oriented Programming (OOP) is a powerful paradigm that helps structure code in a more organized and manageable way. If you’re looking for a practical example to understand OOP concepts in C++, simulating a real-world scenario like a parking ticket system is an excellent approach. This article will guide you through a C++ program designed to simulate a police officer issuing a parking ticket, breaking down each class and its role in the system.
This example is perfect for beginners wanting to grasp the fundamentals of classes, objects, and how they interact to create a functional program. We’ll explore the code structure, explain the purpose of each class, and show you how they work together to simulate the parking ticket process.
Understanding the Classes in Our Parking Simulator
Our parking ticket simulator is built using four main classes, each representing a key entity in the parking ticket scenario:
ParkedCar
Class: This class represents a parked car and holds information about the car.ParkingMeter
Class: This class simulates a parking meter, tracking the amount of paid parking time.ParkingTicket
Class: This class is responsible for creating and reporting parking tickets when a car is parked illegally.PoliceOfficer
Class: This class simulates a police officer who inspects cars and issues tickets.
Let’s delve into each class to understand their responsibilities and how they are implemented in C++.
The ParkedCar
Class: Defining Car Attributes
The ParkedCar
class is designed to store information about a parked vehicle. It encapsulates the attributes that define a car in our simulation. Here’s a look at its structure (from ParkedCar.h
and its usage):
class ParkedCar {
private:
string make;
string model;
string color;
string licenseNumber;
int parkedMinutes;
public:
// ... (Constructors, setters, getters, display function) ...
void set(string amake, string amodel, string acolor, string alicensenumber, int aparkedminutes);
int getparkedminutes() const;
void parkedcardisplay() const;
};
As you can see, the ParkedCar
class holds private member variables for make
, model
, color
, licenseNumber
, and parkedMinutes
. These variables store the car’s make, model, color, license plate number, and the number of minutes it has been parked, respectively. The public section includes functions to set these attributes (set
), retrieve the parked time (getparkedminutes
), and display car information (parkedcardisplay
). This class focuses solely on representing the data associated with a parked car.
The ParkingMeter
Class: Tracking Paid Parking Time
The ParkingMeter
class is simpler, with the primary responsibility of tracking the amount of parking time purchased. Here’s the ParkingMeter
class structure:
class ParkingMeter {
private:
int minutesPurchased;
public:
// ... (Constructor, setter, getter) ...
void setparkingmeter(int minutes);
int getparkingmeter() const;
};
The ParkingMeter
class has one key private member: minutesPurchased
. This integer variable stores the number of minutes of parking time that a user has paid for at the meter. The class provides public methods to set the purchased time (setparkingmeter
) and retrieve it (getparkingmeter
). Its role is limited to managing the paid parking duration.
The ParkingTicket
Class: Issuing and Reporting Violations
The ParkingTicket
class is crucial for simulating the issuance of parking tickets. It determines if a car is illegally parked, calculates the fine, and reports the ticket details. Let’s examine its structure:
class ParkingTicket {
private:
int fine;
ParkingMeter PM;
ParkedCar PC;
PoliceOfficer PO;
public:
ParkingTicket();
ParkingTicket(string aname, string abadge, string amake, string amodel, string acolor, string alicensenumber, int aparkedminutes, int aminutespurchased);
~ParkingTicket();
int getfine() const;
void generateticket();
void reportCarInfo();
void reportfine();
void reportOfficer();
};
The ParkingTicket
class contains several important components. Privately, it holds:
fine
: An integer to store the calculated parking fine.PM
(ParkingMeter): An object of theParkingMeter
class to access parking time information.PC
(ParkedCar): An object of theParkedCar
class to access car details.PO
(PoliceOfficer): An object of thePoliceOfficer
class to record officer information (although in the provided code it’s used in constructor but not consistently afterwards for reporting).
The public methods are designed to:
- Calculate the fine based on illegally parked time.
- Generate and display the parking ticket information, including car details, fine amount, and officer details.
- Report car, fine, and officer information separately.
The ParkingTicket
class acts as the core logic for determining parking violations and generating tickets. The fine calculation logic is implemented in ParkingTicket.cpp
within the reportfine()
method:
void ParkingTicket::reportfine() {
int illegaltime = PC.getparkedminutes() - PM.getparkingmeter();
if (illegaltime> 0 && illegaltime <= 60)
{
fine = 25;
}
else {
if (illegaltime > 60) {
int temp = illegaltime;
fine = 25;
while (temp > 0 || temp > 60) {
fine += 10;
temp -= 60;
}
}
}
cout << "Fine: $" << fine << endl;
}
This code snippet shows how the fine is calculated: $25 for the first hour (or part thereof) of illegal parking, and $10 for each additional hour (or part thereof).
The PoliceOfficer
Class: Inspecting Cars and Issuing Tickets
Finally, the PoliceOfficer
class simulates a police officer’s actions in inspecting parked cars and issuing tickets if necessary. Here’s the class definition:
class PoliceOfficer {
private:
string name;
string badge;
ParkingMeter PM;
ParkedCar PC;
public:
PoliceOfficer();
PoliceOfficer(string aname, string abadge, string amake, string amodel, string acolor, string alicensenumber, int aparkedminutes, int aminutespurchased);
~PoliceOfficer();
void setname(string);
void setbadge(string);
void setOfficer(string, string);
string getname() const;
string getbadge() const;
void patrol();
void displayofficer() const;
};
The PoliceOfficer
class stores the officer’s name
and badge
number. It also contains ParkedCar
(PC) and ParkingMeter
(PM) objects, allowing the officer to examine a car and its parking meter. The crucial method here is patrol()
:
void PoliceOfficer::patrol() {
if (PC.getparkedminutes() > PM.getparkingmeter()) /*parked minutes >= minutes purchased*/
{
ParkingTicket PT;
PT.generateticket();
}
else
{
cout << "No crime has been commited." << endl;
}
}
The patrol()
method checks if the parked time (PC.getparkedminutes()
) exceeds the purchased time (PM.getparkingmeter()
). If it does, it creates a ParkingTicket
object (PT
) and calls PT.generateticket()
to issue a ticket. Otherwise, it indicates that “No crime has been committed.”
Putting It All Together in main.cpp
The main.cpp
file is where the simulation runs. It creates objects of the classes we’ve discussed and simulates the interaction between them. Here’s a simplified view of the main()
function’s logic:
int main() {
int option;
PoliceOfficer officer;
ParkedCar car;
ParkingMeter meter;
do {
// ... (Get vehicle, parking, and officer information from user input) ...
officer.patrol(); // Officer patrols and potentially issues a ticket
// ... (Ask user to restart or exit) ...
} while (option != 0);
return 0;
}
The main()
function sets up a loop that:
- Prompts the user to enter information about the parked car (make, model, color, license, parked minutes), parking meter (minutes purchased), and police officer (name, badge).
- Creates
ParkedCar
,ParkingMeter
, andPoliceOfficer
objects using the input data. - Calls the
officer.patrol()
method to initiate the parking inspection and potential ticket issuance. - Asks the user if they want to run the simulation again or exit.
This loop allows for multiple simulations without restarting the program.
Conclusion: OOP in Action – Simulating Real-World Scenarios
This parking ticket simulator provides a clear and concise example of object-oriented programming in C++. By breaking down the problem into distinct classes – ParkedCar
, ParkingMeter
, ParkingTicket
, and PoliceOfficer
– the code becomes more modular, readable, and easier to understand. Each class has a specific responsibility, and they interact with each other to simulate the parking ticket process.
This example demonstrates key OOP principles like:
- Encapsulation: Each class bundles data (attributes) and methods (functions) that operate on that data.
- Abstraction: We are working with simplified representations of real-world entities (cars, parking meters, officers).
- Object Interaction: Objects of different classes (like
PoliceOfficer
andParkedCar
) interact to perform actions in the simulation.
By studying and modifying this code, you can solidify your understanding of OOP concepts in C++ and learn how to apply them to solve practical problems by simulating real-world scenarios. This example serves as a stepping stone to more complex OOP projects and a valuable learning tool for anyone starting their C++ programming journey.