首页 > 编程语言 >C++ 类的项目练习 定义一个类,来表示某模拟养成游戏中人物: 每个人物, 有昵称,年龄,性别, 配偶, 朋友, 支持的活动有:结婚,离婚, 交友,断交,询问昵称,询问性别,询问年龄, 简介等。

C++ 类的项目练习 定义一个类,来表示某模拟养成游戏中人物: 每个人物, 有昵称,年龄,性别, 配偶, 朋友, 支持的活动有:结婚,离婚, 交友,断交,询问昵称,询问性别,询问年龄, 简介等。

时间:2022-11-20 10:13:17浏览次数:36  
标签:Hero string gender 询问 昵称 other const include 性别

Hero.h:

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
typedef enum gender {
    Man, //男
    Women //女
}gender_t;
class Hero
{
public:
    Hero();
    Hero(const string &name,const int &age,const gender_t &gender);
    string getName() const;
    int getAge() const;
    gender getGender() const;
    vector<Hero*> getFriends() const;
    Hero* getLover() const;
    string description() const;

    void marry(Hero &other);
    void divorce(Hero &other);
    void addFriend(Hero &other);
    void delFriend(Hero &other);
private:
    string name;
    int age;
    gender_t gender;
    Hero* lover;
    vector <Hero*> friends;
};

Hero.cpp:

#include "Hero.h"

Hero::Hero()
{
    name = "";
    age = 0;
}

Hero::Hero(const string& name, const int& age, const gender_t& gender)
{
    this->name = name;
    this->age = age;
    this->gender = gender;
}

string Hero::getName() const
{
    return name;
}

int Hero::getAge() const
{
    return age;
}

gender Hero::getGender() const
{
    return gender;
}

vector<Hero*> Hero::getFriends() const
{
    return friends;
}

Hero* Hero::getLover() const
{
    return lover;
}

string Hero::description() const
{
    stringstream ret;
    ret << name << "-age:" << age << "-" << (gender==Man?"男":"女");
    return ret.str();
}

void Hero::marry(Hero& other)
{
    if (gender == other.gender) {
        return;
    }
    this->lover = &other;
    other.lover = this;
}

void Hero::divorce(Hero &other)
{
    if (this->lover == NULL) {
        return;
    }
    this->lover = NULL;
    lover = NULL;
}

void Hero::addFriend(Hero& other)
{
    friends.push_back(&other);
}

void Hero::delFriend(Hero& other)
{
    for (auto is = friends.begin(); is != friends.end();) {
        if (*is == &other) {
            is = friends.erase(is);//返回下一个成员的"迭代器"
        }
        else {
            is++;
        }
    }
}

main.cpp:

#include <iostream>
#include <vector>
#include <string>
#include "Hero.h"

using namespace std;

int main() {
    Hero lhc("令狐冲", 25, Man);
    Hero ryy("任盈盈", 26, Women);
    Hero tbg("田伯光", 30, Man);
    Hero yls("岳灵珊", 20, Women);
    Hero cx("冲虚道长", 55, Man);

    lhc.marry(yls);
    Hero* who = lhc.getLover();
    cout << lhc.getName() << "的配偶是:" << who->description() << endl;
    cout << who->getName() << "的配偶是:" << who->getLover()->description() << endl;

    cout << lhc.getName() << "离婚。" << endl;
    lhc.divorce(yls);
    if (lhc.getLover() == NULL) {
        cout << lhc.getName() << "单身" << endl;
    }

    lhc.addFriend(tbg);
    lhc.addFriend(cx);
    auto friends = lhc.getFriends();
    cout << lhc.getName() << "的朋友:" << endl;
    for (int i = 0; i < friends.size(); i++) {
        cout << friends[i]->description() << endl;
    }

    cout << lhc.getName() << "删除好友:" << tbg.getName() << endl;
    lhc.delFriend(tbg);
    friends = lhc.getFriends();
    cout << lhc.getName() << "的朋友:" << endl;
    for (int i = 0; i < friends.size(); i++) {
        cout << friends[i]->description() << endl;
    }

    system("pause");
    return 0;
}

标签:Hero,string,gender,询问,昵称,other,const,include,性别
From: https://www.cnblogs.com/smartlearn/p/16907929.html

相关文章