CS304 Object Oriented Programming Assignment 5 Solution Spring 2013

Consider the following Class Diagram; detailed description of the diagram is given in the table.

 

Class Name Attribute Name Attribute Data Type Behavior (Functions)
Voter Password Character String -Default Constructor()

-Login _data()

-Cast_vote()

CNIC_No Integer
Disable     -Cast_vote()
Overseas     -Cast_vote()

 

 

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:

 

  1. Make Voter base/parent class and inherit classes Disable and Overseas from it.
  2. In voter class:
    1. Make Cast_vote ( ) function a pure virtual function.
    2. Login_data() should be a  non-virtual function.

 

  1. In Disable class:
    1. Make Cast_vote() a virtual function.
    2. 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.

 

  1. In Overseas class:
    1. Make Cast_vote() a virtual function.
    2. 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.
#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 Menun";
 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 Menun";
 cout << "n Select your option: (1 for Bat and 2 for Ball)n";
 cout << "n 1. Bat";
 cout << "n 2. Balln";
 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 << "nn Do you want to cast vote (Y/N): ";
 cin >> choice;
 if(choice == 'Y' || choice == 'y')
 {
 cout << "nn Press D or d for disable voter.";
 cout << "nn 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 << "nn Incorrect option.n";
 }
 }
 else if(choice == 'N' || choice == 'n')
 {
 cout << "n Press any key to Exit... ";
 break;
 }
 else
 {
 break;
 }
 }
 cout << "nn Thanks for using this program.nn";

 system("pause");

 return 0;
}


 

DOWNLOAD SOLUTION HERE
loading...