Here is the code for the project:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#ifndef H_personType
#define H_personType
#include <iostream>
#include <string>
using namespace std;
class personType
{
//Overload the stream insertion and extraction operators.
friend istream& operator>>(istream&, personType&);
friend ostream& operator<<(ostream&, const personType&);
public:
const personType& operator=(const personType&);
//Overload the assignment operator.
void setName(string first, string last);
//Function to set firstName and lastName according to
//the parameters.
//Postcondition: firstName = first; lastName = last
void getName(string& first, string& last);
//Function to return firstName and lastName via the
//parameters. //Postcondition: first = firstName; last = lastName
personType(string first = "", string last = "");
//constructor with parameters
//Set firstName and lastName according to the parameters.
//Postcondition: firstName = first; lastName = last
//Overload the relational operators.
bool operator==(const personType& right) const;
bool operator!=(const personType& right) const;
bool operator<=(const personType& right) const;
bool operator<(const personType& right) const;
bool operator>=(const personType& right) const;
bool operator>(const personType& right) const;
protected:
string firstName; //variable to store the first name
string lastName; //variable to store the last name
};
#endif
|
personTypeImp
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;
void personType::setName(string first, string last)
{
//cout<<"See Programming Exercise 11"<<endl;
firstName = first;
lastName = last;
}
void personType::getName(string& first, string& last)
{
//cout<<"See Programming Exercise 11"<<endl;
first = firstName;
last = lastName;
}
//constructor
personType::personType(string first, string last)
{
//cout<<"See Programming Exercise 11"<<endl;
firstName = first;
lastName = last;
}
const personType& personType::operator=(const personType& right)
{
//cout<<"See Programming Exercise 11"<<endl;
if(this != &right) //avoid self=assignment
firstName = right.firstName;
lastName = right.lastName;
return *this;
}
bool personType::operator==(const personType& right) const
{
return(firstName == right.firstName
&& lastName == right.lastName);
}
bool personType::operator!=(const personType& right) const
{
cout<<"See Programming Exercise 11"<<endl;
//cout<<"See Programming Exercise 11."<<endl;
return (firstName != right.firstName
&& lastName != right.lastName);
return false;
}
bool personType::operator<=(const personType& right) const
{
cout<<"See Programming Exercise 11"<<endl;
return (firstName <= right.firstName
&& lastName <= right.lastName);
return false;
}
bool personType::operator<(const personType& right) const
{
cout<<"See Programming Exercise 11"<<endl;
return false;
}
bool personType::operator>=(const personType& right) const
{
cout<<"See Programming Exercise 11"<<endl;
return (firstName<= right.firstName
&& lastName <= right.lastName);
return false;
}
bool personType::operator>(const personType& right) const
{
cout<<"See Programming Exercise 11"<<endl;
return (firstName > right.firstName
&& lastName > right.lastName);
return false;
}
istream& operator>>(istream& is, personType& pName)
{
is>>pName.firstName>>pName.lastName;
return is;
}
ostream& operator<<(ostream& os, const personType& pName)
{
cout<<"See Programming Exercise 11"<<endl;
os <<pName.firstName<<pName.lastName;
return os;
}
|
The candidateType
[code]
#ifndef H_candidateType
#define H_candidateType
#include <string>
#include "personType.h"
using namespace std;
const int noOfRegions = 4;
class candidateType: public personType
{
public:
const candidateType& operator=(const candidateType&);
//Overload the assignment operator for the objects of the
//type candidateType.
const candidateType& operator=(const personType&);
//Overload the assignment operator for the objects so
//that the value of an object of the type personType can
//be assigned to an object of the type candidateType.
void setVotes(int region, int votes);
//Function to set the votes of a candidate for a
//particular region.
//Postcondition: The votes specified by the parameter votes
// are assigned to the region specified by the
// parameter region.
void updateVotesByRegion(int region, int votes);
//Function to update the votes of a candidate for a
//particular region.
//Postcondition: The votes specified by the parameter votes
// are added to the region specified by the
// parameter region.
void calculateTotalVotes();
//Function to calculate the total votes received by a
//candidate.
//Postcondition: The votes received in each region are added.
int getTotalVotes();
//Function to return the total votes received by a
//candidate.
//Postcondition: The total votes received by the candidate
// are 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: Initialize the votes received in each
// region, and the total votes received, to zero.
//Overload the relational operators.
bool operator==(const candidateType& right) const;
bool operator!=(const candidateType& right) const;
bool operator<=(const candidateType& right) const;
bool operator<(const candidateType& right) const;
bool operator>=(const candidateType& right) const;
bool operator>(const candidateType& right) const;
private:
int votesByRegion[noOfRegions];
int totalVotes;
};
#endif
--------------------Election Results--------------------
Votes
Candidate Name Region1 Region2 Region3 Region4 Total
--------------------- ------- ------- ------- ------- ------
4653430
4653430
4653430
4653430
4653430
4653430
Winner: , Votes Received: 0
Total votes polled:4653430
Process returned 0 (0x0) execution time : 0.066 s
Press any key to continue.