Knowee
Questions
Features
Study Tools

Develop a base class called Person with attributes like name, age, and gender. Derive a Voter class from Person, which extends its functionalities with attributes such as voter_id and hasVoted (a Boolean indicating whether the voter has cast their vote). Extend Person with a Candidate class, which introduces attributes like candidate_id and numVotes (representing the number of votes the candidate has received). Implement the described scenario in C++ using appropriate classes and methods to display the count of registered voters and the tally of votes cast.Test Case_1:Input:2Mr.Santhosh 18 MaleVID_101 YesCID_20CSE101 2Mr.Kavin 21 MaleVID_102 YesCID_20CSE102 1Output:number of voters registered: 2

Question

Develop a base class called Person with attributes like name, age, and gender. Derive a Voter class from Person, which extends its functionalities with attributes such as voter_id and hasVoted (a Boolean indicating whether the voter has cast their vote). Extend Person with a Candidate class, which introduces attributes like candidate_id and numVotes (representing the number of votes the candidate has received). Implement the described scenario in C++ using appropriate classes and methods to display the count of registered voters and the tally of votes cast.Test Case_1:Input:2Mr.Santhosh 18 MaleVID_101 YesCID_20CSE101 2Mr.Kavin 21 MaleVID_102 YesCID_20CSE102 1Output:number of voters registered: 2

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the C++ code for the scenario you described:

#include<iostream>
#include<string>
using namespace std;

class Person {
public:
    string name;
    int age;
    string gender;
};

class Voter : public Person {
public:
    string voter_id;
    bool hasVoted;

    Voter(string name, int age, string gender, string voter_id, bool hasVoted) {
        this->name = name;
        this->age = age;
        this->gender = gender;
        this->voter_id = voter_id;
        this->hasVoted = hasVoted;
    }
};

class Candidate : public Person {
public:
    string candidate_id;
    int numVotes;

    Candidate(string name, int age, string gender, string candidate_id, int numVotes) {
        this->name = name;
        this->age = age;
        this->gender = gender;
        this->candidate_id = candidate_id;
        this->numVotes = numVotes;
    }
};

int main() {
    Voter v1("Mr.Santhosh", 18, "Male", "VID_101", true);
    Candidate c1("Mr.Santhosh", 18, "Male", "CID_20CSE101", 2);

    Voter v2("Mr.Kavin", 21, "Male", "VID_102", true);
    Candidate c2("Mr.Kavin", 21, "Male", "CID_20CSE102", 1);

    int voterCount = 0;
    int voteCount = 0;

    if (v1.hasVoted) {
        voterCount++;
        voteCount += c1.numVotes;
    }

    if (v2.hasVoted) {
        voterCount++;
        voteCount += c2.numVotes;
    }

    cout << "Number of voters registered: " << voterCount << endl;
    cout << "Tally of votes cast: " << voteCount << endl;

    return 0;
}

This code first defines a base class Person with attributes name, age, and gender. Then it defines two derived classes Voter and Candidate with additional attributes. In the main function, it creates instances of Voter and Candidate, counts the number of voters and the total votes, and then prints out the results.

This problem has been solved

Similar Questions

Write a program to check whether a person is eligible for voting or not. (accept age from user)

There are 3700037000 people in a town who can vote in a local election. They are categorised by age as shown in the table below.        Age             Number of people    Under 404014700147004040 or over 2230022300 A market research company intends to take a survey of the voting intentions of 18001800 residents just before the election. The number of people aged 4040 or over who should be in the sample is calculated asSelect one:a.   2230037000×18002230037000×1800. b.   223001800×37000223001800×37000. c.   2230014700×1002230014700×100. d.   1470037000×1800

A political candidate surveys voters to learn about their opinions on key issues. The politician’s data team notices that most respondents are people who have already voted in the current election. The fairness of the survey could be improved by over-sampling people from which group?1 pointPeople who have not yet voted in the electionPeople who voted against the candidate in the current electionFirst-time voters who already voted in the current electionPeople who voted for the candidate in the current election

Program to check if a person can vote

list out in detail functional and non functional reuirements of online voting system

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.