指针和引用,房间“有一个”键

时间:2013-04-23 20:51:20

标签: c++ pointers

我基本上想说,如果当前房间包含一个我在下面指定的密钥,然后告诉用户,房间有一把钥匙,我会把剩下的都弄清楚。

int main()
{

    Room mainHall;
    Room sittingRoom;

    mainHall.setNorthExit(&sittingRoom);
    mainHall.setEastExit(&playRoom);

    mainHall.setDescription("The main hall has 2 exits, one to the north, one to the east");
    mainHall.setName("Main Hall");

    sittingRoom.setEastExit(&kitchen);
    sittingRoom.setSouthExit(&mainHall);

    sittingRoom.setDescription("The sitting room has 2 exits, one to the east and one to the south");
    sittingRoom.setName("Sitting Room");

    string userInput;

    Room* currentRoom;
    currentRoom = &mainHall;

    Keys& key1 = playRoom.getKey();
    key1.setName("Sitting Room");

    Keys& key2 = sittingRoom.getKey();
    key2.setName("Play Room");

    if(////need code here) ///I want it to be something like...if(currentRoom "has a key"
    {
        string takeKey;
        cout << "There is still a key in this room, would you like to take it? " << endl;
        cin >> takeKey;
        if(takeKey == "yes")
        {
               //Havent assigned anything yet
        }
    }

这只是我的keye类的cpp文件,以防你需要它

#include"Keys.h"
#include<iostream>

Keys::Keys()
{
    name = "";
}

Keys::Keys(string nameParam)
{
    name = nameParam;
}

void Keys::setName(string nameParam)
{
    name = nameParam;
}

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

void Keys::setKey(string keyParam)
{
    key = keyParam;
}

string Keys::getKey() const
{
    return key;
}

和我的房间类中的getter方法一样

Keys& Room::getKey()
{
    return key;
}

我很感激任何帮助

1 个答案:

答案 0 :(得分:0)

如何在房间类上使用'HasKey()'方法,然后使用

if( sittingRoom.HasKey()) { ... }

在房间类中使用布尔值来确定房间是否有钥匙。