Need to write a program for a C++ class. The professor wants us to implement Virtual functions, virtual classes, and operator overloading, but I don't understand why you would want these in this problem. I guess I need help with the initial design, rather than syntax, or anything like that.
This assignment should reflect Templates, Overloading and Virtual
functions, virtual classes and dynamic arrays.
For this assignment you will take the results of a recent election for the
mayor of a small city and report the results in different formats. The city has
three precincts and you can be sure that there will never be more than ten
candidates running for the mayor’s position.
First you must read the results from a data file and store the values using
dynamic arrays (pointers). The file has the last name of each candidate and the
number of votes received in each of the three precincts on one line. There
could be any number of lines in the file so count them as you go.
The first report should include the results in a tabular form, with the total
votes for each candidate at the right side of the table, and the totals for
each precinct along the bottom.
Use appropriate headings and line up the columns properly. Also print the
total number of votes cast in the election.
For the second report, the candidates want to know how they did in each
precinct – as a percent of the votes cast in that precinct. Again, print it in
tabular form using one decimal place in each of the percentages.
Finally, you must print the results arranged in order from the winner down to
the person with the fewest votes. This means that you will have to sort the
array holding each candidates totals, keeping the names array in parallel. The
report should include only the name of each candidate, the total votes received
by that candidate and the percentage of the votes cast in the election for that
candidate – arranged in order from largest to smallest. Again, print the
percentages with one decimal place.
I understand Virtual functions and classes, and I think I understand operator overloading. But I don't understand how they apply to this problem. Please help?
It looks to me like it could be made easier with functions. But maybe I'm wrong. Anyway, since you need to use virtual classes, you could make an abstract class called Report, that will have basic functionality for reports. You have two reports to print - so you could overload operator<< for ostream,(so friend function involved) and use it to print these reports like this: