C++学习大作业之实现投票功能

1.任务是实现一个投票系统.

2.投票规则:

  • 1.每个投票者只可以投一次票。投票者通过生成的随机数,在第一个到最后一个候选人之间选择一个候选人来进行投票。
  • 2.每一个候选人都保存着投票者给他们投票的记录。
  • 3.选举之后,拥有最高投票数的候选人当选。

主要需要的类有Person(作为Voter和Candidate的父类)、Voter类(投票者类)、Candidate(选举者类)、PersonSet类(容器类).

Person.hpp 

#ifndef Person_hpp
#define Person_hpp

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

class Person{
protected:
    char* _name;
    int _age;
    double _salary;
    int _id;
    static int _totalPersons;//可用于id累加
public:
    Person(const char* name,int age,double salary);
    void SetAge(int newAge);
    void SetSalary(double newSalary);
    int getAge();
    int getSalary();
    void setTotalPerson(int t);
    void setID(int id);
    friend ostream& operator<<(ostream& out,Person& p);
    Person& operator=(Person& p);
    void print();
    ~Person();
};

#endif /* Person_hpp */

Person.cpp

//  Person.cpp


#include "Person.hpp"

int Person::_totalPersons=0;
Person::Person(const char* name,int age,double salary){
    _name = new char[strlen(name)+1];
    strcpy(_name, name);
    _age = age;
    _salary = salary;
    _totalPersons++;
    _id = _totalPersons;
}

void Person::SetAge(int newAge){
    _age = newAge;
}

void Person::SetSalary(double newSalary){
    _salary = newSalary;
}

void Person::setTotalPerson(int t){
    _totalPersons = t;
}

void Person::setID(int id){
    _id = id;
}

int Person::getAge(){
    return _age;
}

int Person::getSalary(){
    return _salary;
}



ostream& operator<<(ostream& out,Person& p){
    out<<"ID:"<<p._id<<endl;
    out<<"name:"<<p._name<<endl;
    out<<"age:"<<p._age<<endl;
    out<<"salary:"<<p._salary<<endl;
    out<<"--------------------------------------------\n";
    return out;
}

Person& Person::operator=(Person& p){
    if(this!=&p){
        delete [] _name;
        _name = new char[strlen(p._name)+1];
        strcpy(_name, p._name);
  • 9
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值