Need help with implementation of Virtual & Overloading

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.

Here's the assignment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.


This is the data file he provided:
1
2
3
4
5
6
7
Lincoln 120 300 400
Parks 100 500 250
Shakespeare 0 30 50
Ghandi 250 100 40
Ashe 300 50 175
Rodriguez 87 118 320
Curie 284 0 112


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?
Last edited on
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:
1
2
3
FReport rep;
//...
std::cout<<"Tabular form: " << std::endl << rep;

I don't get into the details, but if I had to use all these things, that would probably be my starting point.
Last edited on
Topic archived. No new replies allowed.