14 in the programming example election results the class candidatetype contains the 5347101

14. In the Programming Example Election Results, the class candidateType
contains the function calculateTotalVotes. After processing the voting
data, this function calculates the total number of votes received by a candidate.
The function updateVotesByRegion (of the class candidateType)
updates only the number of votes for a particular region.Modify the definition
of this function so that it also updates the total number of votes received by the
candidate. By doing so, the function addVotes in the main program is no
longer needed.Modify and run your program with the modified definition of
the function updateVotesByRegion.

Here is the class provided in the textbook

#include
#include “personType.h”
using namespace std;
const int NO_OF_REGIONS = 4;
class candidateType: public personType
{
public:
const candidateType& operator=(const candidateType&);
//Overload the assignment operator for objects of the
//type candidateType
const candidateType& operator=(const personType&);
//Overload the assignment operator for objects so that
//the value of an object of type personType can be
//assigned to an object of type candidateType
void updateVotesByRegion(int region, int votes);
//Function to update the votes of a candidate for a
//particular region.
//Postcondition: Votes for the region specified by the
// parameter are updated by adding the votes specified
// by the parameter votes.
void setVotes(int region, int votes);
//Function to set the votes of a candidate for a
//particular region.
//Postcondition: Votes for the region specified by the
// parameter are set to the votes specified by the
// parameter votes.
void calculateTotalVotes();
//Function to calculate the total votes received by a
//candidate.
//Postcondition: The votes in each region are added and
// assigned to totalVotes.

1
0
int getTotalVotes() const;
//Function to return the total votes received by a
//candidate.
//Postcondition: The value of totalVotes is returned.
void printData() const;
//Function to output the candidate's name, the votes
//received in each region, and the total votes received.
candidateType();
//Default constructor.
//Postcondition: Candidate's name is initialized to blanks,
// the number of votes in each region, and the total
// votes are initialized to 0.
//Overload the relational operators.
bool operator==(const candidateType& right) const;
bool operator!=(const candidateType& right) const;
bool operator
bool operator
bool operator>=(const candidateType& right) const;
bool operator>(const candidateType& right) const;
private:
int votesByRegion[NO_OF_REGIONS]; //array to store the votes
// received in each region
int totalVotes; //variable to store the total votes
};
The definitions of the member functions of the class candidateType are given
next.
To set the votes of a particular region, the region number and the number of votes are
passed as parameters to the function setVotes. Because an array index starts at 0,
region 1 corresponds to the array component at position 0, and so on. Therefore, to
set the value of the correct array component, 1 is subtracted from the region. The
definition of the function setVotes is as follows:
void candidateType::setVotes(int region, int votes)
{
votesByRegion[region – 1] = votes;
}
To update the votes for a particular region, the region number and the number of
votes for that region are passed as parameters. The votes are then added to the region’s
previous value. The definition of the function updateVotesByRegion is as follows:
void candidateType::updateVotesByRegion(int region, int votes)
{
votesByRegion[region – 1] = votesByRegion[region – 1] + votes;

}

The definitions of the functions calculateTotalVotes, getTotalVotes,
printData, the default constructor, and getName are given next:
void candidateType::calculateTotalVotes()
{
totalVotes = 0;
for (int i = 0; i
totalVotes += votesByRegion[i];
}
int candidateType::getTotalVotes() const
{
return totalVotes;
}
void candidateType::printData() const
{
cout

cout
for (int i = 0; i
cout
cout
}
candidateType::candidateType()
{
for (int i = 0; i
votesByRegion[i] = 0;
totalVotes = 0;
}
To overload the relational operators for the class candidateType, the names of
the candidates are compared. For example, two candidates are the same if they have
the same name. The definitions of these functions are similar to the definitions of the
functions to overload the relational operators for the class personType. We only
give the definition of the function to overload the operator == and leave others as an
exercise for you; see Programming Exercise 13.
bool candidateType::operator==(const candidateType& right) const
{
return (firstName == right.firstName
&& lastName == right.lastName);
}

"Get 15% discount on your first 3 orders with us"
Use the following coupon
FIRST15

Order Now