Click here to Skip to main content
15,029,990 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Create a class for each vehicle accident category and define necessary attributes and methods.
Each class must store its state in private variables. It should use a static variable to maintain a count of all instances of the class. All required access for any of the classes data members should be done using public getter (accessor) member functions. The public methods which you must implement allow the user to do the following:

- Initiate, get and set the state of any created object in any class.

- Create a new vehicle accidents data object for a given country.
- Find and display the most frequent accident category from the collected data.
- Display percentage of causalities in accidents involving drivers over age 60.
- Compare the number of accidents that occur within urban areas to those accidents that occur on highways.
- Display the country name and accidents list which shows the accident details for the drivers involved in accidents having driver's license issued after 2019.
- Display (print) the vehicle accidents data.
- Save the information of all the created vehicle accidents data into a file.

If the user attempts do an operation that will violate the state of objects (for example, enter a license issuance date in the future), you must check for it. In that case, the operation should be ignored, and the application should display an error message without exiting execution.

Phase 2:

The application should use a text file to capture the whole vehicle accidents data structure. To do this, add two methods to your center class:
1. A method to save the content of the created structure (object) into a text file using file streams as an accident report in tabular form.
2. A method to save/restore the content in an appropriate structure (object) for next run time using file streams.

C++ provides a facility to create your own exceptions which are basically derived classes of standard “exception” type. Write a class that defines a duplicate exception and is derived from a standard class of exception type. The duplicate exception will be thrown if an accident is duplicated in the accidents list.

Design and implement a separate testing class for your application. For your testing class you should prepare test data to test your application using functions (unit tests) that executes different scenarios (designed by you) in your project and compare the output of your program with an expected output that you prepared.

What I have tried:

I’m done with phase one, I can’t quite get to phase 2.
Updated 29-May-21 10:05am
KarstenK 30-May-21 4:49am
tip: very important to count down the instances in the destructors. Or you count ALL created instances - with some already gone!!!

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
From part 1, you have a bunch of AccidentRecords. Define operators << and >> to write/read a record to/from a stream:
std::ostream& operator<<(std::ostream& stream, const AccidentRecord& record)
   // write fields in RECORD to STREAM

std::istream& operator<<(std::istream& stream, AccidentRecord& record)
   // read from STREAM to populate the fields in RECORD
You can now iterate over the records and write them to a file.

Next, implement operator== to compare records:
bool AccidentRecord::operator==(const AccidentRecord& that)
   for each field
      if(this->field != that.field) return false;
   return true;
Finally, derive from exception and write a function to import all records from a file where they were saved:
class DuplicateRecordException
   (const AccidentRecord& record) : public std::exception
   DuplicateRecordException(const AccidentRecord& record)
      std::cout << what() << ":\n";
      std::cout << record << '\n';

   const char* what() const noexcept override
      return "Duplicate accident record in database";

void Import(const std::string& path,
   std::vector< AccidentRecord >& database)
   auto file = std::unique_ptr< std::istream >(new std::ifstream(path));

      AccidentRecord record;
      file >> record;


   //  iterate over database to compare each pair of records...
   if(record[i] == record[j])
      throw DuplicateRecordException(record[i]);
That's a sketch. You need to improve it with error handling and so on. And you'll have to write the test harness yourself. I never bother with tests. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900