CS304 Object Oriented Programming Assignment 5 Idea Solution Spring On July 2013
Consider the following Class Diagram; detailed description of the diagram is given in the table.
In given class diagram, voter is a base class; while Disable and Overseas is its derived classes sharing its all attributes and functions.
You are required to implement the above class diagram in C++. For this you have to use the concept of inheritance and polymorphism. In order to cast vote, your program should be able to produce different interfaces for each category of voter e.g. interface for disable voter should be different from the interface of overseas voter.
Solution Guidelines:
- Make Voter base/parent class and inherit classes Disable and Overseas from it.
- In voter class:
- Make Cast_vote ( ) function a pure virtual function.
- Login_data() should be a non-virtual function.
- In Disable class:
- Make Cast_vote() a virtual function.
- Login_data( ) function of disable class should generate an interface that would help disable voters to cast their vote easily. In the body of this function, you also have to call Login_data() function of Voter.
- In Overseas class:
- Make Cast_vote() a virtual function.
- Cast_vote() function of overseas class should generate an interface that would help overseas voters to cast their vote easily. In the body of this function, you also have to call Login_data() function of Voter
Solution:
// Changed Vote Cast Method and Override the loginData method in disable and overseas voter.
cccccccccccccccccccccccccccccc
#include <iostream>
using namespace std;
//...Class Voter
class Voter
{
private:
char password[25];
int cnic;
public:
virtual void castVote() = 0; //...Pure Virtual Function
//..Login Data Funtion will take input
void loginData()
{
cout << " \n Please Enter your password: ";
cin >> password;
cout << " \n Please enter your cnic: ";
cin >> cnic;
cout << "\n Login Successfull!";
}
};
//..Class Disable Publicly Inherited from Voter
class Disable : public Voter
{
//overriding the loginData()
void loginData()
{
Voter::loginData();
int voteNo = 0;
cout << "\n Vote Casting Menu\n";
cout << "\n 1. Button1";
cout << "\n 2. Button2";
cout << "\n Type 1 for press button 1 (for Bat) and type 2 to press button 2 (for Ball): ";
-->
cin >> voteNo;
if(voteNo == 1)
{
cout << "\n Your vote is successfully to casted to Bat.";
}
else if(voteNo == 2)
{
cout << "\n Your vote is successfully to casted to Ball.";
}
}
//implementing the castVote method
void castVote()
{
//calling the loginData method
loginData();
}
};
//..Class Overseas Publicly Inherited from Voter
class Overseas : public Voter
{
//Overriding the LoginData()
void loginData()
{
Voter::loginData();
int voteNo = 0;
cout << "\n Vote Casting Menu\n";
cout << "\n Select your option: (1 for Bat and 2 for Ball)\n";
cout << "\n 1. Bat";
cout << "\n 2. Ball\n";
cin >> voteNo;
if(voteNo == 1)
{
cout << "\n Your vote is successfully to casted to Bat.";
}
else if(voteNo == 2)
{
cout << "\n Your vote is successfully to casted to Ball.";
}
}
//implementing the castVote method
void castVote()
{
//calling the loginData method
loginData();
}
};
///.... Main
int main(int argc, const char * argv[])
{
char choice;
Voter *voters;
while (true)
{
cout << "\n\n Do you want to cast vote (Y/N): ";
cin >> choice;
if(choice == 'Y' || choice == 'y')
{
cout << "\n\n Press D or d for disable voter.";
cout << "\n\n Press O or o for overseas voter.\n";
cin >> choice;
if (choice == 'D' || choice == 'd')
-->
{
voters = new Disable();
//here comes the polymorphism. The correct method based on the underlying object will be called.
voters->castVote();
}
else if (choice == 'O' || choice == 'o')
{
voters = new Overseas();
//here comes the polymorphism. The correct method based on the underlying object will be called.
voters->castVote();
}
else
{
cout << "\n\n Incorrect option.\n";
}
}
else if(choice == 'N' || choice == 'n')
{
cout << "\n Press any key to Exit... ";
break;
}
else
{
break;
}
}
cout << "\n\n Thanks for using this program.\n\n";
system("pause");
return 0;
}